12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241 |
- // Copyright 2019+ Klaus Post. All rights reserved.
- // License information can be found in the LICENSE file.
- // Based on work by Yann Collet, released under BSD License.
- package zstd
- import "fmt"
- const (
- betterLongTableBits = 19 // Bits used in the long match table
- betterLongTableSize = 1 << betterLongTableBits // Size of the table
- betterLongLen = 8 // Bytes used for table hash
- // Note: Increasing the short table bits or making the hash shorter
- // can actually lead to compression degradation since it will 'steal' more from the
- // long match table and match offsets are quite big.
- // This greatly depends on the type of input.
- betterShortTableBits = 13 // Bits used in the short match table
- betterShortTableSize = 1 << betterShortTableBits // Size of the table
- betterShortLen = 5 // Bytes used for table hash
- betterLongTableShardCnt = 1 << (betterLongTableBits - dictShardBits) // Number of shards in the table
- betterLongTableShardSize = betterLongTableSize / betterLongTableShardCnt // Size of an individual shard
- betterShortTableShardCnt = 1 << (betterShortTableBits - dictShardBits) // Number of shards in the table
- betterShortTableShardSize = betterShortTableSize / betterShortTableShardCnt // Size of an individual shard
- )
- type prevEntry struct {
- offset int32
- prev int32
- }
- // betterFastEncoder uses 2 tables, one for short matches (5 bytes) and one for long matches.
- // The long match table contains the previous entry with the same hash,
- // effectively making it a "chain" of length 2.
- // When we find a long match we choose between the two values and select the longest.
- // When we find a short match, after checking the long, we check if we can find a long at n+1
- // and that it is longer (lazy matching).
- type betterFastEncoder struct {
- fastBase
- table [betterShortTableSize]tableEntry
- longTable [betterLongTableSize]prevEntry
- }
- type betterFastEncoderDict struct {
- betterFastEncoder
- dictTable []tableEntry
- dictLongTable []prevEntry
- shortTableShardDirty [betterShortTableShardCnt]bool
- longTableShardDirty [betterLongTableShardCnt]bool
- allDirty bool
- }
- // Encode improves compression...
- func (e *betterFastEncoder) Encode(blk *blockEnc, src []byte) {
- const (
- // Input margin is the number of bytes we read (8)
- // and the maximum we will read ahead (2)
- inputMargin = 8 + 2
- minNonLiteralBlockSize = 16
- )
- // Protect against e.cur wraparound.
- for e.cur >= e.bufferReset-int32(len(e.hist)) {
- if len(e.hist) == 0 {
- e.table = [betterShortTableSize]tableEntry{}
- e.longTable = [betterLongTableSize]prevEntry{}
- e.cur = e.maxMatchOff
- break
- }
- // Shift down everything in the table that isn't already too far away.
- minOff := e.cur + int32(len(e.hist)) - e.maxMatchOff
- for i := range e.table[:] {
- v := e.table[i].offset
- if v < minOff {
- v = 0
- } else {
- v = v - e.cur + e.maxMatchOff
- }
- e.table[i].offset = v
- }
- for i := range e.longTable[:] {
- v := e.longTable[i].offset
- v2 := e.longTable[i].prev
- if v < minOff {
- v = 0
- v2 = 0
- } else {
- v = v - e.cur + e.maxMatchOff
- if v2 < minOff {
- v2 = 0
- } else {
- v2 = v2 - e.cur + e.maxMatchOff
- }
- }
- e.longTable[i] = prevEntry{
- offset: v,
- prev: v2,
- }
- }
- e.cur = e.maxMatchOff
- break
- }
- s := e.addBlock(src)
- blk.size = len(src)
- if len(src) < minNonLiteralBlockSize {
- blk.extraLits = len(src)
- blk.literals = blk.literals[:len(src)]
- copy(blk.literals, src)
- return
- }
- // Override src
- src = e.hist
- sLimit := int32(len(src)) - inputMargin
- // stepSize is the number of bytes to skip on every main loop iteration.
- // It should be >= 1.
- const stepSize = 1
- const kSearchStrength = 9
- // nextEmit is where in src the next emitLiteral should start from.
- nextEmit := s
- cv := load6432(src, s)
- // Relative offsets
- offset1 := int32(blk.recentOffsets[0])
- offset2 := int32(blk.recentOffsets[1])
- addLiterals := func(s *seq, until int32) {
- if until == nextEmit {
- return
- }
- blk.literals = append(blk.literals, src[nextEmit:until]...)
- s.litLen = uint32(until - nextEmit)
- }
- if debugEncoder {
- println("recent offsets:", blk.recentOffsets)
- }
- encodeLoop:
- for {
- var t int32
- // We allow the encoder to optionally turn off repeat offsets across blocks
- canRepeat := len(blk.sequences) > 2
- var matched, index0 int32
- for {
- if debugAsserts && canRepeat && offset1 == 0 {
- panic("offset0 was 0")
- }
- nextHashL := hashLen(cv, betterLongTableBits, betterLongLen)
- nextHashS := hashLen(cv, betterShortTableBits, betterShortLen)
- candidateL := e.longTable[nextHashL]
- candidateS := e.table[nextHashS]
- const repOff = 1
- repIndex := s - offset1 + repOff
- off := s + e.cur
- e.longTable[nextHashL] = prevEntry{offset: off, prev: candidateL.offset}
- e.table[nextHashS] = tableEntry{offset: off, val: uint32(cv)}
- index0 = s + 1
- if canRepeat {
- if repIndex >= 0 && load3232(src, repIndex) == uint32(cv>>(repOff*8)) {
- // Consider history as well.
- var seq seq
- lenght := 4 + e.matchlen(s+4+repOff, repIndex+4, src)
- seq.matchLen = uint32(lenght - zstdMinMatch)
- // We might be able to match backwards.
- // Extend as long as we can.
- start := s + repOff
- // We end the search early, so we don't risk 0 literals
- // and have to do special offset treatment.
- startLimit := nextEmit + 1
- tMin := s - e.maxMatchOff
- if tMin < 0 {
- tMin = 0
- }
- for repIndex > tMin && start > startLimit && src[repIndex-1] == src[start-1] && seq.matchLen < maxMatchLength-zstdMinMatch-1 {
- repIndex--
- start--
- seq.matchLen++
- }
- addLiterals(&seq, start)
- // rep 0
- seq.offset = 1
- if debugSequences {
- println("repeat sequence", seq, "next s:", s)
- }
- blk.sequences = append(blk.sequences, seq)
- // Index match start+1 (long) -> s - 1
- index0 := s + repOff
- s += lenght + repOff
- nextEmit = s
- if s >= sLimit {
- if debugEncoder {
- println("repeat ended", s, lenght)
- }
- break encodeLoop
- }
- // Index skipped...
- for index0 < s-1 {
- cv0 := load6432(src, index0)
- cv1 := cv0 >> 8
- h0 := hashLen(cv0, betterLongTableBits, betterLongLen)
- off := index0 + e.cur
- e.longTable[h0] = prevEntry{offset: off, prev: e.longTable[h0].offset}
- e.table[hashLen(cv1, betterShortTableBits, betterShortLen)] = tableEntry{offset: off + 1, val: uint32(cv1)}
- index0 += 2
- }
- cv = load6432(src, s)
- continue
- }
- const repOff2 = 1
- // We deviate from the reference encoder and also check offset 2.
- // Still slower and not much better, so disabled.
- // repIndex = s - offset2 + repOff2
- if false && repIndex >= 0 && load6432(src, repIndex) == load6432(src, s+repOff) {
- // Consider history as well.
- var seq seq
- lenght := 8 + e.matchlen(s+8+repOff2, repIndex+8, src)
- seq.matchLen = uint32(lenght - zstdMinMatch)
- // We might be able to match backwards.
- // Extend as long as we can.
- start := s + repOff2
- // We end the search early, so we don't risk 0 literals
- // and have to do special offset treatment.
- startLimit := nextEmit + 1
- tMin := s - e.maxMatchOff
- if tMin < 0 {
- tMin = 0
- }
- for repIndex > tMin && start > startLimit && src[repIndex-1] == src[start-1] && seq.matchLen < maxMatchLength-zstdMinMatch-1 {
- repIndex--
- start--
- seq.matchLen++
- }
- addLiterals(&seq, start)
- // rep 2
- seq.offset = 2
- if debugSequences {
- println("repeat sequence 2", seq, "next s:", s)
- }
- blk.sequences = append(blk.sequences, seq)
- s += lenght + repOff2
- nextEmit = s
- if s >= sLimit {
- if debugEncoder {
- println("repeat ended", s, lenght)
- }
- break encodeLoop
- }
- // Index skipped...
- for index0 < s-1 {
- cv0 := load6432(src, index0)
- cv1 := cv0 >> 8
- h0 := hashLen(cv0, betterLongTableBits, betterLongLen)
- off := index0 + e.cur
- e.longTable[h0] = prevEntry{offset: off, prev: e.longTable[h0].offset}
- e.table[hashLen(cv1, betterShortTableBits, betterShortLen)] = tableEntry{offset: off + 1, val: uint32(cv1)}
- index0 += 2
- }
- cv = load6432(src, s)
- // Swap offsets
- offset1, offset2 = offset2, offset1
- continue
- }
- }
- // Find the offsets of our two matches.
- coffsetL := candidateL.offset - e.cur
- coffsetLP := candidateL.prev - e.cur
- // Check if we have a long match.
- if s-coffsetL < e.maxMatchOff && cv == load6432(src, coffsetL) {
- // Found a long match, at least 8 bytes.
- matched = e.matchlen(s+8, coffsetL+8, src) + 8
- t = coffsetL
- if debugAsserts && s <= t {
- panic(fmt.Sprintf("s (%d) <= t (%d)", s, t))
- }
- if debugAsserts && s-t > e.maxMatchOff {
- panic("s - t >e.maxMatchOff")
- }
- if debugMatches {
- println("long match")
- }
- if s-coffsetLP < e.maxMatchOff && cv == load6432(src, coffsetLP) {
- // Found a long match, at least 8 bytes.
- prevMatch := e.matchlen(s+8, coffsetLP+8, src) + 8
- if prevMatch > matched {
- matched = prevMatch
- t = coffsetLP
- }
- if debugAsserts && s <= t {
- panic(fmt.Sprintf("s (%d) <= t (%d)", s, t))
- }
- if debugAsserts && s-t > e.maxMatchOff {
- panic("s - t >e.maxMatchOff")
- }
- if debugMatches {
- println("long match")
- }
- }
- break
- }
- // Check if we have a long match on prev.
- if s-coffsetLP < e.maxMatchOff && cv == load6432(src, coffsetLP) {
- // Found a long match, at least 8 bytes.
- matched = e.matchlen(s+8, coffsetLP+8, src) + 8
- t = coffsetLP
- if debugAsserts && s <= t {
- panic(fmt.Sprintf("s (%d) <= t (%d)", s, t))
- }
- if debugAsserts && s-t > e.maxMatchOff {
- panic("s - t >e.maxMatchOff")
- }
- if debugMatches {
- println("long match")
- }
- break
- }
- coffsetS := candidateS.offset - e.cur
- // Check if we have a short match.
- if s-coffsetS < e.maxMatchOff && uint32(cv) == candidateS.val {
- // found a regular match
- matched = e.matchlen(s+4, coffsetS+4, src) + 4
- // See if we can find a long match at s+1
- const checkAt = 1
- cv := load6432(src, s+checkAt)
- nextHashL = hashLen(cv, betterLongTableBits, betterLongLen)
- candidateL = e.longTable[nextHashL]
- coffsetL = candidateL.offset - e.cur
- // We can store it, since we have at least a 4 byte match.
- e.longTable[nextHashL] = prevEntry{offset: s + checkAt + e.cur, prev: candidateL.offset}
- if s-coffsetL < e.maxMatchOff && cv == load6432(src, coffsetL) {
- // Found a long match, at least 8 bytes.
- matchedNext := e.matchlen(s+8+checkAt, coffsetL+8, src) + 8
- if matchedNext > matched {
- t = coffsetL
- s += checkAt
- matched = matchedNext
- if debugMatches {
- println("long match (after short)")
- }
- break
- }
- }
- // Check prev long...
- coffsetL = candidateL.prev - e.cur
- if s-coffsetL < e.maxMatchOff && cv == load6432(src, coffsetL) {
- // Found a long match, at least 8 bytes.
- matchedNext := e.matchlen(s+8+checkAt, coffsetL+8, src) + 8
- if matchedNext > matched {
- t = coffsetL
- s += checkAt
- matched = matchedNext
- if debugMatches {
- println("prev long match (after short)")
- }
- break
- }
- }
- t = coffsetS
- if debugAsserts && s <= t {
- panic(fmt.Sprintf("s (%d) <= t (%d)", s, t))
- }
- if debugAsserts && s-t > e.maxMatchOff {
- panic("s - t >e.maxMatchOff")
- }
- if debugAsserts && t < 0 {
- panic("t<0")
- }
- if debugMatches {
- println("short match")
- }
- break
- }
- // No match found, move forward in input.
- s += stepSize + ((s - nextEmit) >> (kSearchStrength - 1))
- if s >= sLimit {
- break encodeLoop
- }
- cv = load6432(src, s)
- }
- // Try to find a better match by searching for a long match at the end of the current best match
- if s+matched < sLimit {
- // Allow some bytes at the beginning to mismatch.
- // Sweet spot is around 3 bytes, but depends on input.
- // The skipped bytes are tested in Extend backwards,
- // and still picked up as part of the match if they do.
- const skipBeginning = 3
- nextHashL := hashLen(load6432(src, s+matched), betterLongTableBits, betterLongLen)
- s2 := s + skipBeginning
- cv := load3232(src, s2)
- candidateL := e.longTable[nextHashL]
- coffsetL := candidateL.offset - e.cur - matched + skipBeginning
- if coffsetL >= 0 && coffsetL < s2 && s2-coffsetL < e.maxMatchOff && cv == load3232(src, coffsetL) {
- // Found a long match, at least 4 bytes.
- matchedNext := e.matchlen(s2+4, coffsetL+4, src) + 4
- if matchedNext > matched {
- t = coffsetL
- s = s2
- matched = matchedNext
- if debugMatches {
- println("long match at end-of-match")
- }
- }
- }
- // Check prev long...
- if true {
- coffsetL = candidateL.prev - e.cur - matched + skipBeginning
- if coffsetL >= 0 && coffsetL < s2 && s2-coffsetL < e.maxMatchOff && cv == load3232(src, coffsetL) {
- // Found a long match, at least 4 bytes.
- matchedNext := e.matchlen(s2+4, coffsetL+4, src) + 4
- if matchedNext > matched {
- t = coffsetL
- s = s2
- matched = matchedNext
- if debugMatches {
- println("prev long match at end-of-match")
- }
- }
- }
- }
- }
- // A match has been found. Update recent offsets.
- offset2 = offset1
- offset1 = s - t
- if debugAsserts && s <= t {
- panic(fmt.Sprintf("s (%d) <= t (%d)", s, t))
- }
- if debugAsserts && canRepeat && int(offset1) > len(src) {
- panic("invalid offset")
- }
- // Extend the n-byte match as long as possible.
- l := matched
- // Extend backwards
- tMin := s - e.maxMatchOff
- if tMin < 0 {
- tMin = 0
- }
- for t > tMin && s > nextEmit && src[t-1] == src[s-1] && l < maxMatchLength {
- s--
- t--
- l++
- }
- // Write our sequence
- var seq seq
- seq.litLen = uint32(s - nextEmit)
- seq.matchLen = uint32(l - zstdMinMatch)
- if seq.litLen > 0 {
- blk.literals = append(blk.literals, src[nextEmit:s]...)
- }
- seq.offset = uint32(s-t) + 3
- s += l
- if debugSequences {
- println("sequence", seq, "next s:", s)
- }
- blk.sequences = append(blk.sequences, seq)
- nextEmit = s
- if s >= sLimit {
- break encodeLoop
- }
- // Index match start+1 (long) -> s - 1
- off := index0 + e.cur
- for index0 < s-1 {
- cv0 := load6432(src, index0)
- cv1 := cv0 >> 8
- h0 := hashLen(cv0, betterLongTableBits, betterLongLen)
- e.longTable[h0] = prevEntry{offset: off, prev: e.longTable[h0].offset}
- e.table[hashLen(cv1, betterShortTableBits, betterShortLen)] = tableEntry{offset: off + 1, val: uint32(cv1)}
- index0 += 2
- off += 2
- }
- cv = load6432(src, s)
- if !canRepeat {
- continue
- }
- // Check offset 2
- for {
- o2 := s - offset2
- if load3232(src, o2) != uint32(cv) {
- // Do regular search
- break
- }
- // Store this, since we have it.
- nextHashL := hashLen(cv, betterLongTableBits, betterLongLen)
- nextHashS := hashLen(cv, betterShortTableBits, betterShortLen)
- // We have at least 4 byte match.
- // No need to check backwards. We come straight from a match
- l := 4 + e.matchlen(s+4, o2+4, src)
- e.longTable[nextHashL] = prevEntry{offset: s + e.cur, prev: e.longTable[nextHashL].offset}
- e.table[nextHashS] = tableEntry{offset: s + e.cur, val: uint32(cv)}
- seq.matchLen = uint32(l) - zstdMinMatch
- seq.litLen = 0
- // Since litlen is always 0, this is offset 1.
- seq.offset = 1
- s += l
- nextEmit = s
- if debugSequences {
- println("sequence", seq, "next s:", s)
- }
- blk.sequences = append(blk.sequences, seq)
- // Swap offset 1 and 2.
- offset1, offset2 = offset2, offset1
- if s >= sLimit {
- // Finished
- break encodeLoop
- }
- cv = load6432(src, s)
- }
- }
- if int(nextEmit) < len(src) {
- blk.literals = append(blk.literals, src[nextEmit:]...)
- blk.extraLits = len(src) - int(nextEmit)
- }
- blk.recentOffsets[0] = uint32(offset1)
- blk.recentOffsets[1] = uint32(offset2)
- if debugEncoder {
- println("returning, recent offsets:", blk.recentOffsets, "extra literals:", blk.extraLits)
- }
- }
- // EncodeNoHist will encode a block with no history and no following blocks.
- // Most notable difference is that src will not be copied for history and
- // we do not need to check for max match length.
- func (e *betterFastEncoder) EncodeNoHist(blk *blockEnc, src []byte) {
- e.ensureHist(len(src))
- e.Encode(blk, src)
- }
- // Encode improves compression...
- func (e *betterFastEncoderDict) Encode(blk *blockEnc, src []byte) {
- const (
- // Input margin is the number of bytes we read (8)
- // and the maximum we will read ahead (2)
- inputMargin = 8 + 2
- minNonLiteralBlockSize = 16
- )
- // Protect against e.cur wraparound.
- for e.cur >= e.bufferReset-int32(len(e.hist)) {
- if len(e.hist) == 0 {
- for i := range e.table[:] {
- e.table[i] = tableEntry{}
- }
- for i := range e.longTable[:] {
- e.longTable[i] = prevEntry{}
- }
- e.cur = e.maxMatchOff
- e.allDirty = true
- break
- }
- // Shift down everything in the table that isn't already too far away.
- minOff := e.cur + int32(len(e.hist)) - e.maxMatchOff
- for i := range e.table[:] {
- v := e.table[i].offset
- if v < minOff {
- v = 0
- } else {
- v = v - e.cur + e.maxMatchOff
- }
- e.table[i].offset = v
- }
- for i := range e.longTable[:] {
- v := e.longTable[i].offset
- v2 := e.longTable[i].prev
- if v < minOff {
- v = 0
- v2 = 0
- } else {
- v = v - e.cur + e.maxMatchOff
- if v2 < minOff {
- v2 = 0
- } else {
- v2 = v2 - e.cur + e.maxMatchOff
- }
- }
- e.longTable[i] = prevEntry{
- offset: v,
- prev: v2,
- }
- }
- e.allDirty = true
- e.cur = e.maxMatchOff
- break
- }
- s := e.addBlock(src)
- blk.size = len(src)
- if len(src) < minNonLiteralBlockSize {
- blk.extraLits = len(src)
- blk.literals = blk.literals[:len(src)]
- copy(blk.literals, src)
- return
- }
- // Override src
- src = e.hist
- sLimit := int32(len(src)) - inputMargin
- // stepSize is the number of bytes to skip on every main loop iteration.
- // It should be >= 1.
- const stepSize = 1
- const kSearchStrength = 9
- // nextEmit is where in src the next emitLiteral should start from.
- nextEmit := s
- cv := load6432(src, s)
- // Relative offsets
- offset1 := int32(blk.recentOffsets[0])
- offset2 := int32(blk.recentOffsets[1])
- addLiterals := func(s *seq, until int32) {
- if until == nextEmit {
- return
- }
- blk.literals = append(blk.literals, src[nextEmit:until]...)
- s.litLen = uint32(until - nextEmit)
- }
- if debugEncoder {
- println("recent offsets:", blk.recentOffsets)
- }
- encodeLoop:
- for {
- var t int32
- // We allow the encoder to optionally turn off repeat offsets across blocks
- canRepeat := len(blk.sequences) > 2
- var matched, index0 int32
- for {
- if debugAsserts && canRepeat && offset1 == 0 {
- panic("offset0 was 0")
- }
- nextHashL := hashLen(cv, betterLongTableBits, betterLongLen)
- nextHashS := hashLen(cv, betterShortTableBits, betterShortLen)
- candidateL := e.longTable[nextHashL]
- candidateS := e.table[nextHashS]
- const repOff = 1
- repIndex := s - offset1 + repOff
- off := s + e.cur
- e.longTable[nextHashL] = prevEntry{offset: off, prev: candidateL.offset}
- e.markLongShardDirty(nextHashL)
- e.table[nextHashS] = tableEntry{offset: off, val: uint32(cv)}
- e.markShortShardDirty(nextHashS)
- index0 = s + 1
- if canRepeat {
- if repIndex >= 0 && load3232(src, repIndex) == uint32(cv>>(repOff*8)) {
- // Consider history as well.
- var seq seq
- lenght := 4 + e.matchlen(s+4+repOff, repIndex+4, src)
- seq.matchLen = uint32(lenght - zstdMinMatch)
- // We might be able to match backwards.
- // Extend as long as we can.
- start := s + repOff
- // We end the search early, so we don't risk 0 literals
- // and have to do special offset treatment.
- startLimit := nextEmit + 1
- tMin := s - e.maxMatchOff
- if tMin < 0 {
- tMin = 0
- }
- for repIndex > tMin && start > startLimit && src[repIndex-1] == src[start-1] && seq.matchLen < maxMatchLength-zstdMinMatch-1 {
- repIndex--
- start--
- seq.matchLen++
- }
- addLiterals(&seq, start)
- // rep 0
- seq.offset = 1
- if debugSequences {
- println("repeat sequence", seq, "next s:", s)
- }
- blk.sequences = append(blk.sequences, seq)
- // Index match start+1 (long) -> s - 1
- s += lenght + repOff
- nextEmit = s
- if s >= sLimit {
- if debugEncoder {
- println("repeat ended", s, lenght)
- }
- break encodeLoop
- }
- // Index skipped...
- for index0 < s-1 {
- cv0 := load6432(src, index0)
- cv1 := cv0 >> 8
- h0 := hashLen(cv0, betterLongTableBits, betterLongLen)
- off := index0 + e.cur
- e.longTable[h0] = prevEntry{offset: off, prev: e.longTable[h0].offset}
- e.markLongShardDirty(h0)
- h1 := hashLen(cv1, betterShortTableBits, betterShortLen)
- e.table[h1] = tableEntry{offset: off + 1, val: uint32(cv1)}
- e.markShortShardDirty(h1)
- index0 += 2
- }
- cv = load6432(src, s)
- continue
- }
- const repOff2 = 1
- // We deviate from the reference encoder and also check offset 2.
- // Still slower and not much better, so disabled.
- // repIndex = s - offset2 + repOff2
- if false && repIndex >= 0 && load6432(src, repIndex) == load6432(src, s+repOff) {
- // Consider history as well.
- var seq seq
- lenght := 8 + e.matchlen(s+8+repOff2, repIndex+8, src)
- seq.matchLen = uint32(lenght - zstdMinMatch)
- // We might be able to match backwards.
- // Extend as long as we can.
- start := s + repOff2
- // We end the search early, so we don't risk 0 literals
- // and have to do special offset treatment.
- startLimit := nextEmit + 1
- tMin := s - e.maxMatchOff
- if tMin < 0 {
- tMin = 0
- }
- for repIndex > tMin && start > startLimit && src[repIndex-1] == src[start-1] && seq.matchLen < maxMatchLength-zstdMinMatch-1 {
- repIndex--
- start--
- seq.matchLen++
- }
- addLiterals(&seq, start)
- // rep 2
- seq.offset = 2
- if debugSequences {
- println("repeat sequence 2", seq, "next s:", s)
- }
- blk.sequences = append(blk.sequences, seq)
- s += lenght + repOff2
- nextEmit = s
- if s >= sLimit {
- if debugEncoder {
- println("repeat ended", s, lenght)
- }
- break encodeLoop
- }
- // Index skipped...
- for index0 < s-1 {
- cv0 := load6432(src, index0)
- cv1 := cv0 >> 8
- h0 := hashLen(cv0, betterLongTableBits, betterLongLen)
- off := index0 + e.cur
- e.longTable[h0] = prevEntry{offset: off, prev: e.longTable[h0].offset}
- e.markLongShardDirty(h0)
- h1 := hashLen(cv1, betterShortTableBits, betterShortLen)
- e.table[h1] = tableEntry{offset: off + 1, val: uint32(cv1)}
- e.markShortShardDirty(h1)
- index0 += 2
- }
- cv = load6432(src, s)
- // Swap offsets
- offset1, offset2 = offset2, offset1
- continue
- }
- }
- // Find the offsets of our two matches.
- coffsetL := candidateL.offset - e.cur
- coffsetLP := candidateL.prev - e.cur
- // Check if we have a long match.
- if s-coffsetL < e.maxMatchOff && cv == load6432(src, coffsetL) {
- // Found a long match, at least 8 bytes.
- matched = e.matchlen(s+8, coffsetL+8, src) + 8
- t = coffsetL
- if debugAsserts && s <= t {
- panic(fmt.Sprintf("s (%d) <= t (%d)", s, t))
- }
- if debugAsserts && s-t > e.maxMatchOff {
- panic("s - t >e.maxMatchOff")
- }
- if debugMatches {
- println("long match")
- }
- if s-coffsetLP < e.maxMatchOff && cv == load6432(src, coffsetLP) {
- // Found a long match, at least 8 bytes.
- prevMatch := e.matchlen(s+8, coffsetLP+8, src) + 8
- if prevMatch > matched {
- matched = prevMatch
- t = coffsetLP
- }
- if debugAsserts && s <= t {
- panic(fmt.Sprintf("s (%d) <= t (%d)", s, t))
- }
- if debugAsserts && s-t > e.maxMatchOff {
- panic("s - t >e.maxMatchOff")
- }
- if debugMatches {
- println("long match")
- }
- }
- break
- }
- // Check if we have a long match on prev.
- if s-coffsetLP < e.maxMatchOff && cv == load6432(src, coffsetLP) {
- // Found a long match, at least 8 bytes.
- matched = e.matchlen(s+8, coffsetLP+8, src) + 8
- t = coffsetLP
- if debugAsserts && s <= t {
- panic(fmt.Sprintf("s (%d) <= t (%d)", s, t))
- }
- if debugAsserts && s-t > e.maxMatchOff {
- panic("s - t >e.maxMatchOff")
- }
- if debugMatches {
- println("long match")
- }
- break
- }
- coffsetS := candidateS.offset - e.cur
- // Check if we have a short match.
- if s-coffsetS < e.maxMatchOff && uint32(cv) == candidateS.val {
- // found a regular match
- matched = e.matchlen(s+4, coffsetS+4, src) + 4
- // See if we can find a long match at s+1
- const checkAt = 1
- cv := load6432(src, s+checkAt)
- nextHashL = hashLen(cv, betterLongTableBits, betterLongLen)
- candidateL = e.longTable[nextHashL]
- coffsetL = candidateL.offset - e.cur
- // We can store it, since we have at least a 4 byte match.
- e.longTable[nextHashL] = prevEntry{offset: s + checkAt + e.cur, prev: candidateL.offset}
- e.markLongShardDirty(nextHashL)
- if s-coffsetL < e.maxMatchOff && cv == load6432(src, coffsetL) {
- // Found a long match, at least 8 bytes.
- matchedNext := e.matchlen(s+8+checkAt, coffsetL+8, src) + 8
- if matchedNext > matched {
- t = coffsetL
- s += checkAt
- matched = matchedNext
- if debugMatches {
- println("long match (after short)")
- }
- break
- }
- }
- // Check prev long...
- coffsetL = candidateL.prev - e.cur
- if s-coffsetL < e.maxMatchOff && cv == load6432(src, coffsetL) {
- // Found a long match, at least 8 bytes.
- matchedNext := e.matchlen(s+8+checkAt, coffsetL+8, src) + 8
- if matchedNext > matched {
- t = coffsetL
- s += checkAt
- matched = matchedNext
- if debugMatches {
- println("prev long match (after short)")
- }
- break
- }
- }
- t = coffsetS
- if debugAsserts && s <= t {
- panic(fmt.Sprintf("s (%d) <= t (%d)", s, t))
- }
- if debugAsserts && s-t > e.maxMatchOff {
- panic("s - t >e.maxMatchOff")
- }
- if debugAsserts && t < 0 {
- panic("t<0")
- }
- if debugMatches {
- println("short match")
- }
- break
- }
- // No match found, move forward in input.
- s += stepSize + ((s - nextEmit) >> (kSearchStrength - 1))
- if s >= sLimit {
- break encodeLoop
- }
- cv = load6432(src, s)
- }
- // Try to find a better match by searching for a long match at the end of the current best match
- if s+matched < sLimit {
- nextHashL := hashLen(load6432(src, s+matched), betterLongTableBits, betterLongLen)
- cv := load3232(src, s)
- candidateL := e.longTable[nextHashL]
- coffsetL := candidateL.offset - e.cur - matched
- if coffsetL >= 0 && coffsetL < s && s-coffsetL < e.maxMatchOff && cv == load3232(src, coffsetL) {
- // Found a long match, at least 4 bytes.
- matchedNext := e.matchlen(s+4, coffsetL+4, src) + 4
- if matchedNext > matched {
- t = coffsetL
- matched = matchedNext
- if debugMatches {
- println("long match at end-of-match")
- }
- }
- }
- // Check prev long...
- if true {
- coffsetL = candidateL.prev - e.cur - matched
- if coffsetL >= 0 && coffsetL < s && s-coffsetL < e.maxMatchOff && cv == load3232(src, coffsetL) {
- // Found a long match, at least 4 bytes.
- matchedNext := e.matchlen(s+4, coffsetL+4, src) + 4
- if matchedNext > matched {
- t = coffsetL
- matched = matchedNext
- if debugMatches {
- println("prev long match at end-of-match")
- }
- }
- }
- }
- }
- // A match has been found. Update recent offsets.
- offset2 = offset1
- offset1 = s - t
- if debugAsserts && s <= t {
- panic(fmt.Sprintf("s (%d) <= t (%d)", s, t))
- }
- if debugAsserts && canRepeat && int(offset1) > len(src) {
- panic("invalid offset")
- }
- // Extend the n-byte match as long as possible.
- l := matched
- // Extend backwards
- tMin := s - e.maxMatchOff
- if tMin < 0 {
- tMin = 0
- }
- for t > tMin && s > nextEmit && src[t-1] == src[s-1] && l < maxMatchLength {
- s--
- t--
- l++
- }
- // Write our sequence
- var seq seq
- seq.litLen = uint32(s - nextEmit)
- seq.matchLen = uint32(l - zstdMinMatch)
- if seq.litLen > 0 {
- blk.literals = append(blk.literals, src[nextEmit:s]...)
- }
- seq.offset = uint32(s-t) + 3
- s += l
- if debugSequences {
- println("sequence", seq, "next s:", s)
- }
- blk.sequences = append(blk.sequences, seq)
- nextEmit = s
- if s >= sLimit {
- break encodeLoop
- }
- // Index match start+1 (long) -> s - 1
- off := index0 + e.cur
- for index0 < s-1 {
- cv0 := load6432(src, index0)
- cv1 := cv0 >> 8
- h0 := hashLen(cv0, betterLongTableBits, betterLongLen)
- e.longTable[h0] = prevEntry{offset: off, prev: e.longTable[h0].offset}
- e.markLongShardDirty(h0)
- h1 := hashLen(cv1, betterShortTableBits, betterShortLen)
- e.table[h1] = tableEntry{offset: off + 1, val: uint32(cv1)}
- e.markShortShardDirty(h1)
- index0 += 2
- off += 2
- }
- cv = load6432(src, s)
- if !canRepeat {
- continue
- }
- // Check offset 2
- for {
- o2 := s - offset2
- if load3232(src, o2) != uint32(cv) {
- // Do regular search
- break
- }
- // Store this, since we have it.
- nextHashL := hashLen(cv, betterLongTableBits, betterLongLen)
- nextHashS := hashLen(cv, betterShortTableBits, betterShortLen)
- // We have at least 4 byte match.
- // No need to check backwards. We come straight from a match
- l := 4 + e.matchlen(s+4, o2+4, src)
- e.longTable[nextHashL] = prevEntry{offset: s + e.cur, prev: e.longTable[nextHashL].offset}
- e.markLongShardDirty(nextHashL)
- e.table[nextHashS] = tableEntry{offset: s + e.cur, val: uint32(cv)}
- e.markShortShardDirty(nextHashS)
- seq.matchLen = uint32(l) - zstdMinMatch
- seq.litLen = 0
- // Since litlen is always 0, this is offset 1.
- seq.offset = 1
- s += l
- nextEmit = s
- if debugSequences {
- println("sequence", seq, "next s:", s)
- }
- blk.sequences = append(blk.sequences, seq)
- // Swap offset 1 and 2.
- offset1, offset2 = offset2, offset1
- if s >= sLimit {
- // Finished
- break encodeLoop
- }
- cv = load6432(src, s)
- }
- }
- if int(nextEmit) < len(src) {
- blk.literals = append(blk.literals, src[nextEmit:]...)
- blk.extraLits = len(src) - int(nextEmit)
- }
- blk.recentOffsets[0] = uint32(offset1)
- blk.recentOffsets[1] = uint32(offset2)
- if debugEncoder {
- println("returning, recent offsets:", blk.recentOffsets, "extra literals:", blk.extraLits)
- }
- }
- // ResetDict will reset and set a dictionary if not nil
- func (e *betterFastEncoder) Reset(d *dict, singleBlock bool) {
- e.resetBase(d, singleBlock)
- if d != nil {
- panic("betterFastEncoder: Reset with dict")
- }
- }
- // ResetDict will reset and set a dictionary if not nil
- func (e *betterFastEncoderDict) Reset(d *dict, singleBlock bool) {
- e.resetBase(d, singleBlock)
- if d == nil {
- return
- }
- // Init or copy dict table
- if len(e.dictTable) != len(e.table) || d.id != e.lastDictID {
- if len(e.dictTable) != len(e.table) {
- e.dictTable = make([]tableEntry, len(e.table))
- }
- end := int32(len(d.content)) - 8 + e.maxMatchOff
- for i := e.maxMatchOff; i < end; i += 4 {
- const hashLog = betterShortTableBits
- cv := load6432(d.content, i-e.maxMatchOff)
- nextHash := hashLen(cv, hashLog, betterShortLen) // 0 -> 4
- nextHash1 := hashLen(cv>>8, hashLog, betterShortLen) // 1 -> 5
- nextHash2 := hashLen(cv>>16, hashLog, betterShortLen) // 2 -> 6
- nextHash3 := hashLen(cv>>24, hashLog, betterShortLen) // 3 -> 7
- e.dictTable[nextHash] = tableEntry{
- val: uint32(cv),
- offset: i,
- }
- e.dictTable[nextHash1] = tableEntry{
- val: uint32(cv >> 8),
- offset: i + 1,
- }
- e.dictTable[nextHash2] = tableEntry{
- val: uint32(cv >> 16),
- offset: i + 2,
- }
- e.dictTable[nextHash3] = tableEntry{
- val: uint32(cv >> 24),
- offset: i + 3,
- }
- }
- e.lastDictID = d.id
- e.allDirty = true
- }
- // Init or copy dict table
- if len(e.dictLongTable) != len(e.longTable) || d.id != e.lastDictID {
- if len(e.dictLongTable) != len(e.longTable) {
- e.dictLongTable = make([]prevEntry, len(e.longTable))
- }
- if len(d.content) >= 8 {
- cv := load6432(d.content, 0)
- h := hashLen(cv, betterLongTableBits, betterLongLen)
- e.dictLongTable[h] = prevEntry{
- offset: e.maxMatchOff,
- prev: e.dictLongTable[h].offset,
- }
- end := int32(len(d.content)) - 8 + e.maxMatchOff
- off := 8 // First to read
- for i := e.maxMatchOff + 1; i < end; i++ {
- cv = cv>>8 | (uint64(d.content[off]) << 56)
- h := hashLen(cv, betterLongTableBits, betterLongLen)
- e.dictLongTable[h] = prevEntry{
- offset: i,
- prev: e.dictLongTable[h].offset,
- }
- off++
- }
- }
- e.lastDictID = d.id
- e.allDirty = true
- }
- // Reset table to initial state
- {
- dirtyShardCnt := 0
- if !e.allDirty {
- for i := range e.shortTableShardDirty {
- if e.shortTableShardDirty[i] {
- dirtyShardCnt++
- }
- }
- }
- const shardCnt = betterShortTableShardCnt
- const shardSize = betterShortTableShardSize
- if e.allDirty || dirtyShardCnt > shardCnt*4/6 {
- copy(e.table[:], e.dictTable)
- for i := range e.shortTableShardDirty {
- e.shortTableShardDirty[i] = false
- }
- } else {
- for i := range e.shortTableShardDirty {
- if !e.shortTableShardDirty[i] {
- continue
- }
- copy(e.table[i*shardSize:(i+1)*shardSize], e.dictTable[i*shardSize:(i+1)*shardSize])
- e.shortTableShardDirty[i] = false
- }
- }
- }
- {
- dirtyShardCnt := 0
- if !e.allDirty {
- for i := range e.shortTableShardDirty {
- if e.shortTableShardDirty[i] {
- dirtyShardCnt++
- }
- }
- }
- const shardCnt = betterLongTableShardCnt
- const shardSize = betterLongTableShardSize
- if e.allDirty || dirtyShardCnt > shardCnt*4/6 {
- copy(e.longTable[:], e.dictLongTable)
- for i := range e.longTableShardDirty {
- e.longTableShardDirty[i] = false
- }
- } else {
- for i := range e.longTableShardDirty {
- if !e.longTableShardDirty[i] {
- continue
- }
- copy(e.longTable[i*shardSize:(i+1)*shardSize], e.dictLongTable[i*shardSize:(i+1)*shardSize])
- e.longTableShardDirty[i] = false
- }
- }
- }
- e.cur = e.maxMatchOff
- e.allDirty = false
- }
- func (e *betterFastEncoderDict) markLongShardDirty(entryNum uint32) {
- e.longTableShardDirty[entryNum/betterLongTableShardSize] = true
- }
- func (e *betterFastEncoderDict) markShortShardDirty(entryNum uint32) {
- e.shortTableShardDirty[entryNum/betterShortTableShardSize] = true
- }
|