From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-3.9 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id E19C2211BE for ; Sat, 1 Dec 2018 13:31:28 +0000 (UTC) From: Eric Wong To: kcar-public@bogomips.org Subject: [PATCH 11/11] avoid String#-@ call on request parsing under Ruby 2.6 Date: Sat, 1 Dec 2018 13:31:25 +0000 Message-Id: <20181201133125.5524-12-e@80x24.org> In-Reply-To: <20181201133125.5524-1-e@80x24.org> References: <20181201133125.5524-1-e@80x24.org> List-Id: Ruby 2.6 will unconditionally dedupe all hash key strings thanks to Anmol Chopra at https://bugs.ruby-lang.org/issues/15251 --- ext/kcar/extconf.rb | 11 +++++++++++ ext/kcar/kcar.rl | 5 +++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ext/kcar/extconf.rb b/ext/kcar/extconf.rb index b65846a..ba69c3c 100644 --- a/ext/kcar/extconf.rb +++ b/ext/kcar/extconf.rb @@ -22,4 +22,15 @@ rescue NoMethodError message("no, String#-@ not available\n") end +message('checking if Hash#[]= (rb_hash_aset) dedupes... ') +h = {} +h[%w(m k m f).join('')] = :foo +if 'mkmf'.freeze.equal?(h.keys[0]) + $CPPFLAGS += ' -DHASH_ASET_DEDUPE=1 ' + message("yes\n") +else + $CPPFLAGS += ' -DHASH_ASET_DEDUPE=0 ' + message("no, needs Ruby 2.6+\n") +end + create_makefile("kcar_ext") diff --git a/ext/kcar/kcar.rl b/ext/kcar/kcar.rl index 336af55..9d04dd8 100644 --- a/ext/kcar/kcar.rl +++ b/ext/kcar/kcar.rl @@ -489,7 +489,6 @@ static void write_response_value(struct http_parser *hp, VALUE hdr, } } -/* TODO cache */ static VALUE req_field(const char *ptr, size_t len) { size_t pfxlen = sizeof("HTTP_") - 1; @@ -501,7 +500,7 @@ static VALUE req_field(const char *ptr, size_t len) assert(*(dst + RSTRING_LEN(str)) == '\0' && "string didn't end with \\0"); /* paranoia */ - return str_dd_freeze(str); + return str; } static void snake_upcase(char *ptr, size_t len) @@ -570,6 +569,8 @@ static void write_request_value(struct http_parser *hp, VALUE env, hp->v.host = val; } else { key = req_field(fptr, flen); + if (!HASH_ASET_DEDUPE) + key = str_dd_freeze(key); } existing = rb_hash_aref(env, key); if (NIL_P(existing)) {