Spireblight / STR-Backend

Slay the relics extension
https://dashboard.twitch.tv/extensions/ebkycs9lir8pbic2r0b7wa6bg6n7ua-0.0.1
GNU Affero General Public License v3.0
5 stars 2 forks source link

go api: reduce allocations in deck decompression #19

Closed zdylag closed 20 hours ago

zdylag commented 1 day ago

Previously we would allocate quite often in order to decompress and parse the deck strings that were given to us. This could be alleviated by precomputing some of the regular expressions, preallocating, and using manual conversion logic instead of relying on json.

This could obviously be taken much further, but this seemed like a good compromise on benefit while still retaining readability.

Benchmark comparison:

goos: linux
goarch: amd64
pkg: github.com/MaT1g3R/slaytherelics/api
cpu: AMD Ryzen 9 5900X 12-Core Processor
                              │  before.txt  │              after.txt              │
                              │    sec/op    │   sec/op     vs base                │
DecompressDeck/Simple_deck-24    7.934µ ± 1%   1.318µ ± 0%  -83.39% (p=0.000 n=10)
DecompressDeck/Big_deck-24      227.58µ ± 1%   60.34µ ± 1%  -73.48% (p=0.000 n=10)
geomean                          42.49µ        8.916µ       -79.02%

                              │  before.txt  │              after.txt               │
                              │     B/op     │     B/op      vs base                │
DecompressDeck/Simple_deck-24    5494.5 ± 0%     836.0 ± 0%  -84.78% (p=0.000 n=10)
DecompressDeck/Big_deck-24      330.8Ki ± 0%   219.2Ki ± 0%  -33.72% (p=0.000 n=10)
geomean                         42.13Ki        13.38Ki       -68.24%

                              │ before.txt  │             after.txt              │
                              │  allocs/op  │ allocs/op   vs base                │
DecompressDeck/Simple_deck-24    94.00 ± 0%   17.00 ± 0%  -81.91% (p=0.000 n=10)
DecompressDeck/Big_deck-24      1810.0 ± 0%   165.0 ± 0%  -90.88% (p=0.000 n=10)
geomean                          412.5        52.96       -87.16%