Skip to content

Commit

Permalink
A few perf tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
flanglet committed Jan 18, 2024
1 parent e3a0276 commit 91d984d
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions v2/entropy/EntropyUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ func NormalizeFrequencies(freqs []int, alphabet []int, totalFreq, scale int) (in

// Shortcut
if totalFreq == scale {
for i := 0; i < 256; i++ {
if freqs[i] != 0 {
for i, f := range freqs {
if f != 0 {
alphabet[alphabetSize] = i
alphabetSize++
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func NormalizeFrequencies(freqs []int, alphabet []int, totalFreq, scale int) (in
inc = -1
}

if absDelta*20 < freqs[idxMax] {
if absDelta*10 < freqs[idxMax] {
// Fast path (small error): just adjust the max frequency
freqs[idxMax] -= delta
return alphabetSize, nil
Expand All @@ -246,10 +246,18 @@ func NormalizeFrequencies(freqs []int, alphabet []int, totalFreq, scale int) (in

// Create queue of present symbols
for i := 0; i < alphabetSize; i++ {
if freqs[alphabet[i]] != -inc {
queue[n] = &freqSortData{freq: &freqs[alphabet[i]], symbol: alphabet[i]}
n++
if freqs[alphabet[i]] <= 2 {
// Do not distort small frequencies
continue
}

queue[n] = &freqSortData{freq: &freqs[alphabet[i]], symbol: alphabet[i]}
n++
}

if n == 0 {
freqs[idxMax] -= delta
return alphabetSize, nil
}

// Sort queue by decreasing frequency
Expand Down

0 comments on commit 91d984d

Please sign in to comment.