Uh oh!
There was an error while loading. Please reload this page.
perf: toStringAs appends digits and reverses once - #7
Merged
Conversation
table.insert(t, 1, digit) shifts the whole accumulated table on every loop iteration — quadratic in the digit count (bounded at 32 for Int, so a constant-factor cost in practice). Append least-significant-first and reverse the joined string instead; each digit is a single ASCII byte, so string reverse is safe for every radix up to 36. Measured 1.5-1.6x on 31-digit binary rendering (LuaJIT and PUC 5.1). Refs purescript-lua/purescript-lua#186.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
toStringAsbuilt its digit sequence withtable.insert(t, 1, digit), shifting the whole accumulated table on every loop iteration, quadratic in the digit count. ForIntthe digit count is bounded (at most 32 in radix 2), so in practice this is a constant-factor cost on a hot formatting path rather than an asymptotic blowup. Flagged by the fork-FFI performance audit, purescript-lua/purescript-lua#186.Change
Digits come out of the division loop least-significant first; append them and reverse the joined string once. Each digit is a single ASCII byte for every radix up to 36, so
string.reversecannot split a character. Output is unchanged.Verification
scripts/testis green: all 71 regression checks, including the multi-digit, negative and max-InttoStringAscases that pin digit order (a reversal mistake flips100into001). Measured on 31-digit binary rendering (toStringAs 2 2147483647, 2·10⁵ reps): 1.5× faster under LuaJIT (0.355 s → 0.240 s), 1.6× under PUC Lua 5.1 (1.634 s → 0.992 s).Suggested release: patch (v6.1.3) plus a package-set bump.
Refs purescript-lua/purescript-lua#186.