Skip to content

Commit

Permalink
Upgrade Go minimum release and use min/max to improve code legibility
Browse files Browse the repository at this point in the history
  • Loading branch information
flanglet committed Feb 23, 2024
1 parent be8c978 commit 6201f25
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 305 deletions.
6 changes: 1 addition & 5 deletions v2/app/BlockCompressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,7 @@ func NewBlockCompressor(argsMap map[string]any) (*BlockCompressor, error) {
concurrency = uint(runtime.NumCPU() / 2) // defaults to half the cores
}

if concurrency > _COMP_MAX_CONCURRENCY {
concurrency = _COMP_MAX_CONCURRENCY
}

this.jobs = concurrency
this.jobs = min(concurrency, _COMP_MAX_CONCURRENCY)

if prof, prst := argsMap["cpuProf"]; prst == true {
this.cpuProf = prof.(string)
Expand Down
6 changes: 1 addition & 5 deletions v2/app/BlockDecompressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,7 @@ func NewBlockDecompressor(argsMap map[string]any) (*BlockDecompressor, error) {
concurrency = uint(runtime.NumCPU() / 2) // defaults to half the cores
}

if concurrency > _COMP_MAX_CONCURRENCY {
concurrency = _COMP_MAX_CONCURRENCY
}

this.jobs = concurrency
this.jobs = min(concurrency, _COMP_MAX_CONCURRENCY)
this.verbosity = argsMap["verbosity"].(uint)
delete(argsMap, "verbosity")

Expand Down
75 changes: 14 additions & 61 deletions v2/entropy/ANSRangeCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ func NewANSRangeEncoder(bs kanzi.OutputBitStream, args ...uint) (*ANSRangeEncode
}

if order == 1 {
chkSize <<= 8

if chkSize > _ANS_MAX_CHUNK_SIZE {
chkSize = _ANS_MAX_CHUNK_SIZE
}
chkSize = min(chkSize<<8, _ANS_MAX_CHUNK_SIZE)
}
}

Expand Down Expand Up @@ -155,11 +151,7 @@ func NewANSRangeEncoderWithCtx(bs kanzi.OutputBitStream, ctx *map[string]any, ar
}

if order == 1 {
chkSize <<= 8

if chkSize > _ANS_MAX_CHUNK_SIZE {
chkSize = _ANS_MAX_CHUNK_SIZE
}
chkSize = min(chkSize<<8, _ANS_MAX_CHUNK_SIZE)
}
}

