Remove class alloc check#16571
Merged
Merged
Conversation
This checks that the value returned from the function registered with rb_define_alloc_func is of the correct class. When this was first introduced in 1fe40b7 (by Matz on 2001-10-03), allocation was done via user-defined Object#allocate, so it made sense to have a runtime check in release builds. Now that it's defined via rb_define_alloc_func in the C extension API, I don't think it's necessary. The check is surprisingly expensive. Removing it makes Object.new about 10% faster. It allows the C compiler to optimize the call to the function pointer as a tail-call. Removing this also allows ZJIT/YJIT to call the function directly (ZJIT already does this by having a list of known safe allocation functions). There's no way for users to ever have seen this check, other than by writing a misbehaving C extension, which returns objects with the wrong class. [Feature #21966]
e497b65 to
eb7c3f4
Compare
byroot
approved these changes
Mar 27, 2026
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.
I want to remove the following runtime check in
class_call_alloc_func, replacing it with aRUBY_ASSERT(so we would still have the check, but only on a debug build).This checks that the value returned from the function registered with
rb_define_alloc_funcis of the correct class. When this was first introduced in 1fe40b7 (by Matz on 2001-10-03), allocation was done via user-definedObject#allocate, so it made sense to have a runtime check in release builds. Now that it's defined viarb_define_alloc_funcin the C extension API, I don't think it's necessary.The check is surprisingly expensive. Removing it makes
Object.newabout 10% faster. It allows the C compiler to optimize the call to the function pointer as a tail-call. Removing this also allows ZJIT/YJIT to call the function directly (ZJIT already does this by having a list of known safe allocation functions). The diff in the assembly from skipping this is substantial https://gist.github.com/jhawthorn/20834b654bec4ba0bafee729592d5156 (though much of that is rb_class_real which I also have plans to improve).There's no way for users to ever have seen this check, other than by writing a misbehaving C extension, which returns objects with the wrong class.