From 6dbb9824999e9a9944c3adf265621fedbec11975 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Sat, 11 Jul 2026 10:07:11 -0700 Subject: [PATCH 1/2] Reduce allocations in main_type Instead of multiple splits, use index calls to find the appropriate offset, then extract the single string needed. Also, use strip! instead of strip as the value is a newly allocated string. --- lib/net/http/header.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/net/http/header.rb b/lib/net/http/header.rb index 6106e79..8cdb331 100644 --- a/lib/net/http/header.rb +++ b/lib/net/http/header.rb @@ -740,7 +740,11 @@ def content_type # def main_type return nil unless @header['content-type'] - self['Content-Type'].split(';').first.to_s.split('/')[0].to_s.strip + value = self['Content-Type'] + offset = (value.index(';') || value.length).clamp(0, value.index('/') || value.length) + type = value[0, offset] + type.strip! + type end # Returns the trailing ('subtype') part of the From 43cb807388f318f22b2cfa765e8e881d02cfdae9 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Sat, 11 Jul 2026 10:19:50 -0700 Subject: [PATCH 2/2] Reduce allocations in sub_type Instead of multiple splits, use index calls to find the appropriate offset, then extract the single string needed. Also, use strip! instead of strip as the value is a newly allocated string. --- lib/net/http/header.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/net/http/header.rb b/lib/net/http/header.rb index 8cdb331..0a02514 100644 --- a/lib/net/http/header.rb +++ b/lib/net/http/header.rb @@ -759,9 +759,14 @@ def main_type # def sub_type return nil unless @header['content-type'] - _, sub = *self['Content-Type'].split(';').first.to_s.split('/') - return nil unless sub - sub.strip + value = self['Content-Type'] + end_offset = value.index(";") || value.length + return unless start_offset = value.index("/") + return if start_offset > end_offset + start_offset += 1 + sub = value[start_offset, end_offset - start_offset] + sub.strip! + sub end # Returns the trailing ('parameters') part of the value of field 'Content-Type',