Skip to content

Commit 8071e05

Browse files
committed
Use rb_absint_singlebit_p to reduce object allocation
1 parent 65238c6 commit 8071e05

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

ext/mysql2/extconf.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def asplode(lib)
1414

1515
# 2.1+
1616
have_func('rb_absint_size')
17+
have_func('rb_absint_singlebit_p')
1718

1819
# 2.0-only
1920
have_header('ruby/thread.h') && have_func('rb_thread_call_without_gvl', 'ruby/thread.h')
@@ -23,7 +24,7 @@ def asplode(lib)
2324
have_func('rb_wait_for_single_fd')
2425
have_func('rb_hash_dup')
2526
have_func('rb_intern3')
26-
have_func('rb_big_cmp')
27+
have_func('rb_big_and')
2728

2829
# borrowed from mysqlplus
2930
# http://github.com/oldmoe/mysqlplus/blob/master/ext/extconf.rb

ext/mysql2/statement.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,10 @@ static int my_big2ll(VALUE bignum, LONG_LONG *ptr)
224224
}
225225
else {
226226
if (len == 8 && nlz_bits == 0 &&
227-
#ifdef HAVE_RB_BIG_AND
228-
rb_big_and(bignum, LL2NUM(LLONG_MAX>>1)) != INT2FIX(0)
227+
#ifdef HAVE_RB_ABSINT_SINGLEBIT_P
228+
!rb_absint_singlebit_p(bignum)
229+
#elif defined(HAVE_RB_BIG_AND)
230+
rb_big_and(bignum, LL2NUM(LLONG_MAX)) != INT2FIX(0)
229231
#else
230232
rb_big_cmp(bignum, LL2NUM(LLONG_MIN)) == INT2FIX(-1)
231233
#endif

spec/mysql2/statement_spec.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,16 @@
6868
end
6969

7070
it "should handle bignum but beyond int64_t" do
71-
stmt = @client.prepare('SELECT ? AS max1, ? AS min1')
71+
stmt = @client.prepare('SELECT ? AS max1, ? AS max2, ? AS min1, ? AS min2, ? AS min3')
7272
int64_max1 = (1 << 63)
73+
int64_max2 = (1 << 64) - 1
7374
int64_min1 = -(1 << 63) - 1
74-
result = stmt.execute(int64_max1, int64_min1)
75-
expect(result.to_a).to eq(['max1' => int64_max1.to_s, 'min1' => int64_min1.to_s])
75+
int64_min2 = -(1 << 64) + 1
76+
int64_min3 = -0xC000000000000000
77+
result = stmt.execute(int64_max1, int64_max2, int64_min1, int64_min2, int64_min3)
78+
expect(result.to_a).to eq(['max1' => int64_max1.to_s, 'max2' => int64_max2.to_s, 'min1' => int64_min1.to_s, 'min2' => int64_min2.to_s, 'min3' => int64_min3.to_s])
7679
end
7780

78-
it "should handle bignum but beyond uint64_t" do
79-
stmt = @client.prepare('SELECT ? AS max1, ? AS min1')
80-
int65_max1 = (1 << 64) - 1
81-
int65_min1 = -(1 << 64) + 1
82-
result = stmt.execute(int65_max1, int65_min1)
83-
expect(result.to_a).to eq(['max1' => int65_max1.to_s, 'min1' => int65_min1.to_s])
84-
end
8581
it "should keep its result after other query" do
8682
@client.query 'USE test'
8783
@client.query 'CREATE TABLE IF NOT EXISTS mysql2_stmt_q(a int)'

0 commit comments

Comments
 (0)