From 91d984df0345eae0c67c844b911c1e85bf403d37 Mon Sep 17 00:00:00 2001 From: Frederic Langlet Date: Thu, 18 Jan 2024 15:33:24 -0800 Subject: [PATCH] A few perf tweaks --- v2/entropy/EntropyUtils.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/v2/entropy/EntropyUtils.go b/v2/entropy/EntropyUtils.go index 47425c1b..ecf49de4 100644 --- a/v2/entropy/EntropyUtils.go +++ b/v2/entropy/EntropyUtils.go @@ -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++ } @@ -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 @@ -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