gitea

Development moved to Codeberg

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
  208. 208
  209. 209
  210. 210
  211. 211
  212. 212
  213. 213
  214. 214
  215. 215
  216. 216
  217. 217
  218. 218
  219. 219
  220. 220
  221. 221
  222. 222
  223. 223
  224. 224
  225. 225
  226. 226
  227. 227
  228. 228
  229. 229
  230. 230
  231. 231
  232. 232
  233. 233
  234. 234
  235. 235
  236. 236
  237. 237
  238. 238
  239. 239
  240. 240
  241. 241
  242. 242
  243. 243
  244. 244
  245. 245
  246. 246
  247. 247
  248. 248
  249. 249
  250. 250
  251. 251
  252. 252
  253. 253
  254. 254
  255. 255
  256. 256
  257. 257
  258. 258
  259. 259
  260. 260
  261. 261
  262. 262
  263. 263
  264. 264
  265. 265
  266. 266
  267. 267
  268. 268
  269. 269
  270. 270
  271. 271
  272. 272
  273. 273
  274. 274
  275. 275
  276. 276
  277. 277
  278. 278
  279. 279
  280. 280
  281. 281
  282. 282
  283. 283
  284. 284
  285. 285
  286. 286
  287. 287
  288. 288
  289. 289
  290. 290
  291. 291
  292. 292
  293. 293
  294. 294
  295. 295
  296. 296
  297. 297
  298. 298
  299. 299
  300. 300
  301. 301
  302. 302
  303. 303
  304. 304
  305. 305
  306. 306
  307. 307
  308. 308
  309. 309
  310. 310
  311. 311
  312. 312
  313. 313
  314. 314
  315. 315
  316. 316
  317. 317
  318. 318
  319. 319
  320. 320
  321. 321
  322. 322
  323. 323
  324. 324
  325. 325
  326. 326
  327. 327
  328. 328
  329. 329
  330. 330
  331. 331
  332. 332
  333. 333
  334. 334
  335. 335
  336. 336
  337. 337
  338. 338
  339. 339
  340. 340
  341. 341
  342. 342
  343. 343
  344. 344
  345. 345
  346. 346
  347. 347
  348. 348
  349. 349
  350. 350
  351. 351
  352. 352
  353. 353
  354. 354
  355. 355
  356. 356
  357. 357
  358. 358
  359. 359
  360. 360
  361. 361
  362. 362
  363. 363
  364. 364
  365. 365
  366. 366
  367. 367
  368. 368
  369. 369
  370. 370
  371. 371
  372. 372
  373. 373
  374. 374
  375. 375
  376. 376
  377. 377
  378. 378
  379. 379
  380. 380
  381. 381
  382. 382
  383. 383
  384. 384
  385. 385
  386. 386
  387. 387
  388. 388
  389. 389
  390. 390
  391. 391
  392. 392
  393. 393
  394. 394
  395. 395
  396. 396
  397. 397
  398. 398
  399. 399
  400. 400
  401. 401
  402. 402
  403. 403
  404. 404
  405. 405
  406. 406
  407. 407
  408. 408
  409. 409
  410. 410
  411. 411
  412. 412
  413. 413
  414. 414
  415. 415
  416. 416
  417. 417
  418. 418
  419. 419
  420. 420
  421. 421
  422. 422
  423. 423
  424. 424
  425. 425
  426. 426
  427. 427
  428. 428
  429. 429
  430. 430
  431. 431
  432. 432
  433. 433
  434. 434
  435. 435
  436. 436
  437. 437
  438. 438
  439. 439
  440. 440
  441. 441
  442. 442
  443. 443
  444. 444
  445. 445
  446. 446
  447. 447
  448. 448
  449. 449
  450. 450
  451. 451
  452. 452
  453. 453
  454. 454
  455. 455
  456. 456
  457. 457
  458. 458
  459. 459
  460. 460
  461. 461
  462. 462
  463. 463
  464. 464
  465. 465
  466. 466
  467. 467
  468. 468
  469. 469
  470. 470
  471. 471
  472. 472
  473. 473
  474. 474
  475. 475
  476. 476
  477. 477
  478. 478
  479. 479
  480. 480
  481. 481
  482. 482
  483. 483
  484. 484
  485. 485
  486. 486
  487. 487
  488. 488
  489. 489
  490. 490
  491. 491
  492. 492
  493. 493
  494. 494
  495. 495
  496. 496
  497. 497
  498. 498
  499. 499
  500. 500
  501. 501
  502. 502
  503. 503
  504. 504
  505. 505
  506. 506
  507. 507
  508. 508
  509. 509
  510. 510
  511. 511
  512. 512
  513. 513
  514. 514
  515. 515
  516. 516
  517. 517
  518. 518
  519. 519
  520. 520
  521. 521
  522. 522
  523. 523
  524. 524
  525. 525
  526. 526
  527. 527
  528. 528
  529. 529
  530. 530
  531. 531
  532. 532
  533. 533
  534. 534
  535. 535
  536. 536
  537. 537
  538. 538
  539. 539
  540. 540
  541. 541
  542. 542
  543. 543
  544. 544
  545. 545
  546. 546
  547. 547
  548. 548
  549. 549
  550. 550
  551. 551
  552. 552
  553. 553
  554. 554
  555. 555
  556. 556
  557. 557
  558. 558
  559. 559
  560. 560
  561. 561
  562. 562
  563. 563
  564. 564
  565. 565
  566. 566
  567. 567
  568. 568
  569. 569
  570. 570
  571. 571
  572. 572
  573. 573
  574. 574
  575. 575
  576. 576
  577. 577
  578. 578
  579. 579
  580. 580
  581. 581
  582. 582
  583. 583
  584. 584
  585. 585
  586. 586
  587. 587
  588. 588
  589. 589
  590. 590
  591. 591
  592. 592
  593. 593
  594. 594
  595. 595
  596. 596
  597. 597
  598. 598
  599. 599
  600. 600
  601. 601
  602. 602
  603. 603
  604. 604
  605. 605
  606. 606
  607. 607
  608. 608
  609. 609
  610. 610
  611. 611
  612. 612
  613. 613
  614. 614
  615. 615
  616. 616
  617. 617
  618. 618
  619. 619
  620. 620
  621. 621
  622. 622
  623. 623
  624. 624
  625. 625
  626. 626
  627. 627
  628. 628
  629. 629
  630. 630
  631. 631
  632. 632
  633. 633
  634. 634
  635. 635
  636. 636
  637. 637
  638. 638
  639. 639
  640. 640
  641. 641
  642. 642
  643. 643
  644. 644
  645. 645
  646. 646
  647. 647
  648. 648
  649. 649
  650. 650
  651. 651
  652. 652
  653. 653
  654. 654
  655. 655
  656. 656
  657. 657
  658. 658
  659. 659
  660. 660
  661. 661
  662. 662
  663. 663
  664. 664
  665. 665
  666. 666
  667. 667
  668. 668
  669. 669
  670. 670
  671. 671
  672. 672
  673. 673
  674. 674
  675. 675
  676. 676
  677. 677
  678. 678
  679. 679
  680. 680
  681. 681
  682. 682
  683. 683
  684. 684
  685. 685
  686. 686
  687. 687
  688. 688
  689. 689
  690. 690
  691. 691
  692. 692
  693. 693
  694. 694
  695. 695
  696. 696
  697. 697
  698. 698
  699. 699
  700. 700
  701. 701
  702. 702
  703. 703
  704. 704
  705. 705
  706. 706
  707. 707
  708. 708
  709. 709
  710. 710
  711. 711
  712. 712
  713. 713
  714. 714
  715. 715
  716. 716
  717. 717
  718. 718
  719. 719
  720. 720
  721. 721
  722. 722
  723. 723
  724. 724
  725. 725
  726. 726
  727. 727
  728. 728
  729. 729
  730. 730
  731. 731
  732. 732
  733. 733
  734. 734
  735. 735
  736. 736
  737. 737
  738. 738
  739. 739
  740. 740
  741. 741
  742. 742
  743. 743
  744. 744
  745. 745
  746. 746
  747. 747
  748. 748
  749. 749
  750. 750
  751. 751
  752. 752
  753. 753
  754. 754
  755. 755
  756. 756
  757. 757
  758. 758
  759. 759
  760. 760
  761. 761
  762. 762
  763. 763
  764. 764
  765. 765
  766. 766
  767. 767
  768. 768
  769. 769
  770. 770
  771. 771
  772. 772
  773. 773
  774. 774
  775. 775
  776. 776
  777. 777
  778. 778
  779. 779
  780. 780
  781. 781
  782. 782
  783. 783
  784. 784
  785. 785
  786. 786
  787. 787
  788. 788
  789. 789
  790. 790
  791. 791
  792. 792
  793. 793
  794. 794
  795. 795
  796. 796
