Skip to content

Commit de02863

Browse files
committed
fix protocolbuffers#2036: use rb_hash_* to accumulate hashes
Instead of shifting/xoring the hash at each field, use the built-in ruby apis for generating a hash from multiple input values. Now returns a Fixnum.
1 parent 047419a commit de02863

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

ruby/ext/google/protobuf_c/repeated_field.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,22 +447,20 @@ VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
447447
*/
448448
VALUE RepeatedField_hash(VALUE _self) {
449449
RepeatedField* self = ruby_to_RepeatedField(_self);
450-
451-
VALUE hash = LL2NUM(0);
452-
450+
st_index_t h = rb_hash_start(0);
451+
VALUE hash_sym = rb_intern("hash");
453452
upb_fieldtype_t field_type = self->field_type;
454453
VALUE field_type_class = self->field_type_class;
455454
size_t elem_size = native_slot_size(field_type);
456455
size_t off = 0;
457456
for (int i = 0; i < self->size; i++, off += elem_size) {
458457
void* mem = ((uint8_t *)self->elements) + off;
459458
VALUE elem = native_slot_get(field_type, field_type_class, mem);
460-
hash = rb_funcall(hash, rb_intern("<<"), 1, INT2NUM(2));
461-
hash = rb_funcall(hash, rb_intern("^"), 1,
462-
rb_funcall(elem, rb_intern("hash"), 0));
459+
h = rb_hash_uint(h, NUM2LONG(rb_funcall(elem, hash_sym, 0)));
463460
}
461+
h = rb_hash_end(h);
464462

465-
return hash;
463+
return INT2FIX(h);
466464
}
467465

468466
/*

0 commit comments

Comments
 (0)