Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizes normalize_headers for performance #1029

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 20 additions & 8 deletions lib/webmock/util/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ module Util

class Headers

STANDARD_HEADER_DELIMITER = '-'.freeze
NONSTANDARD_HEADER_DELIMITER = '_'.freeze
JOIN = ', '.freeze

def self.normalize_headers(headers)
return nil unless headers
array = headers.map { |name, value|
[name.to_s.split(/_|-/).map { |segment| segment.capitalize }.join("-"),
case value

headers.each_with_object({}) do |(name, value), new_headers|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a maintainer, but each_with_object is an ActiveSupport method, and webmock doesn't depend on ActiveSupport. You'll probably need to change this for the test suite to pass.

Copy link
Contributor Author

@baweaver baweaver Jun 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://ruby-doc.org/3.2.2/Enumerable.html#method-i-each_with_object

Enumerable#each_with_object is in Ruby itself dating back several versions, confirmed that it's supported back to Ruby 1.9.3.

Also test suite was passing locally with this modification.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow. TIL. Thanks for the clarification!

new_headers[normalize_name(name)] =
case value
when Regexp then value
when Array then (value.size == 1) ? value.first.to_s : value.map {|v| v.to_s}.sort
when Array then (value.size == 1) ? value.first.to_s : value.map(&:to_s).sort
else value.to_s
end
]
}
Hash[*array.inject([]) {|r,x| r + x}]
end
end
end

def self.sorted_headers_string(headers)
Expand Down Expand Up @@ -57,6 +60,15 @@ def self.basic_auth_header(*credentials)
"Basic #{Base64.strict_encode64(credentials.join(':')).chomp}"
end

def self.normalize_name(name)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is very easy to read now. Thank you @baweaver !

name
.to_s
.tr(NONSTANDARD_HEADER_DELIMITER, STANDARD_HEADER_DELIMITER)
.split(STANDARD_HEADER_DELIMITER)
.map!(&:capitalize)
.join(STANDARD_HEADER_DELIMITER)
baweaver marked this conversation as resolved.
Show resolved Hide resolved
end

end

end
Expand Down