fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

scoring: reduce allocations for addScore (#670)

We add a bit more complexity to addScore but avoid allocating a string if debugScore = false.

Test plan:
I ran a couple of benchmarks and confirmed we don't allocate.

+23 -9
+23 -9
eval.go
··· 21 21 "math" 22 22 "regexp/syntax" 23 23 "sort" 24 + "strconv" 24 25 "strings" 25 26 "time" 26 27 ··· 32 33 33 34 const maxUInt16 = 0xffff 34 35 35 - func (m *FileMatch) addScore(what string, s float64, debugScore bool) { 36 - if s != 0 && debugScore { 37 - m.Debug += fmt.Sprintf("%s:%.2f, ", what, s) 36 + // addScore increments the score of the FileMatch by the computed score. If 37 + // debugScore is true, it also adds a debug string to the FileMatch. If raw is 38 + // -1, it is ignored. Otherwise, it is added to the debug string. 39 + func (m *FileMatch) addScore(what string, computed float64, raw float64, debugScore bool) { 40 + if computed != 0 && debugScore { 41 + var b strings.Builder 42 + fmt.Fprintf(&b, "%s", what) 43 + if raw != -1 { 44 + fmt.Fprintf(&b, "(%s)", strconv.FormatFloat(raw, 'f', -1, 64)) 45 + } 46 + fmt.Fprintf(&b, ":%.2f, ", computed) 47 + m.Debug += b.String() 38 48 } 39 - m.Score += s 49 + m.Score += computed 40 50 } 41 51 42 52 func (m *FileMatch) addKeywordScore(score float64, sumTf float64, L float64, debugScore bool) { ··· 408 418 atomMatchCount++ 409 419 }) 410 420 421 + addScore := func(what string, computed float64) { 422 + fileMatch.addScore(what, computed, -1, opts.DebugScore) 423 + } 424 + 411 425 // atom-count boosts files with matches from more than 1 atom. The 412 426 // maximum boost is scoreFactorAtomMatch. 413 427 if atomMatchCount > 0 { 414 - fileMatch.addScore(fmt.Sprintf("atom(%d)", atomMatchCount), (1.0-1.0/float64(atomMatchCount))*scoreFactorAtomMatch, opts.DebugScore) 428 + fileMatch.addScore("atom", (1.0-1.0/float64(atomMatchCount))*scoreFactorAtomMatch, float64(atomMatchCount), opts.DebugScore) 415 429 } 416 430 417 431 maxFileScore := 0.0 ··· 436 450 // Maintain ordering of input files. This 437 451 // strictly dominates the in-file ordering of 438 452 // the matches. 439 - fileMatch.addScore("fragment", maxFileScore, opts.DebugScore) 453 + addScore("fragment", maxFileScore) 440 454 441 455 if opts.UseDocumentRanks && len(d.ranks) > int(doc) { 442 456 weight := scoreFileRankFactor ··· 452 466 // The file rank represents a log (base 2) count. The log ranks should be bounded at 32, but we 453 467 // cap it just in case to ensure it falls in the range [0, 1]. 454 468 normalized := math.Min(1.0, ranks[0]/32.0) 455 - fileMatch.addScore("file-rank", weight*normalized, opts.DebugScore) 469 + addScore("file-rank", weight*normalized) 456 470 } 457 471 } 458 472 459 473 md := d.repoMetaData[d.repos[doc]] 460 - fileMatch.addScore("doc-order", scoreFileOrderFactor*(1.0-float64(doc)/float64(len(d.boundaries))), opts.DebugScore) 461 - fileMatch.addScore("repo-rank", scoreRepoRankFactor*float64(md.Rank)/maxUInt16, opts.DebugScore) 474 + addScore("doc-order", scoreFileOrderFactor*(1.0-float64(doc)/float64(len(d.boundaries)))) 475 + addScore("repo-rank", scoreRepoRankFactor*float64(md.Rank)/maxUInt16) 462 476 463 477 if opts.DebugScore { 464 478 fileMatch.Debug = strings.TrimSuffix(fileMatch.Debug, ", ")