Skip to content

Commit

Permalink
Performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
flanglet committed Jan 7, 2024
1 parent f6df2ca commit 6a2f8a2
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions v2/entropy/CMPredictor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ type CMPredictor struct {
c1 byte
c2 byte
ctx int32
idx int
runMask int32
counter1 [256][]int32
counter2 [512][]int32
p int
idx int
isBsVersion3 bool
}

Expand Down Expand Up @@ -63,9 +62,6 @@ func NewCMPredictor(ctx *map[string]any) (*CMPredictor, error) {
}
}

pc1 := this.counter1[this.ctx]
this.p = int(13*(pc1[256]+pc1[this.c1])+6*pc1[this.c2]) >> 5
this.idx = this.p >> 12
bsVersion := uint(4)

if ctx != nil {
Expand All @@ -80,8 +76,8 @@ func NewCMPredictor(ctx *map[string]any) (*CMPredictor, error) {

// Update updates the probability model based on the internal bit counters
func (this *CMPredictor) Update(bit byte) {
pc1 := this.counter1[this.ctx]
pc2 := this.counter2[this.ctx|this.runMask]
pc1 := this.counter1[this.ctx]

if bit == 0 {
pc1[256] -= (pc1[256] >> _CM_FAST_RATE)
Expand All @@ -108,24 +104,23 @@ func (this *CMPredictor) Update(bit byte) {
this.runMask = 0
}
}

pc1 = this.counter1[this.ctx]
this.p = int(13*(pc1[256]+pc1[this.c1])+6*pc1[this.c2]) >> 5
this.idx = this.p >> 12
}

// Get returns the value representing the probability of the next bit being 1
// in the [0..4095] range. The probability is computed from the internal
// bit counters.
func (this *CMPredictor) Get() int {
pc2 := this.counter2[this.ctx|this.runMask]
pc1 := this.counter1[this.ctx]
p := int(13*(pc1[256]+pc1[this.c1])+6*pc1[this.c2]) >> 5
this.idx = p >> 12
x2 := int(pc2[this.idx+1])
x1 := int(pc2[this.idx])

if this.isBsVersion3 == true {
ssep := x1 + (((x2 - x1) * (this.p & 4095)) >> 12)
return (this.p + 3*ssep + 32) >> 6 // rescale to [0..4095]
ssep := x1 + (((x2 - x1) * (p & 4095)) >> 12)
return (p + 3*ssep + 32) >> 6 // rescale to [0..4095]
}

return (this.p + this.p + 3*(x1+x2) + 64) >> 7 // rescale to [0..4095]
return (p + p + 3*(x1+x2) + 64) >> 7 // rescale to [0..4095]
}

0 comments on commit 6a2f8a2

Please sign in to comment.