diff options
author | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-07-02 22:22:14 +0000 |
---|---|---|
committer | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-07-02 22:22:14 +0000 |
commit | df4e282eeb53ba19e6bd72a5a10f89a1989a55a3 (patch) | |
tree | 985d687ab1a4860bb6a1cb130a33da7ea5282e19 /io.c | |
parent | 79d757b1470b7cfbc569996d7bf9c223f30d4572 (diff) | |
download | ruby-df4e282eeb53ba19e6bd72a5a10f89a1989a55a3.tar.gz |
delay `exception: false' checks for minor speedup
Delay hash lookups until we are about to hit an exception. This
gives a minor speedup ratio of 2-3% in the new bm_io_nonblock_noex
benchmark as well as reducing code.
* benchmark/bm_io_nonblock_noex.rb: new benchmark
* ext/openssl/ossl_ssl.c (no_exception_p): new function
(ossl_start_ssl): adjust for no_exception_p
(ossl_ssl_connect): adjust ossl_start_ssl call
(ossl_ssl_connect_nonblock): ditto
(ossl_ssl_accept): ditto
(ossl_ssl_accept_nonblock): ditto
(ossl_ssl_read_internal): adjust for no_exception_p
(ossl_ssl_write_internal): ditto
(ossl_ssl_write): adjust ossl_write_internal call
(ossl_ssl_write_nonblock): ditto
* ext/stringio/stringio.c (strio_read_nonblock):
delay exception check
* io.c (no_exception_p): new function
(io_getpartial): call no_exception_p
(io_readpartial): adjust for io_getpartial
(get_kwargs_exception): remove
(io_read_nonblock): adjust for io_getpartial,
check no_exception_p on EOF
(io_write_nonblock): call no_exception_p
(rb_io_write_nonblock): do not check `exception: false'
(argf_getpartial): adjust for io_getpartial
[ruby-core:69778] [Feature #11318]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51113 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'io.c')
-rw-r--r-- | io.c | 55 |
1 files changed, 21 insertions, 34 deletions
@@ -2482,8 +2482,18 @@ read_internal_call(VALUE arg) return Qundef; } +static int +no_exception_p(VALUE opts) +{ + VALUE except; + ID id = id_exception; + + rb_get_kwargs(opts, &id, 0, 1, &except); + return except == Qfalse; +} + static VALUE -io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock, int no_exception) +io_getpartial(int argc, VALUE *argv, VALUE io, VALUE opts, int nonblock) { rb_io_t *fptr; VALUE length, str; @@ -2523,7 +2533,7 @@ io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock, int no_exception) if (!nonblock && rb_io_wait_readable(fptr->fd)) goto again; if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN)) { - if (no_exception) + if (no_exception_p(opts)) return sym_wait_readable; else rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "read would block"); @@ -2603,25 +2613,12 @@ io_readpartial(int argc, VALUE *argv, VALUE io) { VALUE ret; - ret = io_getpartial(argc, argv, io, 0, 0); + ret = io_getpartial(argc, argv, io, Qnil, 0); if (NIL_P(ret)) rb_eof_error(); return ret; } -static VALUE -get_kwargs_exception(VALUE opts) -{ - static ID ids[1]; - VALUE except; - - if (!ids[0]) - ids[0] = id_exception; - - rb_get_kwargs(opts, ids, 0, 1, &except); - return except; -} - /* * call-seq: * ios.read_nonblock(maxlen) -> string @@ -2676,19 +2673,14 @@ get_kwargs_exception(VALUE opts) static VALUE io_read_nonblock(int argc, VALUE *argv, VALUE io) { - VALUE ret; - VALUE opts = Qnil; - int no_exception = 0; + VALUE ret, opts; rb_scan_args(argc, argv, "11:", NULL, NULL, &opts); - if (!NIL_P(opts) && Qfalse == get_kwargs_exception(opts)) - no_exception = 1; - - ret = io_getpartial(argc, argv, io, 1, no_exception); + ret = io_getpartial(argc, argv, io, opts, 1); if (NIL_P(ret)) { - if (no_exception) + if (no_exception_p(opts)) return Qnil; else rb_eof_error(); @@ -2697,7 +2689,7 @@ io_read_nonblock(int argc, VALUE *argv, VALUE io) } static VALUE -io_write_nonblock(VALUE io, VALUE str, int no_exception) +io_write_nonblock(VALUE io, VALUE str, VALUE opts) { rb_io_t *fptr; long n; @@ -2717,7 +2709,7 @@ io_write_nonblock(VALUE io, VALUE str, int no_exception) if (n == -1) { if (errno == EWOULDBLOCK || errno == EAGAIN) { - if (no_exception) { + if (no_exception_p(opts)) { return sym_wait_writable; } else { @@ -2791,16 +2783,11 @@ io_write_nonblock(VALUE io, VALUE str, int no_exception) static VALUE rb_io_write_nonblock(int argc, VALUE *argv, VALUE io) { - VALUE str; - VALUE opts = Qnil; - int no_exceptions = 0; + VALUE str, opts; rb_scan_args(argc, argv, "10:", &str, &opts); - if (!NIL_P(opts) && Qfalse == get_kwargs_exception(opts)) - no_exceptions = 1; - - return io_write_nonblock(io, str, no_exceptions); + return io_write_nonblock(io, str, opts); } /* @@ -11218,7 +11205,7 @@ argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock) RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0); } else { - tmp = io_getpartial(argc, argv, ARGF.current_file, nonblock, 0); + tmp = io_getpartial(argc, argv, ARGF.current_file, Qnil, nonblock); } if (NIL_P(tmp)) { if (ARGF.next_p == -1) { |