From 179976f7ea2a080235a71e01c2b3b13cb187e9fb Mon Sep 17 00:00:00 2001 From: Josh Nichols Date: Sun, 13 Aug 2023 12:55:29 -0400 Subject: [PATCH] Improve performance of HTTPHeader#content_type 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. --- lib/net/http/header.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/net/http/header.rb b/lib/net/http/header.rb index e5fabec4..e32d3da1 100644 --- a/lib/net/http/header.rb +++ b/lib/net/http/header.rb @@ -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