package brotli

import "math"

type zopfliNode struct {
	length              uint32
	distance            uint32
	dcode_insert_length uint32
	u                   struct {
		cost     float32
		next     uint32
		shortcut uint32
	}
}

const maxEffectiveDistanceAlphabetSize = 544

const kInfinity float32 = 1.7e38 /* ~= 2 ^ 127 */

var kDistanceCacheIndex = []uint32{0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}

var kDistanceCacheOffset = []int{0, 0, 0, 0, -1, 1, -2, 2, -3, 3, -1, 1, -2, 2, -3, 3}

func initZopfliNodes(array []zopfliNode, length uint) {
	var stub zopfliNode
	var i uint
	stub.length = 1
	stub.distance = 0
	stub.dcode_insert_length = 0
	stub.u.cost = kInfinity
	for i = 0; i < length; i++ {
		array[i] = stub
	}
}

func zopfliNodeCopyLength(self *zopfliNode) uint32 {
	return self.length & 0x1FFFFFF
}

func zopfliNodeLengthCode(self *zopfliNode) uint32 {
	var modifier uint32 = self.length >> 25
	return zopfliNodeCopyLength(self) + 9 - modifier
}

func zopfliNodeCopyDistance(self *zopfliNode) uint32 {
	return self.distance
}

func zopfliNodeDistanceCode(self *zopfliNode) uint32 {
	var short_code uint32 = self.dcode_insert_length >> 27
	if short_code == 0 {
		return zopfliNodeCopyDistance(self) + numDistanceShortCodes - 1
	} else {
		return short_code - 1
	}
}

