From cb8ab86956bb0a633458440f1b633ed6be73679b Mon Sep 17 00:00:00 2001 From: Alexander Wang <3120367+alixander@users.noreply.github.com> Date: Mon, 7 Sep 2026 10:28:09 -0700 Subject: [PATCH 1/8] perf: skip zero residual runs in PNG row filters --- d2cli/raster_png_encoder.go | 95 +++++++++++----- d2cli/raster_png_filter_runs_test.go | 160 +++++++++++++++++++++++++++ 2 files changed, 229 insertions(+), 26 deletions(-) create mode 100644 d2cli/raster_png_filter_runs_test.go diff --git a/d2cli/raster_png_encoder.go b/d2cli/raster_png_encoder.go index 8a04a1d775..9ddc752a49 100644 --- a/d2cli/raster_png_encoder.go +++ b/d2cli/raster_png_encoder.go @@ -482,13 +482,31 @@ func rasterPNGUpFilter(destination, raw, prior []byte) int { destination[0] = 2 destination = destination[1:] cost := 0 - for index, value := range raw { - destination[index] = value - prior[index] - cost += rasterPNGAbs8(destination[index]) + for index := 0; index < len(raw); { + end := min(index+16, len(raw)) + if end-index == 16 && rasterPNGEqual16(raw[index:], prior[index:]) { + clear(destination[index:end]) + index = end + continue + } + // Avoid probing every block of a varied row that starts without an + // unchanged run, as happens for photos and gradients. + if index == 0 { + end = len(raw) + } + for ; index < end; index++ { + destination[index] = raw[index] - prior[index] + cost += rasterPNGAbs8(destination[index]) + } } return cost } +func rasterPNGEqual16(a, b []byte) bool { + return binary.LittleEndian.Uint64(a) == binary.LittleEndian.Uint64(b) && + binary.LittleEndian.Uint64(a[8:]) == binary.LittleEndian.Uint64(b[8:]) +} + func rasterPNGSubFilter(destination, raw []byte, stopAt, channels int) int { destination[0] = 1 destination = destination[1:] @@ -500,11 +518,22 @@ func rasterPNGSubFilter(destination, raw []byte, stopAt, channels int) int { return cost } } - for index := channels; index < len(raw); index++ { - destination[index] = raw[index] - raw[index-channels] - cost += rasterPNGAbs8(destination[index]) - if cost >= stopAt { - break + for index := channels; index < len(raw); { + end := min(index+16, len(raw)) + if end-index == 16 && rasterPNGEqual16(raw[index:], raw[index-channels:]) { + clear(destination[index:end]) + index = end + continue + } + if index == channels { + end = len(raw) + } + for ; index < end; index++ { + destination[index] = raw[index] - raw[index-channels] + cost += rasterPNGAbs8(destination[index]) + if cost >= stopAt { + return cost + } } } return cost @@ -544,26 +573,40 @@ func rasterPNGPaethFilter(destination, raw, prior []byte, stopAt, channels int) return cost } } - for index := channels; index < len(raw); index++ { - left := raw[index-channels] - upperLeft := prior[index-channels] - upper := prior[index] - leftDistance := rasterPNGByteDistance(upper, upperLeft) - upperDistance := rasterPNGByteDistance(left, upperLeft) - diagonalDistance := int(left) + int(upper) - 2*int(upperLeft) - if diagonalDistance < 0 { - diagonalDistance = -diagonalDistance + for index := channels; index < len(raw); { + end := min(index+16, len(raw)) + // An unchanged run, including its preceding pixel, has zero Paeth + // residuals: left equals upper-left, so the predictor is upper. + if end-index == 16 && rasterPNGEqual16(raw[index:], prior[index:]) && + bytes.Equal(raw[index-channels:index], prior[index-channels:index]) { + clear(destination[index:end]) + index = end + continue } - predictor := upperLeft - if leftDistance <= upperDistance && leftDistance <= diagonalDistance { - predictor = left - } else if upperDistance <= diagonalDistance { - predictor = upper + if index == channels { + end = len(raw) } - destination[index] = raw[index] - predictor - cost += rasterPNGAbs8(destination[index]) - if cost >= stopAt { - break + for ; index < end; index++ { + left := raw[index-channels] + upperLeft := prior[index-channels] + upper := prior[index] + leftDistance := rasterPNGByteDistance(upper, upperLeft) + upperDistance := rasterPNGByteDistance(left, upperLeft) + diagonalDistance := int(left) + int(upper) - 2*int(upperLeft) + if diagonalDistance < 0 { + diagonalDistance = -diagonalDistance + } + predictor := upperLeft + if leftDistance <= upperDistance && leftDistance <= diagonalDistance { + predictor = left + } else if upperDistance <= diagonalDistance { + predictor = upper + } + destination[index] = raw[index] - predictor + cost += rasterPNGAbs8(destination[index]) + if cost >= stopAt { + return cost + } } } return cost diff --git a/d2cli/raster_png_filter_runs_test.go b/d2cli/raster_png_filter_runs_test.go new file mode 100644 index 0000000000..b974d5c400 --- /dev/null +++ b/d2cli/raster_png_filter_runs_test.go @@ -0,0 +1,160 @@ +package d2cli + +import ( + "bytes" + "math" + "math/rand" + "testing" +) + +// Compare the actual scores and scratch writes against the pre-optimization +// scalar filters, including early termination and runs crossing pixel boundaries. +func TestRasterPNGFilterRunsMatchScalar(t *testing.T) { + random := rand.New(rand.NewSource(2879)) + for _, channels := range []int{3, 4} { + for _, width := range []int{1, 2, 5, 6, 7, 8, 10, 15, 16, 17, 31, 32, 33, 63, 64, 65, 257} { + for pattern := 0; pattern < 9; pattern++ { + raw, prior := make([]byte, width*channels), make([]byte, width*channels) + random.Read(raw) + random.Read(prior) + switch pattern { + case 1: + copy(raw, prior) + case 2, 3: + for i := range raw { + raw[i] = byte(i%channels*53 + 1) + prior[i] = raw[i] + if pattern == 3 { + prior[i] += 19 + } + } + case 4: + copy(raw, prior) + for i := channels - 1; i < len(raw); i += 16 { + raw[i] ^= 0x81 + } + case 5: + for i := range raw { + raw[i] = 255 + prior[i] = 255 + if i%32 == 15 { + raw[i] = byte(i) + } + } + case 6: + for i := 0; i < min(64, len(raw)); i++ { + raw[i] = 255 + prior[i] = 255 + } + case 7: + for i := 16; i+16 <= len(raw); i += 32 { + copy(raw[i:i+16], prior[i:i+16]) + } + case 8: + clear(raw) + clear(prior) + } + thresholds := []int{0, 1, 2, 7, 255, 65535, math.MaxInt} + scratch := make([]byte, len(raw)+1) + for _, fullCost := range []int{ + referenceRasterPNGUpFilter(scratch, raw, prior), + referenceRasterPNGSubFilter(scratch, raw, math.MaxInt, channels), + referenceRasterPNGPaethFilter(scratch, raw, prior, math.MaxInt, channels), + } { + thresholds = append(thresholds, max(0, fullCost-1), fullCost, fullCost+1) + } + for _, stopAt := range thresholds { + for filter := 0; filter < 3; filter++ { + got, want := bytes.Repeat([]byte{0xa5}, len(raw)+1), bytes.Repeat([]byte{0xa5}, len(raw)+1) + var gotCost, wantCost int + switch filter { + case 0: + gotCost = rasterPNGUpFilter(got, raw, prior) + wantCost = referenceRasterPNGUpFilter(want, raw, prior) + case 1: + gotCost = rasterPNGSubFilter(got, raw, stopAt, channels) + wantCost = referenceRasterPNGSubFilter(want, raw, stopAt, channels) + case 2: + gotCost = rasterPNGPaethFilter(got, raw, prior, stopAt, channels) + wantCost = referenceRasterPNGPaethFilter(want, raw, prior, stopAt, channels) + } + if gotCost != wantCost || !bytes.Equal(got, want) { + t.Fatalf("channels=%d width=%d pattern=%d stop=%d filter=%d: cost %d != %d or scratch differs", channels, width, pattern, stopAt, filter, gotCost, wantCost) + } + } + } + } + } + } +} + +// Frozen scalar implementations from 70affc0e. +func referenceRasterPNGUpFilter(destination, raw, prior []byte) int { + _ = prior[len(raw)-1] + destination[0] = 2 + destination = destination[1:] + cost := 0 + for index, value := range raw { + destination[index] = value - prior[index] + cost += rasterPNGAbs8(destination[index]) + } + return cost +} + +func referenceRasterPNGSubFilter(destination, raw []byte, stopAt, channels int) int { + destination[0] = 1 + destination = destination[1:] + cost := 0 + for index := 0; index < channels; index++ { + destination[index] = raw[index] + cost += rasterPNGAbs8(destination[index]) + if cost >= stopAt { + return cost + } + } + for index := channels; index < len(raw); index++ { + destination[index] = raw[index] - raw[index-channels] + cost += rasterPNGAbs8(destination[index]) + if cost >= stopAt { + break + } + } + return cost +} + +func referenceRasterPNGPaethFilter(destination, raw, prior []byte, stopAt, channels int) int { + _ = prior[len(raw)-1] + destination[0] = 4 + destination = destination[1:] + cost := 0 + for index := 0; index < channels; index++ { + destination[index] = raw[index] - prior[index] + cost += rasterPNGAbs8(destination[index]) + if cost >= stopAt { + return cost + } + } + for index := channels; index < len(raw); index++ { + left := raw[index-channels] + upperLeft := prior[index-channels] + upper := prior[index] + leftDistance := rasterPNGByteDistance(upper, upperLeft) + upperDistance := rasterPNGByteDistance(left, upperLeft) + diagonalDistance := int(left) + int(upper) - 2*int(upperLeft) + if diagonalDistance < 0 { + diagonalDistance = -diagonalDistance + } + predictor := upperLeft + if leftDistance <= upperDistance && leftDistance <= diagonalDistance { + predictor = left + } else if upperDistance <= diagonalDistance { + predictor = upper + } + destination[index] = raw[index] - predictor + cost += rasterPNGAbs8(destination[index]) + if cost >= stopAt { + break + } + } + return cost +} From ba3e9641371dd3dd4afd5b67c555e4b271472eb3 Mon Sep 17 00:00:00 2001 From: Alexander Wang <3120367+alixander@users.noreply.github.com> Date: Mon, 7 Sep 2026 10:17:33 -0700 Subject: [PATCH 2/8] Avoid repeated JSON decoding and corpus copying during rendering --- d2target/corpus_hash_test.go | 130 +++++++++++++++++++++++++++++++++++ d2target/d2target.go | 69 ++++++++++++------- 2 files changed, 173 insertions(+), 26 deletions(-) create mode 100644 d2target/corpus_hash_test.go diff --git a/d2target/corpus_hash_test.go b/d2target/corpus_hash_test.go new file mode 100644 index 0000000000..c17b09fc76 --- /dev/null +++ b/d2target/corpus_hash_test.go @@ -0,0 +1,130 @@ +package d2target + +import ( + "bytes" + "encoding/json" + "fmt" + "math" + "net/url" + "strings" + "testing" +) + +func TestHashJSONWithoutIconsMatchesNormalization(t *testing.T) { + labels := []string{ + "", `quoted "icon":{"Scheme":"fake"}`, `escaped \\"icon":{`, + "