From 32fefc5742d1646d63a119b281baefcac551dfec Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 29 May 2015 05:59:27 +0000 Subject: http: use rb_hash_clear in Ruby 2.0+ Calling the function directly avoids the overhead of Ruby method table lookup and global method cache. The only downside is this is now hidden from tracers and cannot be overridden from Ruby, but I doubt anybody cares about that. --- ext/unicorn_http/extconf.rb | 1 + ext/unicorn_http/unicorn_http.rl | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/ext/unicorn_http/extconf.rb b/ext/unicorn_http/extconf.rb index 7a1b0cd..1da0282 100644 --- a/ext/unicorn_http/extconf.rb +++ b/ext/unicorn_http/extconf.rb @@ -5,6 +5,7 @@ have_macro("SIZEOF_OFF_T", "ruby.h") or check_sizeof("off_t", "sys/types.h") have_macro("SIZEOF_SIZE_T", "ruby.h") or check_sizeof("size_t", "sys/types.h") have_macro("SIZEOF_LONG", "ruby.h") or check_sizeof("long", "sys/types.h") have_func("rb_str_set_len", "ruby.h") +have_func("rb_hash_clear", "ruby.h") # Ruby 2.0+ have_func("gmtime_r", "time.h") create_makefile("unicorn_http") diff --git a/ext/unicorn_http/unicorn_http.rl b/ext/unicorn_http/unicorn_http.rl index 4254540..bd45dd0 100644 --- a/ext/unicorn_http/unicorn_http.rl +++ b/ext/unicorn_http/unicorn_http.rl @@ -60,7 +60,19 @@ struct http_parser { } len; }; -static ID id_clear, id_set_backtrace, id_response_start_sent; +static ID id_set_backtrace, id_response_start_sent; + +#ifdef HAVE_RB_HASH_CLEAR /* Ruby >= 2.0 */ +# define my_hash_clear(h) (void)rb_hash_clear(h) +#else /* !HAVE_RB_HASH_CLEAR - Ruby <= 1.9.3 */ + +static ID id_clear; + +static void my_hash_clear(VALUE h) +{ + rb_funcall(h, id_clear, 0); +} +#endif /* HAVE_RB_HASH_CLEAR */ static void finalize_header(struct http_parser *hp); @@ -584,7 +596,7 @@ static VALUE HttpParser_clear(VALUE self) struct http_parser *hp = data_get(self); http_parser_init(hp); - rb_funcall(hp->env, id_clear, 0); + my_hash_clear(hp->env); rb_ivar_set(self, id_response_start_sent, Qfalse); return self; @@ -926,9 +938,12 @@ void Init_unicorn_http(void) SET_GLOBAL(g_http_transfer_encoding, "TRANSFER_ENCODING"); SET_GLOBAL(g_content_length, "CONTENT_LENGTH"); SET_GLOBAL(g_http_connection, "CONNECTION"); - id_clear = rb_intern("clear"); id_set_backtrace = rb_intern("set_backtrace"); id_response_start_sent = rb_intern("@response_start_sent"); init_unicorn_httpdate(); + +#ifndef HAVE_RB_HASH_CLEAR + id_clear = rb_intern("clear"); +#endif } #undef SET_GLOBAL -- cgit v1.2.3-24-ge0c7