Repository navigation
Decode text dates ~4x faster in C, with 1 allocation instead of 8 - #758
Merged
Merged
Conversation
The Ruby implementation allocates the input String, a MatchData and three capture Strings per value, before the Date itself. Parsing the ISO format in C, like TextDecoder::Timestamp does, leaves only the Date: 8 -> 1 object per decoded date and ~4x faster on a 1000 row result. Behaviour is unchanged: anything else than YYYY-MM-DD (infinity, BC dates, years beyond 9999, other DateStyles) is still returned as String. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
|
I hoped someone will open a pull request for this. I'm happy it worked! Looks good. |
Contributor
Author
|
Hi @larskanis, Glad you liked it! Thanks for the quick review and the merge. I'm happy to contribute. If other spots in pg could use the same treatment, I'm up for taking a look. And if you need a hand with anything else, please don't hesitate to ask. I'd be glad to help. :) |
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.
PG::TextDecoder::Dateis still written in Ruby: for each value, pg builds the input String for the Ruby decoder, then the regexp match allocates aMatchDataand three capture Strings beforeDate.new. That's 8 objects per date, where the neighbouringTextDecoder::TimestampandBinaryDecoder::Dateare already in C.This implements it in C, on the same model as
init_inetandinit_numeric:lib/pg/text_decoder/date.rbnow only callsinit_date. Behaviour is unchanged: onlyYYYY-MM-DDbecomes aDate(stillDate::GREGORIAN), everything else (infinity, BC dates, years beyond 9999, other DateStyles) is returned as a String, and an invalid date still raisesDate::Error.Decoding 1,000 dates through
Result#valuesgoes from 8 objects to 1 per date and gets ~4x faster (2.5–3.5 ms → 0.55–0.9 ms). In our Rails app, the homepage allocates 2,690 fewer objects per request (−2.4%).The 5 new specs also pass on the Ruby implementation, which is how I checked the two agree. Full suite: 1,160 examples, 0 failures.
🤖 Generated with Claude Code, but prompted, read, and verified by @czj, a caring human :)