func zopfliNodeCommandLength(self *zopfliNode) uint32 {
	return zopfliNodeCopyLength(self) + (self.dcode_insert_length & 0x7FFFFFF)
}

/* Histogram based cost model for zopflification. */
type zopfliCostModel struct {
	cost_cmd_               [numCommandSymbols]float32
	cost_dist_              []float32
	distance_histogram_size uint32
	literal_costs_          []float32
	min_cost_cmd_           float32
	num_bytes_              uint
}

func initZopfliCostModel(self *zopfliCostModel, dist *distanceParams, num_bytes uint) {
	var distance_histogram_size uint32 = dist.alphabet_size
	if distance_histogram_size > maxEffectiveDistanceAlphabetSize {
		distance_histogram_size = maxEffectiveDistanceAlphabetSize
	}

	self.num_bytes_ = num_bytes
	self.literal_costs_ = make([]float32, (num_bytes + 2))
	self.cost_dist_ = make([]float32, (dist.alphabet_size))
	self.distance_histogram_size = distance_histogram_size
}

func cleanupZopfliCostModel(self *zopfliCostModel) {
	self.literal_costs_ = nil
	self.cost_dist_ = nil
}

func setCost(histogram []uint32, histogram_size uint, literal_histogram bool, cost []float32) {
	var sum uint = 0
	var missing_symbol_sum uint
	var log2sum float32
	var missing_symbol_cost float32
	var i uint
	for i = 0; i < histogram_size; i++ {
		sum += uint(histogram[i])
	}

	log2sum = float32(fastLog2(sum))
	missing_symbol_sum = sum
	if !literal_histogram {
		for i = 0; i < histogram_size; i++ {
			if histogram[i] == 0 {
				missing_symbol_sum++
			}
		}
	}

	missing_symbol_cost = float32(fastLog2(missing_symbol_sum)) + 2
	for i = 0; i < histogram_size; i++ {
		if histogram[i] == 0 {
			cost[i] = missing_symbol_cost
			continue
		}

		/* Shannon bits for this symbol. */
		cost[i] = log2sum - float32(fastLog2(uint(histogram[i])))

		/* Cannot be coded with less than 1 bit */
		if cost[i] < 1 {
			cost[i] = 1
		}
	}
}

func zopfliCostModelSetFromCommands(self *zopfliCostModel, position uint, ringbuffer []byte, ringbuffer_mask uint, commands []command, last_insert_len uint) {
	var histogram_literal [numLiteralSymbols]uint32
	var histogram_cmd [numCommandSymbols]uint32
	var histogram_dist [maxEffectiveDistanceAlphabetSize]uint32
	var cost_literal [numLiteralSymbols]float32
	var pos uint = position - last_insert_len
	var min_cost_cmd float32 = kInfinity
	var cost_cmd []float32 = self.cost_cmd_[:]
	var literal_costs []float32

	histogram_literal = [numLiteralSymbols]uint32{}
	histogram_cmd = [numCommandSymbols]uint32{}
	histogram_dist = [maxEffectiveDistanceAlphabetSize]uint32{}

	for i := range commands {
		var inslength uint = uint(commands[i].insert_len_)
		var copylength uint = uint(commandCopyLen(&commands[i]))
		var distcode uint = uint(commands[i].dist_prefix_) & 0x3FF
		var cmdcode uint = uint(commands[i].cmd_prefix_)
		var j uint

		histogram_cmd[cmdcode]++
		if cmdcode >= 128 {
			histogram_dist[distcode]++
		}

		for j = 0; j < inslength; j++ {
			histogram_literal[ringbuffer[(pos+j)&ringbuffer_mask]]++
		}

		pos += inslength + copylength
	}

	setCost(histogram_literal[:], numLiteralSymbols, true, cost_literal[:])
	setCost(histogram_cmd[:], numCommandSymbols, false, cost_cmd)
	setCost(histogram_dist[:], uint(self.distance_histogram_size), false, self.cost_dist_)

	for i := 0; i < numCommandSymbols; i++ {
		min_cost_cmd = brotli_min_float(min_cost_cmd, cost_cmd[i])
	}

	self.min_cost_cmd_ = min_cost_cmd
	{
		literal_costs = self.literal_costs_
		var literal_carry float32 = 0.0
		num_bytes := int(self.num_bytes_)
		literal_costs[0] = 0.0
		for i := 0; i < num_bytes; i++ {
			literal_carry += cost_literal[ringbuffer[(position+uint(i))&ringbuffer_mask]]
			literal_costs[i+1] = literal_costs[i] + literal_carry
			literal_carry -= literal_costs[i+1] - literal_costs[i]
		}
	}
}