Expand Down Expand Up @@ -248,11 +240,7 @@ func (this *ANSRangeEncoder) encodeHeader(alphabet []int, frequencies []int, lr
for i := 1; i < alphabetSize; i += chkSize {
max := frequencies[alphabet[i]] - 1
logMax := uint(0)
endj := i + chkSize

if endj > alphabetSize {
endj = alphabetSize
}
endj := min(i+chkSize, alphabetSize)

// Search for max frequency log size in next chunk
for j := i + 1; j < endj; j++ {
Expand Down Expand Up @@ -294,15 +282,8 @@ func (this *ANSRangeEncoder) Write(block []byte) (int, error) {
}

sizeChunk := this.chunkSize
size := 2 * len(block)

if size > sizeChunk+(sizeChunk>>3) { // min
size = sizeChunk + (sizeChunk >> 3)
}

if size < 65536 { // max
size = 65536
}
size := min(2*len(block), sizeChunk+(sizeChunk>>3))
size = max(size, 65536)

// Add some padding
if len(this.buffer) < size {
Expand All @@ -313,13 +294,8 @@ func (this *ANSRangeEncoder) Write(block []byte) (int, error) {
startChunk := 0

for startChunk < end {
endChunk := startChunk + sizeChunk

if endChunk >= end {
endChunk = end
sizeChunk = endChunk - startChunk
}

endChunk := min(startChunk+sizeChunk, end)
sizeChunk = endChunk - startChunk
alphabetSize, err := this.rebuildStatistics(block[startChunk:endChunk], this.logRange)

if err != nil {
Expand Down Expand Up @@ -461,10 +437,7 @@ type encSymbol struct {

func (this *encSymbol) reset(cumFreq, freq int, logRange uint) {
// Make sure xMax is a positive int32. Compatibility with Java implementation
if freq >= 1<<logRange {
freq = (1 << logRange) - 1
}

freq = min(freq, (1<<logRange)-1)
this.xMax = ((_ANS_TOP >> logRange) << 16) * freq
this.cmplFreq = (1 << logRange) - freq

Expand Down Expand Up @@ -538,11 +511,7 @@ func NewANSRangeDecoder(bs kanzi.InputBitStream, args ...uint) (*ANSRangeDecoder
}

if order == 1 {
chkSize <<= 8

if chkSize > _ANS_MAX_CHUNK_SIZE {
chkSize = _ANS_MAX_CHUNK_SIZE
}
chkSize = min(chkSize<<8, _ANS_MAX_CHUNK_SIZE)
}
}

Expand Down Expand Up @@ -602,11 +571,7 @@ func NewANSRangeDecoderWithCtx(bs kanzi.InputBitStream, ctx *map[string]any, arg
}

if order == 1 {
chkSize <<= 8

if chkSize > _ANS_MAX_CHUNK_SIZE {
chkSize = _ANS_MAX_CHUNK_SIZE
}
chkSize = min(chkSize<<8, _ANS_MAX_CHUNK_SIZE)
}
}

Expand Down Expand Up @@ -682,11 +647,7 @@ func (this *ANSRangeDecoder) decodeHeader(frequencies, alphabet []int) (int, err
return alphabetSize, err
}

endj := i + chkSize

if endj > alphabetSize {
endj = alphabetSize
}
endj := min(i+chkSize, alphabetSize)

// Read frequencies
for j := i; j < endj; j++ {
Expand Down Expand Up @@ -755,13 +716,8 @@ func (this *ANSRangeDecoder) Read(block []byte) (int, error) {
var alphabet [256]int

for startChunk < end {
endChunk := startChunk + sizeChunk

if endChunk >= end {
endChunk = end
sizeChunk = end - startChunk
}

endChunk := min(startChunk+sizeChunk, end)
sizeChunk = endChunk - startChunk
alphabetSize, err := this.decodeHeader(this.freqs, alphabet[:])

if err != nil || alphabetSize == 0 {
Expand Down Expand Up @@ -987,10 +943,7 @@ type decSymbol struct {

func (this *decSymbol) reset(cumFreq, freq int, logRange uint) {
// Mirror encoder
if freq >= 1<<logRange {
freq = (1 << logRange) - 1
}

freq = min(freq, (1<<logRange)-1)
this.cumFreq = cumFreq
this.freq = freq
}
2 changes: 2 additions & 0 deletions v2/entropy/AdaptiveProbMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ func NewAdaptiveProbMap(mapType int, n, rate uint) (AdaptiveProbMap, error) {
if mapType == LINEAR_APM {
return newLinearAdaptiveProbMap(n, rate)
}

if mapType == LOGISTIC_APM {
return newLogisticAdaptiveProbMap(n, rate)
}

if mapType == FAST_LOGISTIC_APM {
return newFastLogisticAdaptiveProbMap(n, rate)
}
Expand Down
12 changes: 2 additions & 10 deletions v2/entropy/BinaryEntropyCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,7 @@ func (this *BinaryEntropyEncoder) Write(block []byte) (int, error) {

// Split block into chunks, read bit array from bitstream and decode chunk
for startChunk < end {
chunkSize := length

if startChunk+length >= end {
chunkSize = end - startChunk
}
chunkSize := min(length, end-startChunk)

if len(this.buffer) < (chunkSize + (chunkSize >> 3)) {
this.buffer = make([]byte, chunkSize+(chunkSize>>3))
Expand Down Expand Up @@ -292,11 +288,7 @@ func (this *BinaryEntropyDecoder) Read(block []byte) (int, error) {

// Split block into chunks, read bit array from bitstream and decode chunk
for startChunk < end {
chunkSize := length

if startChunk+length >= end {
chunkSize = end - startChunk
}
chunkSize := min(length, end-startChunk)

if len(this.buffer) < chunkSize+(chunkSize>>3) {
this.buffer = make([]byte, chunkSize+(chunkSize>>3))
Expand Down
25 changes: 4 additions & 21 deletions v2/entropy/HuffmanCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,27 +340,15 @@ func (this *HuffmanEncoder) Write(block []byte) (int, error) {

end := len(block)
startChunk := 0
minBufLen := this.chunkSize + (this.chunkSize >> 3)

if minBufLen > 2*len(block) {
minBufLen = 2 * len(block)
}

if minBufLen < 65536 {
minBufLen = 65536
}
minBufLen := min(this.chunkSize+(this.chunkSize>>3), 2*len(block))
minBufLen = max(minBufLen, 65536)

if len(this.buffer) < minBufLen {
this.buffer = make([]byte, minBufLen)
}

for startChunk < end {
endChunk := startChunk + this.chunkSize

if endChunk > len(block) {
endChunk = len(block)
}

endChunk := min(startChunk+this.chunkSize, len(block))
var freqs [256]int
internal.ComputeHistogram(block[startChunk:endChunk], freqs[:], true, false)
count, err := this.updateFrequencies(freqs[:])
Expand Down Expand Up @@ -522,7 +510,6 @@ func NewHuffmanDecoderWithCtx(bs kanzi.InputBitStream, ctx *map[string]any) (*Hu
this := &HuffmanDecoder{}
this.bitstream = bs
this.isBsVersion3 = bsVersion < 4

this.maxSymbolSize = _HUF_MAX_SYMBOL_SIZE_V4

if this.isBsVersion3 {
Expand Down Expand Up @@ -631,11 +618,7 @@ func (this *HuffmanDecoder) Read(block []byte) (int, error) {
startChunk := 0

for startChunk < end {
endChunk := startChunk + this.chunkSize

if endChunk > end {
endChunk = end
}
endChunk := min(startChunk+this.chunkSize, end)

// For each chunk, read code lengths, rebuild codes, rebuild decoding table
alphabetSize, err := this.readLengths()
Expand Down
26 changes: 4 additions & 22 deletions v2/entropy/RangeCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,7 @@ func (this *RangeEncoder) encodeHeader(alphabet []int, frequencies []int, lr uin
for i := 1; i < alphabetSize; i += chkSize {
max := frequencies[alphabet[i]] - 1
logMax := uint(0)
endj := i + chkSize

if endj > alphabetSize {
endj = alphabetSize
}
endj := min(i+chkSize, alphabetSize)

// Search for max frequency log size in next chunk
for j := i + 1; j < endj; j++ {
Expand Down Expand Up @@ -198,12 +194,7 @@ func (this *RangeEncoder) Write(block []byte) (int, error) {
this.rng = _TOP_RANGE
this.low = 0
lr := this.logRange

endChunk := startChunk + sizeChunk

if endChunk > end {
endChunk = end
}
endChunk := min(startChunk+sizeChunk, end)

// Lower log range if the size of the data block is small
for lr > 8 && 1<<lr > endChunk-startChunk {
Expand Down Expand Up @@ -372,11 +363,7 @@ func (this *RangeDecoder) decodeHeader(frequencies []int) (int, error) {
return alphabetSize, err
}

endj := i + chkSize

if endj > alphabetSize {
endj = alphabetSize
}
endj := min(i+chkSize, alphabetSize)

// Read frequencies
for j := i; j < endj; j++ {
Expand Down Expand Up @@ -435,12 +422,7 @@ func (this *RangeDecoder) Read(block []byte) (int, error) {
sizeChunk := int(this.chunkSize)

for startChunk < end {
endChunk := startChunk + sizeChunk

if endChunk > end {
endChunk = end
}

endChunk := min(startChunk+sizeChunk, end)
alphabetSize, err := this.decodeHeader(this.freqs[:])

if err != nil || alphabetSize == 0 {
Expand Down
17 changes: 5 additions & 12 deletions v2/entropy/TPAQPredictor.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,8 @@ func NewTPAQPredictor(ctx *map[string]any) (*TPAQPredictor, error) {
mixersSize = 1 << 8
}

if bufferSize > rbsz {
bufferSize = rbsz
}

if hashSize > 16*absz {
hashSize = 16 * absz
}
bufferSize = min(bufferSize, rbsz)
hashSize = min(hashSize, 16*absz)
}

mixersSize <<= (2 * extraMem)
Expand Down Expand Up @@ -352,14 +347,12 @@ func (this *TPAQPredictor) Update(bit byte) {
this.binCount += ((this.c4 >> 7) & 1)

// Select Neural Net
lenCtx := int32(0)

if this.matchLen != 0 {
lenCtx = 1
this.mixer = &this.mixers[(this.c4&this.mixersMask)+1]
} else {
this.mixer = &this.mixers[this.c4&this.mixersMask]
}

this.mixer = &this.mixers[(this.c4&this.mixersMask)|lenCtx]

// Add contexts to NN
this.ctx0 = (this.c4 & 0xFF) << 8
this.ctx1 = (this.c4 & 0xFFFF) << 8
Expand Down
2 changes: 1 addition & 1 deletion v2/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/flanglet/kanzi-go/v2

go 1.20
go 1.21
2 changes: 1 addition & 1 deletion v2/transform/AliasCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (this *AliasCodec) Forward(src, dst []byte) (uint, uint, error) {
dstIdx = 1
j := 0

for i := range freqs0 {
for i := range &freqs0 {
if freqs0[i] != 0 {
dst[dstIdx] = byte(i)
dstIdx++
Expand Down
Loading

0 comments on commit 6201f25

Please sign in to comment.