Post

We just released a new package: spatie/sourcemaps-lookup It resolves JS stack frames against source maps. Flare uses it in its frame resolver. It's 99% faster and uses 91% less memory on a 6 MB map. Took me a couple of steps to get there... 🧵

First win: lazy parsing + binary search. A source map is a list of lines, each with mappings sorted by column. The old approach decoded every line upfront, all 110K of them. By only decoding the line you ask for and halving the search space, it's only ~17 steps instead of 3000.

Then there's the decoding state. Each line stored its state as an associative array and those string keys aren't free. PHP keeps them in a hashtable at ±250 bytes per entry. Swapping to just values [0,0,0,0] is only ±150 bytes per entry. A 10MB improvement on 110K lines.

0 reposts0 likes

The biggest win: PHP's lesser-known (un)pack function lets you store data as raw bytes instead of PHP objects. Each mapping is 5 ints. As an object that's ±200 bytes, but as 5 int32s in a packed string it's only 20 bytes. That's 10x less memory used!