Skip to content

Instantly share code, notes, and snippets.

@eregon
Created December 1, 2023 12:34
Show Gist options
  • Save eregon/c65e3b8071a439a3e4d7c3bd6818fc3e to your computer and use it in GitHub Desktop.
Save eregon/c65e3b8071a439a3e4d7c3bd6818fc3e to your computer and use it in GitHub Desktop.
puts RUBY_DESCRIPTION
require 'benchmark'
N = Integer(ARGV[0] || 50_000_000)
content = "Accept-Language: fr-FR, en-US#{"0" * N}"
def print_size(context, sike_kb)
puts "#{context}: #{(sike_kb / 1024.0).round(1)} MB"
end
print_size("baseline", `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i).last)
print_size("content", content.bytesize / 1024)
REGEXP_UNION = Regexp.union(
"Accept-Language:",
"Thread-Topic:",
).freeze
reg = /.*#{REGEXP_UNION}/
p reg
GC.start
print_size("before match?", `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i).last)
allocated = GC.stat(:total_allocated_objects)
p Benchmark.realtime {
p content.match(reg)
}
p GC.stat(:total_allocated_objects) - allocated
GC.start
print_size("after match?", `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i).last)
p "---"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment