RUBY-183 better int bson performance (Chuck Remes)

This commit is contained in:
Kyle Banker 2010-09-20 11:57:06 -04:00
parent 412ddc94cd
commit 81395373e9
1 changed files with 10 additions and 6 deletions

View File

@ -240,23 +240,27 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
switch(TYPE(value)) { switch(TYPE(value)) {
case T_BIGNUM: case T_BIGNUM:
case T_FIXNUM:
{ {
if (rb_funcall(value, gt_operator, 1, LL2NUM(9223372036854775807LL)) == Qtrue || if (rb_funcall(value, gt_operator, 1, LL2NUM(9223372036854775807LL)) == Qtrue ||
rb_funcall(value, lt_operator, 1, LL2NUM(-9223372036854775808ULL)) == Qtrue) { rb_funcall(value, lt_operator, 1, LL2NUM(-9223372036854775808ULL)) == Qtrue) {
buffer_free(buffer); buffer_free(buffer);
rb_raise(rb_eRangeError, "MongoDB can only handle 8-byte ints"); rb_raise(rb_eRangeError, "MongoDB can only handle 8-byte ints");
} }
if (rb_funcall(value, gt_operator, 1, INT2NUM(2147483647L)) == Qtrue || }
rb_funcall(value, lt_operator, 1, INT2NUM(-2147483648L)) == Qtrue) { // NOTE: falls through to T_FIXNUM code
long long ll_value; case T_FIXNUM:
{
long long ll_value;
ll_value = NUM2LL(value);
if (ll_value > 2147483647L ||
ll_value < -2147483648L) {
write_name_and_type(buffer, key, 0x12); write_name_and_type(buffer, key, 0x12);
ll_value = NUM2LL(value);
SAFE_WRITE(buffer, (char*)&ll_value, 8); SAFE_WRITE(buffer, (char*)&ll_value, 8);
} else { } else {
int int_value; int int_value;
write_name_and_type(buffer, key, 0x10); write_name_and_type(buffer, key, 0x10);
int_value = NUM2LL(value); int_value = ll_value;
SAFE_WRITE(buffer, (char*)&int_value, 4); SAFE_WRITE(buffer, (char*)&int_value, 4);
} }
break; break;