next.js
c17942f9 - Unroll recursion in `RuleCondition::matches` This trades allocations due to `Box::pin` calls during recursion for an explicit stack and a tiny interpreter loop. (#79310)

Commit
1 year ago
Unroll recursion in `RuleCondition::matches` This trades allocations due to `Box::pin` calls during recursion for an explicit stack and a tiny interpreter loop. (#79310) ### What? Replace recursion with an explicit stack to reduce allocations. Also, add tests! This trims peak heap in a build of `vercel-site` by 100-300m, see https://vercel.slack.com/archives/C06PPGZ0FD3/p1747438469043789?thread_ts=1746823119.596949&cid=C06PPGZ0FD3 ### Why? This replaces a lot of `Box::pin` calls during evaluation, instead we can amortize the heap allocation cost by pushing items onto explicit stack. This is managed as a SmallVec so in many cases we should be able to avoid allocations. ### How? Tediously introduce a small bytecode machine... sigh. The main alternatives considered were: * pulling all `awaits` up to the top level and using a normal recursive function. This works, but would be limiting when we inevitably add another async dependency on the `Source::content` * eliminating asynchrony from `Source::ident`. This turned out to be controversial (see discussion in linear) and incredibly complex to implement so it is deferred. Closes PACK-4560
Author
Parents
Loading