Skip to content

Commit 395f7a2

Browse files
committed
improve header lookup time
1 parent ea05b1a commit 395f7a2

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

lib/webmachine/request.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@ def initialize(method, uri, headers, body)
3030
# lowercased-underscored version of the header name, e.g.
3131
# `if_unmodified_since`.
3232
def method_missing(m, *args, &block)
33-
if m.to_s =~ HTTP_HEADERS_MATCH
33+
if m =~ HTTP_HEADERS_MATCH
3434
# Access headers more easily as underscored methods.
35-
self[m.to_s.tr(UNDERSCORE, DASH)]
35+
header_name = m.to_s.tr(UNDERSCORE, DASH)
36+
if (header_value = headers[header_name])
37+
# Make future lookups faster.
38+
self.class.class_eval <<-RUBY, __FILE__, __LINE__
39+
def #{m}
40+
headers["#{header_name}"]
41+
end
42+
RUBY
43+
end
44+
header_value
3645
else
3746
super
3847
end

memory_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def to_html
2828
CONSTANTIZED = '/constantized'.freeze
2929
require 'memory_profiler'
3030
report = MemoryProfiler.report do
31-
5.times do
31+
100.times do
3232
session.get(CONSTANTIZED)
3333
end
3434
end

0 commit comments

Comments
 (0)