Repository navigation
Conversation
Use ALLOCV for the scanned digits buffer (stack for short inputs) and check for Infinity/NaN only when the first character can start one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The guard lives next to the table whose first characters it mirrors, and VpAlloc calls the parser unconditionally again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On TruffleRuby, the ALLOCV_N heap fallback (inputs of 1023 bytes or more) is a plain malloc freed only by ALLOCV_END, so the invalid_value exits leaked it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
|
A final review caught a leak on TruffleRuby, where the |
This branch has not been deployed
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 free
to 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.
Hi again, a smaller one found while looking at how ruby-pg decodes
numericcolumns, which callsBigDecimal(string)for every value it reads. On our app's database, 1.19M numeric values, about 45% of that decoding time is spent in bigdecimal's parser.VpAllocallocates a temporary String withrb_str_tmp_newon every parse to hold the scanned digits, then shrinks it. This PR usesALLOCV_Ninstead, which stays on the stack for short inputs and falls back to a heap buffer from 1 KB, freed withALLOCV_ENDon every normal exit. It also only tries theInfinity/+Infinity/-Infinity/NaNcomparisons when the first character can start one of them (I,N,+,-), instead of fourstrncmpcalls on every number.The trade-off: on CRuby that heap buffer is GC-managed, so nothing leaks even when parsing raises. On TruffleRuby it's a plain
malloc; the invalid-input exits now free it, but an exponent overflow raised from insideVpCtoV(only withBigDecimal::EXCEPTION_OVERFLOWorEXCEPTION_UNDERFLOWturned on) would still skipALLOCV_ENDfor inputs over 1 KB. Scanning the string in place, without copying it unless an_was skipped, would close that too and save a few more ns; I kept it out to keep this diff small, but I'm happy to do it here or as a follow-up if you prefer.On the repository's own benchmark (
benchmark/from_small_integer.yml),int_n38goes from 191.2 to 174.9 ns;int_n9andint_n19don't reach this code and are unchanged.BigDecimal("129.90")goes from 98 ns and 3 objects to 80 ns and 2 objects, andBigDecimal("0.00")from 108 to 88 ns. All of these are medians of 9 interleaved runs, Ruby 4.0.9, macOS arm64, against master. Over 200k values sampled from our database it was about 255 → 220 ns per value. On Linux GitHub Actions runners (run, median of 7 interleaved rounds),BigDecimal("129.90")goes from 258 to 208 ns on x86_64 and from 238 to 191 ns on aarch64.Results are identical to master on 367k inputs: the distinct values of that corpus, plus 300k fuzzed strings with invalid characters, whitespace, underscores, exponents, the special values and strings over 1 KB, with and without
exception: false, and throughString#to_d. Two tests are added: one checks thatBigDecimal("129.90")and"129.90".to_dallocate only their result (it fails on master), and one parses valid and invalid strings over 1 KB underGC.stress, the path that uses the heap buffer.It's independent from #564 (
Float#to_d), and it would also benefit ged/ruby-pg#762, where I asked whether pg could reuse decoded values. Would you rather have theInfinity/NaNcheck as its own PR? Thanks for having a look!🤖 Generated with Claude Code, but prompted, read, and verified by @czj, a caring human :)