func zopfliCostModelSetFromLiteralCosts(self *zopfliCostModel, position uint, ringbuffer []byte, ringbuffer_mask uint) {
	var literal_costs []float32 = self.literal_costs_
	var literal_carry float32 = 0.0
	var cost_dist []float32 = self.cost_dist_
	var cost_cmd []float32 = self.cost_cmd_[:]
	var num_bytes uint = self.num_bytes_
	var i uint
	estimateBitCostsForLiterals(position, num_bytes, ringbuffer_mask, ringbuffer, literal_costs[1:])
	literal_costs[0] = 0.0
	for i = 0; i < num_bytes; i++ {
		literal_carry += literal_costs[i+1]
		literal_costs[i+1] = literal_costs[i] + literal_carry
		literal_carry -= literal_costs[i+1] - literal_costs[i]
	}

	for i = 0; i < numCommandSymbols; i++ {
		cost_cmd[i] = float32(fastLog2(uint(11 + uint32(i))))
	}

	for i = 0; uint32(i) < self.distance_histogram_size; i++ {
		cost_dist[i] = float32(fastLog2(uint(20 + uint32(i))))
	}

	self.min_cost_cmd_ = float32(fastLog2(11))
}

func zopfliCostModelGetCommandCost(self *zopfliCostModel, cmdcode uint16) float32 {
	return self.cost_cmd_[cmdcode]
}

func zopfliCostModelGetDistanceCost(self *zopfliCostModel, distcode uint) float32 {
	return self.cost_dist_[distcode]
}

func zopfliCostModelGetLiteralCosts(self *zopfliCostModel, from uint, to uint) float32 {
	return self.literal_costs_[to] - self.literal_costs_[from]
}

func zopfliCostModelGetMinCostCmd(self *zopfliCostModel) float32 {
	return self.min_cost_cmd_
}

/* REQUIRES: len >= 2, start_pos <= pos */
/* REQUIRES: cost < kInfinity, nodes[start_pos].cost < kInfinity */
/* Maintains the "ZopfliNode array invariant". */
func updateZopfliNode(nodes []zopfliNode, pos uint, start_pos uint, len uint, len_code uint, dist uint, short_code uint, cost float32) {
	var next *zopfliNode = &nodes[pos+len]
	next.length = uint32(len | (len+9-len_code)<<25)
	next.distance = uint32(dist)
	next.dcode_insert_length = uint32(short_code<<27 | (pos - start_pos))
	next.u.cost = cost
}

type posData struct {
	pos            uint
	distance_cache [4]int
	costdiff       float32
	cost           float32
}

/* Maintains the smallest 8 cost difference together with their positions */
type startPosQueue struct {
	q_   [8]posData
	idx_ uint
}

func initStartPosQueue(self *startPosQueue) {
	self.idx_ = 0
}

func startPosQueueSize(self *startPosQueue) uint {
	return brotli_min_size_t(self.idx_, 8)
}

func startPosQueuePush(self *startPosQueue, posdata *posData) {
	var offset uint = ^(self.idx_) & 7
	self.idx_++
	var len uint = startPosQueueSize(self)
	var i uint
	var q []posData = self.q_[:]
	q[offset] = *posdata

	/* Restore the sorted order. In the list of |len| items at most |len - 1|
	   adjacent element comparisons / swaps are required. */
	for i = 1; i < len; i++ {
		if q[offset&7].costdiff > q[(offset+1)&7].costdiff {
			var tmp posData = q[offset&7]
			q[offset&7] = q[(offset+1)&7]
			q[(offset+1)&7] = tmp
		}

		offset++
	}
}

func startPosQueueAt(self *startPosQueue, k uint) *posData {
	return &self.q_[(k-self.idx_)&7]
}

/* Returns the minimum possible copy length that can improve the cost of any */
/* future position. */
func computeMinimumCopyLength(start_cost float32, nodes []zopfliNode, num_bytes uint, pos uint) uint {
	var min_cost float32 = start_cost
	var len uint = 2
	var next_len_bucket uint = 4
	/* Compute the minimum possible cost of reaching any future position. */

	var next_len_offset uint = 10
	for pos+len <= num_bytes && nodes[pos+len].u.cost <= min_cost {
		/* We already reached (pos + len) with no more cost than the minimum
		   possible cost of reaching anything from this pos, so there is no point in
		   looking for lengths <= len. */
		len++

		if len == next_len_offset {
			/* We reached the next copy length code bucket, so we add one more
			   extra bit to the minimum cost. */
			min_cost += 1.0

			next_len_offset += next_len_bucket
			next_len_bucket *= 2
		}
	}

	return uint(len)
}

/* REQUIRES: nodes[pos].cost < kInfinity
   REQUIRES: nodes[0..pos] satisfies that "ZopfliNode array invariant". */
func computeDistanceShortcut(block_start uint, pos uint, max_backward_limit uint, gap uint, nodes []zopfliNode) uint32 {
	var clen uint = uint(zopfliNodeCopyLength(&nodes[pos]))
	var ilen uint = uint(nodes[pos].dcode_insert_length & 0x7FFFFFF)
	var dist uint = uint(zopfliNodeCopyDistance(&nodes[pos]))

	/* Since |block_start + pos| is the end position of the command, the copy part
	   starts from |block_start + pos - clen|. Distances that are greater than
	   this or greater than |max_backward_limit| + |gap| are static dictionary
	   references, and do not update the last distances.
	   Also distance code 0 (last distance) does not update the last distances. */
	if pos == 0 {
		return 0
	} else if dist+clen <= block_start+pos+gap && dist <= max_backward_limit+gap && zopfliNodeDistanceCode(&nodes[pos]) > 0 {
		return uint32(pos)
	} else {
		return nodes[pos-clen-ilen].u.shortcut
	}
}

