Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Mail::Header: resolve performance issue #369

Merged
merged 1 commit into from

2 participants

Bogdan Gusiev Mikel Lindsaar
Bogdan Gusiev

Mail::Header#charset is called pretty often during header parser work.
If there are large amount of headers #[] is pretty slow.

There is already @charset variable, but it doesn't speed up the charset method. This patch makes headers parsing faster by keeping @charset variable always up to date and speeding up the charset method .

Benchmark code:

Mail::Header.new("X-SubscriberID: 345\n" * 100)

Benchmark: https://gist.github.com/6b84d985c4e2b29dbb80

Result (with patch and without patch):

Running benchmark with current working tree
Checkout HEAD^
Running benchmark with HEAD^
Checkout to previous HEAD again

                    user     system      total        real
----------------------------------headers parsing when long
After patch:    0.100000   0.000000   0.100000 (  0.089926)
Before patch:   0.700000   0.000000   0.700000 (  0.697444)

----------------------------------headers parsing when tiny
After patch:    0.000000   0.000000   0.000000 (  0.009930)
Before patch:   0.020000   0.000000   0.020000 (  0.024283)

---------------------------------headers parsing when empty
After patch:    0.010000   0.000000   0.010000 (  0.002160)
Before patch:   0.000000   0.000000   0.000000 (  0.002354)
Mikel Lindsaar
Owner

Great work, thanks :)

Mikel Lindsaar mikel merged commit 4668309 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 10, 2012
  1. Bogdan Gusiev
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 6 deletions.
  1. +6 −6 lib/mail/header.rb
12 lib/mail/header.rb
View
@@ -159,15 +159,15 @@ def []=(name, value)
# Need to insert in correct order for trace fields
self.fields << Field.new(name.to_s, value, charset)
end
+ if dasherize(fn) == "content-type"
+ # Update charset if specified in Content-Type
+ params = self[:content_type].parameters rescue nil
+ @charset = params && params[:charset]
+ end
end
def charset
- params = self[:content_type].parameters rescue nil
- if params
- params[:charset]
- else
- @charset
- end
+ @charset
end
def charset=(val)
Something went wrong with that request. Please try again.