3 ms·
Really neat article, though it makes me wonder why they didn't just remove `sym_equal` entirely and replace all usages of it with `rb_obj_equal`. At the very le
by munificent 12y ago
Really neat article, though it makes me wonder why they didn't just remove `sym_equal` entirely and replace all usages of it with `rb_obj_equal`. At the very least, that `#define` needs a comment saying it must have the same identity as `rb_obj_equal` or a perf loss will happen.
- timr 12y agoWell, 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.
- mtdewcmu 12y ago>> remove `sym_equal` entirely and replace all usages of it with `rb_obj_equal`. ...it must have the same identity as `rb_obj_equal` That's exactly what #define does. It replaces all usages and gives the token the same identity as the other token. As a C programmer first and foremost, it looks reasonable to me.
- munificent 12y agoYes, I understand the semantics of #define. My point is that someone seeing a reference to sym_equal may not realize that (1) it's identical (at the C level) to rb_obj_equal and that (2) that fact has important performance ramifications. First class functions are super useful in C, but I think it's pretty rare to rely on their identity at runtime. If you are doing that, also aliasing the same function by another name seems like a recipe for confusion.
- jenrzzz 12y agoIt's an optimization hack. By nature, they aren't that nice to look at. `sym_equal` only makes 2 appearances in the whole codebase (to be bound to `Symbol#==` and `Symbol#===` in string.c), so there's not a whole lot of room for confusion. Also, this is Ruby core! Smart people do what works and makes sense to them and don't spend a lot of time worrying about how clear it is to us unwashed masses.
- wmt 12y agoCome, that's just plain wrong to say that the code quality cannot be questioned because it's an optimization and because it's made by "smart people" and it "makes sense to them". No code is sacred. It's perfectly valid to question using a macro to rename a function instead of just renaming calls, especially if the renamed function is only called twice. It also would've been more pragmatic to mention in the code that rb_obj_equal is being used due to an optimization in opt_eq_func, as it's not obvious. There might be reasons for using a macro in this case, like wanting to underline the fact that symbols are being compared, but I strongly suspect the author was just focused on changing the implementation of sym_equal to be better, and didn't think of the option of throwing it away.
- ramigb 12y agobackward compatibility?