enc_dfast.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // Copyright 2019+ Klaus Post. All rights reserved.
  2. // License information can be found in the LICENSE file.
  3. // Based on work by Yann Collet, released under BSD License.
  4. package zstd
  5. const (
  6. dFastLongTableBits = 17 // Bits used in the long match table
  7. dFastLongTableSize = 1 << dFastLongTableBits // Size of the table
  8. dFastLongTableMask = dFastLongTableSize - 1 // Mask for table indices. Redundant, but can eliminate bounds checks.
  9. dFastShortTableBits = tableBits // Bits used in the short match table
  10. dFastShortTableSize = 1 << dFastShortTableBits // Size of the table
  11. dFastShortTableMask = dFastShortTableSize - 1 // Mask for table indices. Redundant, but can eliminate bounds checks.
  12. )
  13. type doubleFastEncoder struct {
  14. fastEncoder
  15. longTable [dFastLongTableSize]tableEntry
  16. }
  17. // Encode mimmics functionality in zstd_dfast.c
  18. func (e *doubleFastEncoder) Encode(blk *blockEnc, src []byte) {
  19. const (
  20. // Input margin is the number of bytes we read (8)
  21. // and the maximum we will read ahead (2)
  22. inputMargin = 8 + 2
  23. minNonLiteralBlockSize = 16
  24. )
  25. // Protect against e.cur wraparound.
  26. for e.cur > (1<<30)+e.maxMatchOff {
  27. if len(e.hist) == 0 {
  28. for i := range e.table[:] {
  29. e.table[i] = tableEntry{}
  30. }
  31. for i := range e.longTable[:] {
  32. e.longTable[i] = tableEntry{}
  33. }
  34. e.cur = e.maxMatchOff
  35. break
  36. }
  37. // Shift down everything in the table that isn't already too far away.
  38. minOff := e.cur + int32(len(e.hist)) - e.maxMatchOff
  39. for i := range e.table[:] {
  40. v := e.table[i].offset
  41. if v < minOff {
  42. v = 0
  43. } else {
  44. v = v - e.cur + e.maxMatchOff
  45. }
  46. e.table[i].offset = v
  47. }
  48. for i := range e.longTable[:] {
  49. v := e.longTable[i].offset
  50. if v < minOff {
  51. v = 0
  52. } else {
  53. v = v - e.cur + e.maxMatchOff
  54. }
  55. e.longTable[i].offset = v
  56. }
  57. e.cur = e.maxMatchOff
  58. }
  59. s := e.addBlock(src)
  60. blk.size = len(src)
  61. if len(src) < minNonLiteralBlockSize {
  62. blk.extraLits = len(src)
  63. blk.literals = blk.literals[:len(src)]
  64. copy(blk.literals, src)
  65. return
  66. }
  67. // Override src
  68. src = e.hist
  69. sLimit := int32(len(src)) - inputMargin
  70. // stepSize is the number of bytes to skip on every main loop iteration.
  71. // It should be >= 1.
  72. stepSize := int32(e.o.targetLength)
  73. if stepSize == 0 {
  74. stepSize++
  75. }
  76. const kSearchStrength = 8
  77. // nextEmit is where in src the next emitLiteral should start from.
  78. nextEmit := s
  79. cv := load6432(src, s)
  80. // Relative offsets
  81. offset1 := int32(blk.recentOffsets[0])
  82. offset2 := int32(blk.recentOffsets[1])
  83. addLiterals := func(s *seq, until int32) {
  84. if until == nextEmit {
  85. return
  86. }
  87. blk.literals = append(blk.literals, src[nextEmit:until]...)
  88. s.litLen = uint32(until - nextEmit)
  89. }
  90. if debug {
  91. println("recent offsets:", blk.recentOffsets)
  92. }
  93. encodeLoop:
  94. for {
  95. var t int32
  96. // We allow the encoder to optionally turn off repeat offsets across blocks
  97. canRepeat := len(blk.sequences) > 2
  98. for {
  99. if debug && canRepeat && offset1 == 0 {
  100. panic("offset0 was 0")
  101. }
  102. nextHashS := hash5(cv, dFastShortTableBits)
  103. nextHashL := hash8(cv, dFastLongTableBits)
  104. candidateL := e.longTable[nextHashL]
  105. candidateS := e.table[nextHashS]
  106. const repOff = 1
  107. repIndex := s - offset1 + repOff
  108. entry := tableEntry{offset: s + e.cur, val: uint32(cv)}
  109. e.longTable[nextHashL] = entry
  110. e.table[nextHashS] = entry
  111. if canRepeat {
  112. if repIndex >= 0 && load3232(src, repIndex) == uint32(cv>>(repOff*8)) {
  113. // Consider history as well.
  114. var seq seq
  115. lenght := 4 + e.matchlen(s+4+repOff, repIndex+4, src)
  116. seq.matchLen = uint32(lenght - zstdMinMatch)
  117. // We might be able to match backwards.
  118. // Extend as long as we can.
  119. start := s + repOff
  120. // We end the search early, so we don't risk 0 literals
  121. // and have to do special offset treatment.
  122. startLimit := nextEmit + 1
  123. tMin := s - e.maxMatchOff
  124. if tMin < 0 {
  125. tMin = 0
  126. }
  127. for repIndex > tMin && start > startLimit && src[repIndex-1] == src[start-1] && seq.matchLen < maxMatchLength-zstdMinMatch-1 {
  128. repIndex--
  129. start--
  130. seq.matchLen++
  131. }
  132. addLiterals(&seq, start)
  133. // rep 0
  134. seq.offset = 1
  135. if debugSequences {
  136. println("repeat sequence", seq, "next s:", s)
  137. }
  138. blk.sequences = append(blk.sequences, seq)
  139. s += lenght + repOff
  140. nextEmit = s
  141. if s >= sLimit {
  142. if debug {
  143. println("repeat ended", s, lenght)
  144. }
  145. break encodeLoop
  146. }
  147. cv = load6432(src, s)
  148. continue
  149. }
  150. const repOff2 = 1
  151. // We deviate from the reference encoder and also check offset 2.
  152. // Slower and not consistently better, so disabled.
  153. // repIndex = s - offset2 + repOff2
  154. if false && repIndex >= 0 && load3232(src, repIndex) == uint32(cv>>(repOff2*8)) {
  155. // Consider history as well.
  156. var seq seq
  157. lenght := 4 + e.matchlen(s+4+repOff2, repIndex+4, src)
  158. seq.matchLen = uint32(lenght - zstdMinMatch)
  159. // We might be able to match backwards.
  160. // Extend as long as we can.
  161. start := s + repOff2
  162. // We end the search early, so we don't risk 0 literals
  163. // and have to do special offset treatment.
  164. startLimit := nextEmit + 1
  165. tMin := s - e.maxMatchOff
  166. if tMin < 0 {
  167. tMin = 0
  168. }
  169. for repIndex > tMin && start > startLimit && src[repIndex-1] == src[start-1] && seq.matchLen < maxMatchLength-zstdMinMatch-1 {
  170. repIndex--
  171. start--
  172. seq.matchLen++
  173. }
  174. addLiterals(&seq, start)
  175. // rep 2
  176. seq.offset = 2
  177. if debugSequences {
  178. println("repeat sequence 2", seq, "next s:", s)
  179. }
  180. blk.sequences = append(blk.sequences, seq)
  181. s += lenght + repOff2
  182. nextEmit = s
  183. if s >= sLimit {
  184. if debug {
  185. println("repeat ended", s, lenght)
  186. }
  187. break encodeLoop
  188. }
  189. cv = load6432(src, s)
  190. // Swap offsets
  191. offset1, offset2 = offset2, offset1
  192. continue
  193. }
  194. }
  195. // Find the offsets of our two matches.
  196. coffsetL := s - (candidateL.offset - e.cur)
  197. coffsetS := s - (candidateS.offset - e.cur)
  198. // Check if we have a long match.
  199. if coffsetL < e.maxMatchOff && uint32(cv) == candidateL.val {
  200. // Found a long match, likely at least 8 bytes.
  201. // Reference encoder checks all 8 bytes, we only check 4,
  202. // but the likelihood of both the first 4 bytes and the hash matching should be enough.
  203. t = candidateL.offset - e.cur
  204. if debug && s <= t {
  205. panic("s <= t")
  206. }
  207. if debug && s-t > e.maxMatchOff {
  208. panic("s - t >e.maxMatchOff")
  209. }
  210. if debugMatches {
  211. println("long match")
  212. }
  213. break
  214. }
  215. // Check if we have a short match.
  216. if coffsetS < e.maxMatchOff && uint32(cv) == candidateS.val {
  217. // found a regular match
  218. // See if we can find a long match at s+1
  219. const checkAt = 1
  220. cv := load6432(src, s+checkAt)
  221. nextHashL = hash8(cv, dFastLongTableBits)
  222. candidateL = e.longTable[nextHashL]
  223. coffsetL = s - (candidateL.offset - e.cur) + checkAt
  224. // We can store it, since we have at least a 4 byte match.
  225. e.longTable[nextHashL] = tableEntry{offset: s + checkAt + e.cur, val: uint32(cv)}
  226. if coffsetL < e.maxMatchOff && uint32(cv) == candidateL.val {
  227. // Found a long match, likely at least 8 bytes.
  228. // Reference encoder checks all 8 bytes, we only check 4,
  229. // but the likelihood of both the first 4 bytes and the hash matching should be enough.
  230. t = candidateL.offset - e.cur
  231. s += checkAt
  232. if debugMatches {
  233. println("long match (after short)")
  234. }
  235. break
  236. }
  237. t = candidateS.offset - e.cur
  238. if debug && s <= t {
  239. panic("s <= t")
  240. }
  241. if debug && s-t > e.maxMatchOff {
  242. panic("s - t >e.maxMatchOff")
  243. }
  244. if debug && t < 0 {
  245. panic("t<0")
  246. }
  247. if debugMatches {
  248. println("short match")
  249. }
  250. break
  251. }
  252. // No match found, move forward in input.
  253. s += stepSize + ((s - nextEmit) >> (kSearchStrength - 1))
  254. if s >= sLimit {
  255. break encodeLoop
  256. }
  257. cv = load6432(src, s)
  258. }
  259. // A 4-byte match has been found. Update recent offsets.
  260. // We'll later see if more than 4 bytes.
  261. offset2 = offset1
  262. offset1 = s - t
  263. if debug && s <= t {
  264. panic("s <= t")
  265. }
  266. if debug && canRepeat && int(offset1) > len(src) {
  267. panic("invalid offset")
  268. }
  269. // Extend the 4-byte match as long as possible.
  270. l := e.matchlen(s+4, t+4, src) + 4
  271. // Extend backwards
  272. tMin := s - e.maxMatchOff
  273. if tMin < 0 {
  274. tMin = 0
  275. }
  276. for t > tMin && s > nextEmit && src[t-1] == src[s-1] && l < maxMatchLength {
  277. s--
  278. t--
  279. l++
  280. }
  281. // Write our sequence
  282. var seq seq
  283. seq.litLen = uint32(s - nextEmit)
  284. seq.matchLen = uint32(l - zstdMinMatch)
  285. if seq.litLen > 0 {
  286. blk.literals = append(blk.literals, src[nextEmit:s]...)
  287. }
  288. seq.offset = uint32(s-t) + 3
  289. s += l
  290. if debugSequences {
  291. println("sequence", seq, "next s:", s)
  292. }
  293. blk.sequences = append(blk.sequences, seq)
  294. nextEmit = s
  295. if s >= sLimit {
  296. break encodeLoop
  297. }
  298. // Index match start+1 (long) and start+2 (short)
  299. index0 := s - l + 1
  300. // Index match end-2 (long) and end-1 (short)
  301. index1 := s - 2
  302. cv0 := load6432(src, index0)
  303. cv1 := load6432(src, index1)
  304. te0 := tableEntry{offset: index0 + e.cur, val: uint32(cv0)}
  305. te1 := tableEntry{offset: index1 + e.cur, val: uint32(cv1)}
  306. e.longTable[hash8(cv0, dFastLongTableBits)] = te0
  307. e.longTable[hash8(cv1, dFastLongTableBits)] = te1
  308. cv0 >>= 8
  309. cv1 >>= 8
  310. te0.offset++
  311. te1.offset++
  312. te0.val = uint32(cv0)
  313. te1.val = uint32(cv1)
  314. e.table[hash5(cv0, dFastShortTableBits)] = te0
  315. e.table[hash5(cv1, dFastShortTableBits)] = te1
  316. cv = load6432(src, s)
  317. if !canRepeat {
  318. continue
  319. }
  320. // Check offset 2
  321. for {
  322. o2 := s - offset2
  323. if load3232(src, o2) != uint32(cv) {
  324. // Do regular search
  325. break
  326. }
  327. // Store this, since we have it.
  328. nextHashS := hash5(cv1>>8, dFastShortTableBits)
  329. nextHashL := hash8(cv, dFastLongTableBits)
  330. // We have at least 4 byte match.
  331. // No need to check backwards. We come straight from a match
  332. l := 4 + e.matchlen(s+4, o2+4, src)
  333. entry := tableEntry{offset: s + e.cur, val: uint32(cv)}
  334. e.longTable[nextHashL] = entry
  335. e.table[nextHashS] = entry
  336. seq.matchLen = uint32(l) - zstdMinMatch
  337. seq.litLen = 0
  338. // Since litlen is always 0, this is offset 1.
  339. seq.offset = 1
  340. s += l
  341. nextEmit = s
  342. if debugSequences {
  343. println("sequence", seq, "next s:", s)
  344. }
  345. blk.sequences = append(blk.sequences, seq)
  346. // Swap offset 1 and 2.
  347. offset1, offset2 = offset2, offset1
  348. if s >= sLimit {
  349. // Finished
  350. break encodeLoop
  351. }
  352. cv = load6432(src, s)
  353. }
  354. }
  355. if int(nextEmit) < len(src) {
  356. blk.literals = append(blk.literals, src[nextEmit:]...)
  357. blk.extraLits = len(src) - int(nextEmit)
  358. }
  359. blk.recentOffsets[0] = uint32(offset1)
  360. blk.recentOffsets[1] = uint32(offset2)
  361. if debug {
  362. println("returning, recent offsets:", blk.recentOffsets, "extra literals:", blk.extraLits)
  363. }
  364. }
  365. // EncodeNoHist will encode a block with no history and no following blocks.
  366. // Most notable difference is that src will not be copied for history and
  367. // we do not need to check for max match length.
  368. func (e *doubleFastEncoder) EncodeNoHist(blk *blockEnc, src []byte) {
  369. const (
  370. // Input margin is the number of bytes we read (8)
  371. // and the maximum we will read ahead (2)
  372. inputMargin = 8 + 2
  373. minNonLiteralBlockSize = 16
  374. )
  375. // Protect against e.cur wraparound.
  376. if e.cur > (1<<30)+e.maxMatchOff {
  377. for i := range e.table[:] {
  378. e.table[i] = tableEntry{}
  379. }
  380. for i := range e.longTable[:] {
  381. e.longTable[i] = tableEntry{}
  382. }
  383. e.cur = e.maxMatchOff
  384. }
  385. s := int32(0)
  386. blk.size = len(src)
  387. if len(src) < minNonLiteralBlockSize {
  388. blk.extraLits = len(src)
  389. blk.literals = blk.literals[:len(src)]
  390. copy(blk.literals, src)
  391. return
  392. }
  393. // Override src
  394. sLimit := int32(len(src)) - inputMargin
  395. // stepSize is the number of bytes to skip on every main loop iteration.
  396. // It should be >= 1.
  397. stepSize := int32(e.o.targetLength)
  398. if stepSize == 0 {
  399. stepSize++
  400. }
  401. const kSearchStrength = 8
  402. // nextEmit is where in src the next emitLiteral should start from.
  403. nextEmit := s
  404. cv := load6432(src, s)
  405. // Relative offsets
  406. offset1 := int32(blk.recentOffsets[0])
  407. offset2 := int32(blk.recentOffsets[1])
  408. addLiterals := func(s *seq, until int32) {
  409. if until == nextEmit {
  410. return
  411. }
  412. blk.literals = append(blk.literals, src[nextEmit:until]...)
  413. s.litLen = uint32(until - nextEmit)
  414. }
  415. if debug {
  416. println("recent offsets:", blk.recentOffsets)
  417. }
  418. encodeLoop:
  419. for {
  420. var t int32
  421. for {
  422. nextHashS := hash5(cv, dFastShortTableBits)
  423. nextHashL := hash8(cv, dFastLongTableBits)
  424. candidateL := e.longTable[nextHashL]
  425. candidateS := e.table[nextHashS]
  426. const repOff = 1
  427. repIndex := s - offset1 + repOff
  428. entry := tableEntry{offset: s + e.cur, val: uint32(cv)}
  429. e.longTable[nextHashL] = entry
  430. e.table[nextHashS] = entry
  431. if len(blk.sequences) > 2 {
  432. if load3232(src, repIndex) == uint32(cv>>(repOff*8)) {
  433. // Consider history as well.
  434. var seq seq
  435. //length := 4 + e.matchlen(s+4+repOff, repIndex+4, src)
  436. length := 4 + int32(matchLen(src[s+4+repOff:], src[repIndex+4:]))
  437. seq.matchLen = uint32(length - zstdMinMatch)
  438. // We might be able to match backwards.
  439. // Extend as long as we can.
  440. start := s + repOff
  441. // We end the search early, so we don't risk 0 literals
  442. // and have to do special offset treatment.
  443. startLimit := nextEmit + 1
  444. tMin := s - e.maxMatchOff
  445. if tMin < 0 {
  446. tMin = 0
  447. }
  448. for repIndex > tMin && start > startLimit && src[repIndex-1] == src[start-1] {
  449. repIndex--
  450. start--
  451. seq.matchLen++
  452. }
  453. addLiterals(&seq, start)
  454. // rep 0
  455. seq.offset = 1
  456. if debugSequences {
  457. println("repeat sequence", seq, "next s:", s)
  458. }
  459. blk.sequences = append(blk.sequences, seq)
  460. s += length + repOff
  461. nextEmit = s
  462. if s >= sLimit {
  463. if debug {
  464. println("repeat ended", s, length)
  465. }
  466. break encodeLoop
  467. }
  468. cv = load6432(src, s)
  469. continue
  470. }
  471. }
  472. // Find the offsets of our two matches.
  473. coffsetL := s - (candidateL.offset - e.cur)
  474. coffsetS := s - (candidateS.offset - e.cur)
  475. // Check if we have a long match.
  476. if coffsetL < e.maxMatchOff && uint32(cv) == candidateL.val {
  477. // Found a long match, likely at least 8 bytes.
  478. // Reference encoder checks all 8 bytes, we only check 4,
  479. // but the likelihood of both the first 4 bytes and the hash matching should be enough.
  480. t = candidateL.offset - e.cur
  481. if debug && s <= t {
  482. panic("s <= t")
  483. }
  484. if debug && s-t > e.maxMatchOff {
  485. panic("s - t >e.maxMatchOff")
  486. }
  487. if debugMatches {
  488. println("long match")
  489. }
  490. break
  491. }
  492. // Check if we have a short match.
  493. if coffsetS < e.maxMatchOff && uint32(cv) == candidateS.val {
  494. // found a regular match
  495. // See if we can find a long match at s+1
  496. const checkAt = 1
  497. cv := load6432(src, s+checkAt)
  498. nextHashL = hash8(cv, dFastLongTableBits)
  499. candidateL = e.longTable[nextHashL]
  500. coffsetL = s - (candidateL.offset - e.cur) + checkAt
  501. // We can store it, since we have at least a 4 byte match.
  502. e.longTable[nextHashL] = tableEntry{offset: s + checkAt + e.cur, val: uint32(cv)}
  503. if coffsetL < e.maxMatchOff && uint32(cv) == candidateL.val {
  504. // Found a long match, likely at least 8 bytes.
  505. // Reference encoder checks all 8 bytes, we only check 4,
  506. // but the likelihood of both the first 4 bytes and the hash matching should be enough.
  507. t = candidateL.offset - e.cur
  508. s += checkAt
  509. if debugMatches {
  510. println("long match (after short)")
  511. }
  512. break
  513. }
  514. t = candidateS.offset - e.cur
  515. if debug && s <= t {
  516. panic("s <= t")
  517. }
  518. if debug && s-t > e.maxMatchOff {
  519. panic("s - t >e.maxMatchOff")
  520. }
  521. if debug && t < 0 {
  522. panic("t<0")
  523. }
  524. if debugMatches {
  525. println("short match")
  526. }
  527. break
  528. }
  529. // No match found, move forward in input.
  530. s += stepSize + ((s - nextEmit) >> (kSearchStrength - 1))
  531. if s >= sLimit {
  532. break encodeLoop
  533. }
  534. cv = load6432(src, s)
  535. }
  536. // A 4-byte match has been found. Update recent offsets.
  537. // We'll later see if more than 4 bytes.
  538. offset2 = offset1
  539. offset1 = s - t
  540. if debug && s <= t {
  541. panic("s <= t")
  542. }
  543. // Extend the 4-byte match as long as possible.
  544. //l := e.matchlen(s+4, t+4, src) + 4
  545. l := int32(matchLen(src[s+4:], src[t+4:])) + 4
  546. // Extend backwards
  547. tMin := s - e.maxMatchOff
  548. if tMin < 0 {
  549. tMin = 0
  550. }
  551. for t > tMin && s > nextEmit && src[t-1] == src[s-1] {
  552. s--
  553. t--
  554. l++
  555. }
  556. // Write our sequence
  557. var seq seq
  558. seq.litLen = uint32(s - nextEmit)
  559. seq.matchLen = uint32(l - zstdMinMatch)
  560. if seq.litLen > 0 {
  561. blk.literals = append(blk.literals, src[nextEmit:s]...)
  562. }
  563. seq.offset = uint32(s-t) + 3
  564. s += l
  565. if debugSequences {
  566. println("sequence", seq, "next s:", s)
  567. }
  568. blk.sequences = append(blk.sequences, seq)
  569. nextEmit = s
  570. if s >= sLimit {
  571. break encodeLoop
  572. }
  573. // Index match start+1 (long) and start+2 (short)
  574. index0 := s - l + 1
  575. // Index match end-2 (long) and end-1 (short)
  576. index1 := s - 2
  577. cv0 := load6432(src, index0)
  578. cv1 := load6432(src, index1)
  579. te0 := tableEntry{offset: index0 + e.cur, val: uint32(cv0)}
  580. te1 := tableEntry{offset: index1 + e.cur, val: uint32(cv1)}
  581. e.longTable[hash8(cv0, dFastLongTableBits)] = te0
  582. e.longTable[hash8(cv1, dFastLongTableBits)] = te1
  583. cv0 >>= 8
  584. cv1 >>= 8
  585. te0.offset++
  586. te1.offset++
  587. te0.val = uint32(cv0)
  588. te1.val = uint32(cv1)
  589. e.table[hash5(cv0, dFastShortTableBits)] = te0
  590. e.table[hash5(cv1, dFastShortTableBits)] = te1
  591. cv = load6432(src, s)
  592. if len(blk.sequences) <= 2 {
  593. continue
  594. }
  595. // Check offset 2
  596. for {
  597. o2 := s - offset2
  598. if load3232(src, o2) != uint32(cv) {
  599. // Do regular search
  600. break
  601. }
  602. // Store this, since we have it.
  603. nextHashS := hash5(cv1>>8, dFastShortTableBits)
  604. nextHashL := hash8(cv, dFastLongTableBits)
  605. // We have at least 4 byte match.
  606. // No need to check backwards. We come straight from a match
  607. //l := 4 + e.matchlen(s+4, o2+4, src)
  608. l := 4 + int32(matchLen(src[s+4:], src[o2+4:]))
  609. entry := tableEntry{offset: s + e.cur, val: uint32(cv)}
  610. e.longTable[nextHashL] = entry
  611. e.table[nextHashS] = entry
  612. seq.matchLen = uint32(l) - zstdMinMatch
  613. seq.litLen = 0
  614. // Since litlen is always 0, this is offset 1.
  615. seq.offset = 1
  616. s += l
  617. nextEmit = s
  618. if debugSequences {
  619. println("sequence", seq, "next s:", s)
  620. }
  621. blk.sequences = append(blk.sequences, seq)
  622. // Swap offset 1 and 2.
  623. offset1, offset2 = offset2, offset1
  624. if s >= sLimit {
  625. // Finished
  626. break encodeLoop
  627. }
  628. cv = load6432(src, s)
  629. }
  630. }
  631. if int(nextEmit) < len(src) {
  632. blk.literals = append(blk.literals, src[nextEmit:]...)
  633. blk.extraLits = len(src) - int(nextEmit)
  634. }
  635. if debug {
  636. println("returning, recent offsets:", blk.recentOffsets, "extra literals:", blk.extraLits)
  637. }
  638. }