Skip to content

Commit

Permalink
[ruby/net-http] Improve performance of HTTPHeader#content_type
Browse files Browse the repository at this point in the history
In the existing implementation, `main_type` and `sub_type` would end up
being called multiple times potentially.

Instead of doing that, save the result so it can be re-used.

ruby/net-http@179976f7ea
  • Loading branch information
technicalpickles authored and matzbot committed Aug 16, 2023
1 parent 8d985b1 commit 0300ea5
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/net/http/header.rb
Expand Up @@ -699,10 +699,14 @@ def range_length
# res.content_type # => "application/json"
#
def content_type
return nil unless main_type()
if sub_type()
then "#{main_type()}/#{sub_type()}"
else main_type()
main = main_type()
return nil unless main

sub = sub_type()
if sub
"#{main}/#{sub}"
else
main
end
end

Expand Down

0 comments on commit 0300ea5

Please sign in to comment.