/* Fills in dist_cache[0..3] with the last four distances (as defined by
   Section 4. of the Spec) that would be used at (block_start + pos) if we
   used the shortest path of commands from block_start, computed from
   nodes[0..pos]. The last four distances at block_start are in
   starting_dist_cache[0..3].
   REQUIRES: nodes[pos].cost < kInfinity
   REQUIRES: nodes[0..pos] satisfies that "ZopfliNode array invariant". */
func computeDistanceCache(pos uint, starting_dist_cache []int, nodes []zopfliNode, dist_cache []int) {
	var idx int = 0
	var p uint = uint(nodes[pos].u.shortcut)
	for idx < 4 && p > 0 {
		var ilen uint = uint(nodes[p].dcode_insert_length & 0x7FFFFFF)
		var clen uint = uint(zopfliNodeCopyLength(&nodes[p]))
		var dist uint = uint(zopfliNodeCopyDistance(&nodes[p]))
		dist_cache[idx] = int(dist)
		idx++

		/* Because of prerequisite, p >= clen + ilen >= 2. */
		p = uint(nodes[p-clen-ilen].u.shortcut)
	}

	for ; idx < 4; idx++ {
		dist_cache[idx] = starting_dist_cache[0]
		starting_dist_cache = starting_dist_cache[1:]
	}
}

/* Maintains "ZopfliNode array invariant" and pushes node to the queue, if it
   is eligible. */
func evaluateNode(block_start uint, pos uint, max_backward_limit uint, gap uint, starting_dist_cache []int, model *zopfliCostModel, queue *startPosQueue, nodes []zopfliNode) {
	/* Save cost, because ComputeDistanceCache invalidates it. */
	var node_cost float32 = nodes[pos].u.cost
	nodes[pos].u.shortcut = computeDistanceShortcut(block_start, pos, max_backward_limit, gap, nodes)
	if node_cost <= zopfliCostModelGetLiteralCosts(model, 0, pos) {
		var posdata posData
		posdata.pos = pos
		posdata.cost = node_cost
		posdata.costdiff = node_cost - zopfliCostModelGetLiteralCosts(model, 0, pos)
		computeDistanceCache(pos, starting_dist_cache, nodes, posdata.distance_cache[:])
		startPosQueuePush(queue, &posdata)
	}
}

