Remove respond_to check from Class#alloacte#13116
Conversation
In many cases these classes do have an alloc func, e.g.: rb_cMatch = rb_define_class("MatchData", rb_cObject);
rb_define_alloc_func(rb_cMatch, match_alloc);
rb_undef_method(CLASS_OF(rb_cMatch), "new");
rb_undef_method(CLASS_OF(rb_cMatch), "allocate");But perhaps there is another way we could mark these classes as not publicly allocatable. |
This comment has been minimized.
This comment has been minimized.
eregon
left a comment
There was a problem hiding this comment.
The current change in the PR means non-core classes can no longer benefit from that check (unless they already use rb_undef_alloc_func but that's not always possible, e.g. for pure-Ruby gems).
Maybe we could have a hook when undefining a method, specifically allocate to set this flag internally so it's just a performance optimization but has the exact same semantics?
Though I'm not sure the current check is ideal either.
|
Why does MatchData define an alloc_func, what is used for? |
For |
1f1a3db to
a6365ce
Compare
My point in opening https://bugs.ruby-lang.org/issues/21267 is that the exact existing semantics are useless, as it only protects against someone trying really hard to make an uninitialized object (by calling For pure-Ruby gems. I don't think this "feature" is necessary, because uninitialized objects won't crash the VM (which I believe is the motivation for the extra checking). |
|
Per https://bugs.ruby-lang.org/issues/21267?next_issue_id=21302&prev_issue_id=21298#note-1 I've reverted this to my original proposal, simply removing the check. |
|
I'm OK with the removal of that check (in case it wasn't clear from my replies on https://bugs.ruby-lang.org/issues/21267). |
https://bugs.ruby-lang.org/issues/21267
Class#allocatehas an additionalrb_obj_respond_to(klass, rb_intern("allocate"))check to forbid allocate being called on a class where it has been made private or undefined, even if used via ex.bind_call.However I don't think this provides any additional protection from users accessing an uninitialized object, as the user can redefine allocate to anything to bypass the check:
Or even override
respond_to_missing?So I think we should remove this check. For classes that we need to forbid allocation we should use
rb_undef_alloc_func.The classes I see this used for are:
My main motivation is that this check makes
Class#allocateslow. There are ways we could improve that, but I don't think the check as-is is useful. If there's an alternative check we'd like to do instead of simply removing this I'd be happy to implement it.This makes
allocate~75% faster.