From 16f7018af1f278a9f0cc368e224258860ea4cb8c Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 6 Dec 2018 23:26:24 +0000 Subject: deduplicate strings VM-wide in Ruby 2.5+ String#-@ deduplicates strings starting with Ruby 2.5.0 Hash#[]= deduplicates strings starting in Ruby 2.6.0-rc1 This allows us to save a small amount of memory by sharing objects with other parts of the stack (e.g. Rack). --- test/unit/test_http_parser.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'test') diff --git a/test/unit/test_http_parser.rb b/test/unit/test_http_parser.rb index 31e6f71..697af44 100644 --- a/test/unit/test_http_parser.rb +++ b/test/unit/test_http_parser.rb @@ -865,4 +865,20 @@ class HttpParserTest < Test::Unit::TestCase rescue LoadError # not all Ruby implementations have objspace end + + def test_dedupe + parser = HttpParser.new + # n.b. String#freeze optimization doesn't work under modern test-unit + exp = -'HTTP_HOST' + get = "GET / HTTP/1.1\r\nHost: example.com\r\nHavpbea-fhpxf: true\r\n\r\n" + assert parser.add_parse(get) + key = parser.env.keys.detect { |k| k == exp } + assert_same exp, key + + if RUBY_VERSION.to_r >= 2.6 # 2.6.0-rc1+ + exp = -'HTTP_HAVPBEA_FHPXF' + key = parser.env.keys.detect { |k| k == exp } + assert_same exp, key + end + end if RUBY_VERSION.to_r >= 2.5 && RUBY_ENGINE == 'ruby' end -- cgit v1.2.3-24-ge0c7