/* Returns longest copy length. */
func updateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, max_backward_limit uint, starting_dist_cache []int, num_matches uint, matches []backwardMatch, model *zopfliCostModel, queue *startPosQueue, nodes []zopfliNode) uint {
	var cur_ix uint = block_start + pos
	var cur_ix_masked uint = cur_ix & ringbuffer_mask
	var max_distance uint = brotli_min_size_t(cur_ix, max_backward_limit)
	var max_len uint = num_bytes - pos
	var max_zopfli_len uint = maxZopfliLen(params)
	var max_iters uint = maxZopfliCandidates(params)
	var min_len uint
	var result uint = 0
	var k uint
	var gap uint = 0

	evaluateNode(block_start, pos, max_backward_limit, gap, starting_dist_cache, model, queue, nodes)
	{
		var posdata *posData = startPosQueueAt(queue, 0)
		var min_cost float32 = (posdata.cost + zopfliCostModelGetMinCostCmd(model) + zopfliCostModelGetLiteralCosts(model, posdata.pos, pos))
		min_len = computeMinimumCopyLength(min_cost, nodes, num_bytes, pos)
	}

	/* Go over the command starting positions in order of increasing cost
	   difference. */
	for k = 0; k < max_iters && k < startPosQueueSize(queue); k++ {
		var posdata *posData = startPosQueueAt(queue, k)
		var start uint = posdata.pos
		var inscode uint16 = getInsertLengthCode(pos - start)
		var start_costdiff float32 = posdata.costdiff
		var base_cost float32 = start_costdiff + float32(getInsertExtra(inscode)) + zopfliCostModelGetLiteralCosts(model, 0, pos)
		var best_len uint = min_len - 1
		var j uint = 0
		/* Look for last distance matches using the distance cache from this
		   starting position. */
		for ; j < numDistanceShortCodes && best_len < max_len; j++ {
			var idx uint = uint(kDistanceCacheIndex[j])
			var backward uint = uint(posdata.distance_cache[idx] + kDistanceCacheOffset[j])
			var prev_ix uint = cur_ix - backward
			var len uint = 0
			var continuation byte = ringbuffer[cur_ix_masked+best_len]
			if cur_ix_masked+best_len > ringbuffer_mask {
				break
			}

			if backward > max_distance+gap {
				/* Word dictionary -> ignore. */
				continue
			}

			if backward <= max_distance {
				/* Regular backward reference. */
				if prev_ix >= cur_ix {
					continue
				}

				prev_ix &= ringbuffer_mask
				if prev_ix+best_len > ringbuffer_mask || continuation != ringbuffer[prev_ix+best_len] {
					continue
				}

				len = findMatchLengthWithLimit(ringbuffer[prev_ix:], ringbuffer[cur_ix_masked:], max_len)
			} else {
				continue
			}
			{
				var dist_cost float32 = base_cost + zopfliCostModelGetDistanceCost(model, j)
				var l uint
				for l = best_len + 1; l <= len; l++ {
					var copycode uint16 = getCopyLengthCode(l)
					var cmdcode uint16 = combineLengthCodes(inscode, copycode, j == 0)
					var tmp float32
					if cmdcode < 128 {
						tmp = base_cost
					} else {
						tmp = dist_cost
					}
					var cost float32 = tmp + float32(getCopyExtra(copycode)) + zopfliCostModelGetCommandCost(model, cmdcode)
					if cost < nodes[pos+l].u.cost {
						updateZopfliNode(nodes, pos, start, l, l, backward, j+1, cost)
						result = brotli_max_size_t(result, l)
					}

					best_len = l
				}
			}
		}

		/* At higher iterations look only for new last distance matches, since
		   looking only for new command start positions with the same distances
		   does not help much. */
		if k >= 2 {
			continue
		}
		{
			/* Loop through all possible copy lengths at this position. */
			var len uint = min_len
			for j = 0; j < num_matches; j++ {
				var match backwardMatch = matches[j]
				var dist uint = uint(match.distance)
				var is_dictionary_match bool = (dist > max_distance+gap)
				var dist_code uint = dist + numDistanceShortCodes - 1
				var dist_symbol uint16
				var distextra uint32
				var distnumextra uint32
				var dist_cost float32
				var max_match_len uint
				/* We already tried all possible last distance matches, so we can use
				   normal distance code here. */
				prefixEncodeCopyDistance(dist_code, uint(params.dist.num_direct_distance_codes), uint(params.dist.distance_postfix_bits), &dist_symbol, &distextra)

				distnumextra = uint32(dist_symbol) >> 10
				dist_cost = base_cost + float32(distnumextra) + zopfliCostModelGetDistanceCost(model, uint(dist_symbol)&0x3FF)

				/* Try all copy lengths up until the maximum copy length corresponding
				   to this distance. If the distance refers to the static dictionary, or
				   the maximum length is long enough, try only one maximum length. */
				max_match_len = backwardMatchLength(&match)

				if len < max_match_len && (is_dictionary_match || max_match_len > max_zopfli_len) {
					len = max_match_len
				}

				for ; len <= max_match_len; len++ {
					var len_code uint
					if is_dictionary_match {
						len_code = backwardMatchLengthCode(&match)
					} else {
						len_code = len
					}
					var copycode uint16 = getCopyLengthCode(len_code)
					var cmdcode uint16 = combineLengthCodes(inscode, copycode, false)
					var cost float32 = dist_cost + float32(getCopyExtra(copycode)) + zopfliCostModelGetCommandCost(model, cmdcode)
					if cost < nodes[pos+len].u.cost {
						updateZopfliNode(nodes, pos, start, uint(len), len_code, dist, 0, cost)
						if len > result {
							result = len
						}
					}
				}
			}
		}
	}

	return result
}

func computeShortestPathFromNodes(num_bytes uint, nodes []zopfliNode) uint {
	var index uint = num_bytes
	var num_commands uint = 0
	for nodes[index].dcode_insert_length&0x7FFFFFF == 0 && nodes[index].length == 1 {
		index--
	}
	nodes[index].u.next = math.MaxUint32
	for index != 0 {
		var len uint = uint(zopfliNodeCommandLength(&nodes[index]))
		index -= uint(len)
		nodes[index].u.next = uint32(len)
		num_commands++
	}

	return num_commands
}

/* REQUIRES: nodes != NULL and len(nodes) >= num_bytes + 1 */
func zopfliCreateCommands(num_bytes uint, block_start uint, nodes []zopfliNode, dist_cache []int, last_insert_len *uint, params *encoderParams, commands *[]command, num_literals *uint) {
	var max_backward_limit uint = maxBackwardLimit(params.lgwin)
	var pos uint = 0
	var offset uint32 = nodes[0].u.next
	var i uint
	var gap uint = 0
	for i = 0; offset != math.MaxUint32; i++ {
		var next *zopfliNode = &nodes[uint32(pos)+offset]
		var copy_length uint = uint(zopfliNodeCopyLength(next))
		var insert_length uint = uint(next.dcode_insert_length & 0x7FFFFFF)
		pos += insert_length
		offset = next.u.next
		if i == 0 {
			insert_length += *last_insert_len
			*last_insert_len = 0
		}
		{
			var distance uint = uint(zopfliNodeCopyDistance(next))
			var len_code uint = uint(zopfliNodeLengthCode(next))
			var max_distance uint = brotli_min_size_t(block_start+pos, max_backward_limit)
			var is_dictionary bool = (distance > max_distance+gap)
			var dist_code uint = uint(zopfliNodeDistanceCode(next))
			*commands = append(*commands, makeCommand(&params.dist, insert_length, copy_length, int(len_code)-int(copy_length), dist_code))

			if !is_dictionary && dist_code > 0 {
				dist_cache[3] = dist_cache[2]
				dist_cache[2] = dist_cache[1]
				dist_cache[1] = dist_cache[0]
				dist_cache[0] = int(distance)
			}
		}

		*num_literals += insert_length
		pos += copy_length
	}

	*last_insert_len += num_bytes - pos
}

