Skip to content

Commit a734989

Browse files
committed
Check only whether block was given
`rb_scan_args(argc, argv, "01&", ...)` will generate `Proc` object from block. However, the object has used to only check whether block was given. To remove redundant object generating, this patch will use `rb_block_given_p()` to check whether block was given. * Before ``` Warming up -------------------------------------- query 845.000 i/100ms each 86.916k i/100ms fields 231.527k i/100ms Calculating ------------------------------------- query 9.553k (± 2.0%) i/s - 48.320k in 5.059947s each 1.133M (± 0.3%) i/s - 5.736M in 5.062606s fields 6.319M (± 0.1%) i/s - 31.719M in 5.019960s ``` * After ``` Warming up -------------------------------------- query 864.000 i/100ms each 106.916k i/100ms fields 251.255k i/100ms Calculating ------------------------------------- query 9.457k (± 3.8%) i/s - 47.520k in 5.032949s each 1.550M (± 0.3%) i/s - 7.805M in 5.037029s fields 6.233M (± 0.1%) i/s - 31.407M in 5.039049s ```
1 parent d479969 commit a734989

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

ext/mysql2/result.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ typedef struct {
3030
int streaming;
3131
ID db_timezone;
3232
ID app_timezone;
33-
VALUE block_given;
33+
int block_given; /* boolean */
3434
} result_each_args;
3535

3636
extern VALUE mMysql2, cMysql2Client, cMysql2Error;
@@ -741,7 +741,7 @@ static VALUE rb_mysql_result_each_(VALUE self,
741741
row = fetch_row_func(self, fields, args);
742742
if (row != Qnil) {
743743
wrapper->numberOfRows++;
744-
if (args->block_given != Qnil) {
744+
if (args->block_given) {
745745
rb_yield(row);
746746
}
747747
}
@@ -791,7 +791,7 @@ static VALUE rb_mysql_result_each_(VALUE self,
791791
return Qnil;
792792
}
793793

794-
if (args->block_given != Qnil) {
794+
if (args->block_given) {
795795
rb_yield(row);
796796
}
797797
}
@@ -809,7 +809,7 @@ static VALUE rb_mysql_result_each_(VALUE self,
809809

810810
static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
811811
result_each_args args;
812-
VALUE defaults, opts, block, (*fetch_row_func)(VALUE, MYSQL_FIELD *fields, const result_each_args *args);
812+
VALUE defaults, opts, (*fetch_row_func)(VALUE, MYSQL_FIELD *fields, const result_each_args *args);
813813
ID db_timezone, app_timezone, dbTz, appTz;
814814
int symbolizeKeys, asArray, castBool, cacheRows, cast;
815815

@@ -821,7 +821,10 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
821821

822822
defaults = rb_ivar_get(self, intern_query_options);
823823
Check_Type(defaults, T_HASH);
824-
if (rb_scan_args(argc, argv, "01&", &opts, &block) == 1) {
824+
825+
// A block can be passed to this method, but since we don't call the block directly from C,
826+
// we don't need to capture it into a variable here with the "&" scan arg.
827+
if (rb_scan_args(argc, argv, "01", &opts) == 1) {
825828
opts = rb_funcall(defaults, intern_merge, 1, opts);
826829
} else {
827830
opts = defaults;
@@ -887,7 +890,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
887890
args.cast = cast;
888891
args.db_timezone = db_timezone;
889892
args.app_timezone = app_timezone;
890-
args.block_given = block;
893+
args.block_given = rb_block_given_p();
891894

892895
if (wrapper->stmt_wrapper) {
893896
fetch_row_func = rb_mysql_result_fetch_row_stmt;

0 commit comments

Comments
 (0)