3 ms·
Well, among other things, you can override the object equality check in Ruby -- so you can't simply rely on pointer identity as a check of equality. That said,
by timr 12y ago
Well, among other things, you can override the object equality check in Ruby -- so you can't simply rely on pointer identity as a check of equality.
That said, yeah, it'd be nice if you could just make the assumption that if two objects point to the same memory, they're de facto identical...
- jrochkind1 12y agoYou can make that assumption for symbols. And that is in fact what the code was doing -- both before and after optimization. The optimization was about something else, weirder, that was still making string comparison (where you can't just check the pointer) faster even though it ought to be slower. Did you read the article?
- timr 12y ago"The optimization was about something else, weirder, that was still making string comparison (where you can't just check the pointer) faster even though it ought to be slower. Did you read the article?" I'm the author of the article. The optimization here is that the check for plain-old symbol equality is happening without the need for a full ruby method dispatch. You can't make that assumption in the general case, because it's possible to override equality in Ruby, which then requires more work. If you look at the full source code for the method in question, you'll see that it does special checks for Fixnums and Floats and Strings, then a check for the default object equality (i.e. does the comparison use rb_obj_equal?), then, finally, it falls back to a full method call.
- jenrzzz 12y agoActually, he wrote it!
- munificent 12y ago> so you can't simply rely on pointer identity as a check of equality. Sure, but we aren't talking Ruby semantics here, we're talking C. I don't see why it's useful to still have a sym_equal identifier that's just an alias for rb_obj_equal.