New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modernize usage of Ruby's C extension API #116
Merged
Merged
Conversation
This file contains 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
mudge
reviewed
Nov 11, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for contributing this, I was unaware of the newer TypedData
API.
mudge
reviewed
Nov 11, 2023
There's no need to copy an already frozen string.
Rather than using a mix of, e.g. re2_matchdata* self and re2_matchdata *self, always place the asterisk on the name rather than the type for consistency with the rest of the code.
Define rb_gc_location as the identity function for Ruby < 2.7 so that the code it generates is valid otherwise we get syntax errors like the following: ../../../../ext/re2/re2.cc: In function 'void re2_matchdata_update_references(void*)': ../../../../ext/re2/re2.cc:142:46: error: expected primary-expression before ';' token 142 | self->regexp = rb_gc_location(self->regexp);
For Ruby versions < 2.7, there is no dcompact function for TypedData so re2_matchdata_update_references and re2_scanner_update_references will never be used resulting in a compiler warning. Avoid this by only defining those functions when rb_gc_mark_movable is present.
I also ran this through ruby_memcheck and no leaks were reported. |
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.
Use the
TypedData
API introduced in Ruby 1.9.2, instead of the oldData
API that was deprecated in Ruby 2.3.This allows to implement Write Barriers.
Write barrier protected objects are allowed to be promoted to the old generation, which means they only get marked on major GC, making minor GC faster.
The downside is that the
RB_BJ_WRITE
macro MUST be used to set references, otherwise the referenced object may be garbaged collected.This also allow to implement GC compaction.