Skip to content

Commit

Permalink
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.
  • Loading branch information
technicalpickles committed Aug 13, 2023
1 parent 705e7c0 commit 179976f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/net/http/header.rb
Original file line number Diff line number Diff line change
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 179976f

Please sign in to comment.