Skip to content
This repository

Mail::Header: resolve performance issue #369

Merged
merged 1 commit into from over 1 year ago

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 November 17, 2012
Mikel Lindsaar mikel closed this November 17, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 10, 2012
Bogdan Gusiev Mail::Header: cache charset properly 4668309
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 6 additions and 6 deletions. Show diff stats Hide diff stats

  1. 12  lib/mail/header.rb
12  lib/mail/header.rb
@@ -159,15 +159,15 @@ def []=(name, value)
159 159
         # Need to insert in correct order for trace fields
160 160
         self.fields << Field.new(name.to_s, value, charset)
161 161
       end
  162
+      if dasherize(fn) == "content-type"
  163
+        # Update charset if specified in Content-Type
  164
+        params = self[:content_type].parameters rescue nil
  165
+        @charset = params && params[:charset]
  166
+      end
162 167
     end
163 168
     
164 169
     def charset
165  
-      params = self[:content_type].parameters rescue nil
166  
-      if params
167  
-        params[:charset]
168  
-      else
169  
-        @charset
170  
-      end
  170
+      @charset
171 171
     end
172 172
     
173 173
     def charset=(val)
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.