Skip to content

Commit

Permalink
Fix regression and more optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
flanglet committed Jan 24, 2024
1 parent 1923111 commit 05439b6
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions v2/entropy/EntropyUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ func NormalizeFrequencies(freqs []int, alphabet []int, totalFreq, scale int) (in
continue
}

if f > freqs[idxMax] {
idxMax = i
}

sf := int64(freqs[i]) * int64(scale)
var scaledFreq int

Expand All @@ -211,6 +207,10 @@ func NormalizeFrequencies(freqs []int, alphabet []int, totalFreq, scale int) (in
alphabetSize++
sumScaledFreq += scaledFreq
freqs[i] = scaledFreq

if scaledFreq > freqs[idxMax] {
idxMax = i
}
}

if alphabetSize == 0 {
Expand All @@ -224,22 +224,31 @@ func NormalizeFrequencies(freqs []int, alphabet []int, totalFreq, scale int) (in

if sumScaledFreq != scale {
delta := sumScaledFreq - scale
errThr := freqs[idxMax] >> 4
var inc, absDelta int

if sumScaledFreq < scale {
if delta < 0 {
absDelta = -delta
inc = 1
} else {
absDelta = delta
inc = -1
}

if absDelta*10 < freqs[idxMax] {
if absDelta <= errThr {
// Fast path (small error): just adjust the max frequency
freqs[idxMax] -= delta
return alphabetSize, nil
}

if delta < 0 {
freqs[idxMax] += errThr
sumScaledFreq += errThr
} else {
freqs[idxMax] -= errThr
sumScaledFreq -= errThr
}

// Slow path: spread error across frequencies
queue := make(sortByFreq, alphabetSize)
n := 0
Expand Down

0 comments on commit 05439b6

Please sign in to comment.