Skip to content

Commit

Permalink
Avoid busting the global constant cache
Browse files Browse the repository at this point in the history
`Object#extend(mod)` bump the global constant cache if the module
has constants of its own.

So by moving these constants outside of `Meta` we avoid bumping
the cache.
  • Loading branch information
byroot committed Feb 18, 2022
1 parent 174a8eb commit 3fe158a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
/pkg/
/spec/reports/
/tmp/
Gemfile.lock

14 changes: 7 additions & 7 deletions lib/open-uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ def io
end
end

# :stopdoc:
RE_LWS = /[\r\n\t ]+/n
RE_TOKEN = %r{[^\x00- ()<>@,;:\\"/\[\]?={}\x7f]+}n
RE_QUOTED_STRING = %r{"(?:[\r\n\t !#-\[\]-~\x80-\xff]|\\[\x00-\x7f])*"}n
RE_PARAMETERS = %r{(?:;#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?=#{RE_LWS}?(?:#{RE_TOKEN}|#{RE_QUOTED_STRING})#{RE_LWS}?)*}n
# :startdoc:

# Mixin for holding meta-information.
module Meta
def Meta.init(obj, src=nil) # :nodoc:
Expand Down Expand Up @@ -487,13 +494,6 @@ def last_modified
end
end

# :stopdoc:
RE_LWS = /[\r\n\t ]+/n
RE_TOKEN = %r{[^\x00- ()<>@,;:\\"/\[\]?={}\x7f]+}n
RE_QUOTED_STRING = %r{"(?:[\r\n\t !#-\[\]-~\x80-\xff]|\\[\x00-\x7f])*"}n
RE_PARAMETERS = %r{(?:;#{RE_LWS}?#{RE_TOKEN}#{RE_LWS}?=#{RE_LWS}?(?:#{RE_TOKEN}|#{RE_QUOTED_STRING})#{RE_LWS}?)*}n
# :startdoc:

def content_type_parse # :nodoc:
vs = @metas['content-type']
# The last (?:;#{RE_LWS}?)? matches extra ";" which violates RFC2045.
Expand Down
10 changes: 9 additions & 1 deletion test/open-uri/test_open-uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -902,5 +902,13 @@ def test_ftp_over_http_proxy_auth
}
end

end
def test_meta_init_doesnt_bump_global_constant_state
skip "RubyVM.stat not defined" unless defined? RubyVM.stat

OpenURI::Meta.init(Object.new) # prewarm

before = RubyVM.stat(:global_constant_state)
OpenURI::Meta.init(Object.new)
assert_equal 0, RubyVM.stat(:global_constant_state) - before
end
end

0 comments on commit 3fe158a

Please sign in to comment.