Skip to content

Commit

Permalink
Check only whether block was given
Browse files Browse the repository at this point in the history
`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
```
  • Loading branch information
Watson1978 committed May 19, 2019
1 parent d479969 commit a734989
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions ext/mysql2/result.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef struct {
int streaming;
ID db_timezone;
ID app_timezone;
VALUE block_given;
int block_given; /* boolean */
} result_each_args;

extern VALUE mMysql2, cMysql2Client, cMysql2Error;
Expand Down Expand Up @@ -741,7 +741,7 @@ static VALUE rb_mysql_result_each_(VALUE self,
row = fetch_row_func(self, fields, args);
if (row != Qnil) {
wrapper->numberOfRows++;
if (args->block_given != Qnil) {
if (args->block_given) {
rb_yield(row);
}
}
Expand Down Expand Up @@ -791,7 +791,7 @@ static VALUE rb_mysql_result_each_(VALUE self,
return Qnil;
}

if (args->block_given != Qnil) {
if (args->block_given) {
rb_yield(row);
}
}
Expand All @@ -809,7 +809,7 @@ static VALUE rb_mysql_result_each_(VALUE self,

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

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

defaults = rb_ivar_get(self, intern_query_options);
Check_Type(defaults, T_HASH);
if (rb_scan_args(argc, argv, "01&", &opts, &block) == 1) {

// A block can be passed to this method, but since we don't call the block directly from C,
// we don't need to capture it into a variable here with the "&" scan arg.
if (rb_scan_args(argc, argv, "01", &opts) == 1) {
opts = rb_funcall(defaults, intern_merge, 1, opts);
} else {
opts = defaults;
Expand Down Expand Up @@ -887,7 +890,7 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
args.cast = cast;
args.db_timezone = db_timezone;
args.app_timezone = app_timezone;
args.block_given = block;
args.block_given = rb_block_given_p();

if (wrapper->stmt_wrapper) {
fetch_row_func = rb_mysql_result_fetch_row_stmt;
Expand Down

0 comments on commit a734989

Please sign in to comment.