func zopfliIterate(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, gap uint, dist_cache []int, model *zopfliCostModel, num_matches []uint32, matches []backwardMatch, nodes []zopfliNode) uint {
	var max_backward_limit uint = maxBackwardLimit(params.lgwin)
	var max_zopfli_len uint = maxZopfliLen(params)
	var queue startPosQueue
	var cur_match_pos uint = 0
	var i uint
	nodes[0].length = 0
	nodes[0].u.cost = 0
	initStartPosQueue(&queue)
	for i = 0; i+3 < num_bytes; i++ {
		var skip uint = updateNodes(num_bytes, position, i, ringbuffer, ringbuffer_mask, params, max_backward_limit, dist_cache, uint(num_matches[i]), matches[cur_match_pos:], model, &queue, nodes)
		if skip < longCopyQuickStep {
			skip = 0
		}
		cur_match_pos += uint(num_matches[i])
		if num_matches[i] == 1 && backwardMatchLength(&matches[cur_match_pos-1]) > max_zopfli_len {
			skip = brotli_max_size_t(backwardMatchLength(&matches[cur_match_pos-1]), skip)
		}

		if skip > 1 {
			skip--
			for skip != 0 {
				i++
				if i+3 >= num_bytes {
					break
				}
				evaluateNode(position, i, max_backward_limit, gap, dist_cache, model, &queue, nodes)
				cur_match_pos += uint(num_matches[i])
				skip--
			}
		}
	}

	return computeShortestPathFromNodes(num_bytes, nodes)
}

/* Computes the shortest path of commands from position to at most
   position + num_bytes.

   On return, path->size() is the number of commands found and path[i] is the
   length of the i-th command (copy length plus insert length).
   Note that the sum of the lengths of all commands can be less than num_bytes.

   On return, the nodes[0..num_bytes] array will have the following
   "ZopfliNode array invariant":
   For each i in [1..num_bytes], if nodes[i].cost < kInfinity, then
     (1) nodes[i].copy_length() >= 2
     (2) nodes[i].command_length() <= i and
     (3) nodes[i - nodes[i].command_length()].cost < kInfinity

 REQUIRES: nodes != nil and len(nodes) >= num_bytes + 1 */
func zopfliComputeShortestPath(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, dist_cache []int, hasher *h10, nodes []zopfliNode) uint {
	var max_backward_limit uint = maxBackwardLimit(params.lgwin)
	var max_zopfli_len uint = maxZopfliLen(params)
	var model zopfliCostModel
	var queue startPosQueue
	var matches [2 * (maxNumMatchesH10 + 64)]backwardMatch
	var store_end uint
	if num_bytes >= hasher.StoreLookahead() {
		store_end = position + num_bytes - hasher.StoreLookahead() + 1
	} else {
		store_end = position
	}
	var i uint
	var gap uint = 0
	var lz_matches_offset uint = 0
	nodes[0].length = 0
	nodes[0].u.cost = 0
	initZopfliCostModel(&model, &params.dist, num_bytes)
	zopfliCostModelSetFromLiteralCosts(&model, position, ringbuffer, ringbuffer_mask)
	initStartPosQueue(&queue)
	for i = 0; i+hasher.HashTypeLength()-1 < num_bytes; i++ {
		var pos uint = position + i
		var max_distance uint = brotli_min_size_t(pos, max_backward_limit)
		var skip uint
		var num_matches uint
		num_matches = findAllMatchesH10(hasher, &params.dictionary, ringbuffer, ringbuffer_mask, pos, num_bytes-i, max_distance, gap, params, matches[lz_matches_offset:])
		if num_matches > 0 && backwardMatchLength(&matches[num_matches-1]) > max_zopfli_len {
			matches[0] = matches[num_matches-1]
			num_matches = 1
		}

		skip = updateNodes(num_bytes, position, i, ringbuffer, ringbuffer_mask, params, max_backward_limit, dist_cache, num_matches, matches[:], &model, &queue, nodes)
		if skip < longCopyQuickStep {
			skip = 0
		}
		if num_matches == 1 && backwardMatchLength(&matches[0]) > max_zopfli_len {
			skip = brotli_max_size_t(backwardMatchLength(&matches[0]), skip)
		}

		if skip > 1 {
			/* Add the tail of the copy to the hasher. */
			hasher.StoreRange(ringbuffer, ringbuffer_mask, pos+1, brotli_min_size_t(pos+skip, store_end))

			skip--
			for skip != 0 {
				i++
				if i+hasher.HashTypeLength()-1 >= num_bytes {
					break
				}
				evaluateNode(position, i, max_backward_limit, gap, dist_cache, &model, &queue, nodes)
				skip--
			}
		}
	}

	cleanupZopfliCostModel(&model)
	return computeShortestPathFromNodes(num_bytes, nodes)
}

func createZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, hasher *h10, dist_cache []int, last_insert_len *uint, commands *[]command, num_literals *uint) {
	var nodes []zopfliNode
	nodes = make([]zopfliNode, (num_bytes + 1))
	initZopfliNodes(nodes, num_bytes+1)
	zopfliComputeShortestPath(num_bytes, position, ringbuffer, ringbuffer_mask, params, dist_cache, hasher, nodes)
	zopfliCreateCommands(num_bytes, position, nodes, dist_cache, last_insert_len, params, commands, num_literals)
	nodes = nil
}

func createHqZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, hasher hasherHandle, dist_cache []int, last_insert_len *uint, commands *[]command, num_literals *uint) {
	var max_backward_limit uint = maxBackwardLimit(params.lgwin)
	var num_matches []uint32 = make([]uint32, num_bytes)
	var matches_size uint = 4 * num_bytes
	var store_end uint
	if num_bytes >= hasher.StoreLookahead() {
		store_end = position + num_bytes - hasher.StoreLookahead() + 1
	} else {
		store_end = position
	}
	var cur_match_pos uint = 0
	var i uint
	var orig_num_literals uint
	var orig_last_insert_len uint
	var orig_dist_cache [4]int
	var orig_num_commands int
	var model zopfliCostModel
	var nodes []zopfliNode
	var matches []backwardMatch = make([]backwardMatch, matches_size)
	var gap uint = 0
	var shadow_matches uint = 0
	var new_array []backwardMatch
	for i = 0; i+hasher.HashTypeLength()-1 < num_bytes; i++ {
		var pos uint = position + i
		var max_distance uint = brotli_min_size_t(pos, max_backward_limit)
		var max_length uint = num_bytes - i
		var num_found_matches uint
		var cur_match_end uint
		var j uint

		/* Ensure that we have enough free slots. */
		if matches_size < cur_match_pos+maxNumMatchesH10+shadow_matches {
			var new_size uint = matches_size
			if new_size == 0 {
				new_size = cur_match_pos + maxNumMatchesH10 + shadow_matches
			}

			for new_size < cur_match_pos+maxNumMatchesH10+shadow_matches {
				new_size *= 2
			}

			new_array = make([]backwardMatch, new_size)
			if matches_size != 0 {
				copy(new_array, matches[:matches_size])
			}

			matches = new_array
			matches_size = new_size
		}

		num_found_matches = findAllMatchesH10(hasher.(*h10), &params.dictionary, ringbuffer, ringbuffer_mask, pos, max_length, max_distance, gap, params, matches[cur_match_pos+shadow_matches:])
		cur_match_end = cur_match_pos + num_found_matches
		for j = cur_match_pos; j+1 < cur_match_end; j++ {
			assert(backwardMatchLength(&matches[j]) <= backwardMatchLength(&matches[j+1]))
		}

		num_matches[i] = uint32(num_found_matches)
		if num_found_matches > 0 {
			var match_len uint = backwardMatchLength(&matches[cur_match_end-1])
			if match_len > maxZopfliLenQuality11 {
				var skip uint = match_len - 1
				matches[cur_match_pos] = matches[cur_match_end-1]
				cur_match_pos++
				num_matches[i] = 1

				/* Add the tail of the copy to the hasher. */
				hasher.StoreRange(ringbuffer, ringbuffer_mask, pos+1, brotli_min_size_t(pos+match_len, store_end))
				var pos uint = i
				for i := 0; i < int(skip); i++ {
					num_matches[pos+1:][i] = 0
				}
				i += skip
			} else {
				cur_match_pos = cur_match_end
			}
		}
	}

	orig_num_literals = *num_literals
	orig_last_insert_len = *last_insert_len
	copy(orig_dist_cache[:], dist_cache[:4])
	orig_num_commands = len(*commands)
	nodes = make([]zopfliNode, (num_bytes + 1))
	initZopfliCostModel(&model, &params.dist, num_bytes)
	for i = 0; i < 2; i++ {
		initZopfliNodes(nodes, num_bytes+1)
		if i == 0 {
			zopfliCostModelSetFromLiteralCosts(&model, position, ringbuffer, ringbuffer_mask)
		} else {
			zopfliCostModelSetFromCommands(&model, position, ringbuffer, ringbuffer_mask, (*commands)[orig_num_commands:], orig_last_insert_len)
		}

		*commands = (*commands)[:orig_num_commands]
		*num_literals = orig_num_literals
		*last_insert_len = orig_last_insert_len
		copy(dist_cache, orig_dist_cache[:4])
		zopfliIterate(num_bytes, position, ringbuffer, ringbuffer_mask, params, gap, dist_cache, &model, num_matches, matches, nodes)
		zopfliCreateCommands(num_bytes, position, nodes, dist_cache, last_insert_len, params, commands, num_literals)
	}

	cleanupZopfliCostModel(&model)
	nodes = nil
	matches = nil
	num_matches = nil
}