Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster encodedword unfold by 15x #226

Merged
merged 3 commits into from
Oct 24, 2019
Merged

Conversation

carsonip
Copy link
Contributor

15x faster unfold function:

  1. Use the re object's instance method instead of re.sub to skip _compile. See: https://stackoverflow.com/a/47477439/3315725
  2. Change replacing backref to only replace part with empty string (I don't understand why it was done that way in the first place)
  3. Use non-capturing groups for another 10-20% boost
In [22]: _RE_FOLDING_WHITE_SPACES = re.compile(r"(\n\r?|\r\n?)(\s*)")

In [23]: def unfold(value):
   ....:         """
   ....:         Unfolding is accomplished by simply removing any CRLF
   ....:         that is immediately followed by WSP.  Each header field should be
   ....:         treated in its unfolded form for further syntactic and semantic
   ....:         evaluation.
   ....:         """
   ....:         return re.sub(_RE_FOLDING_WHITE_SPACES, r'\2', value)
   ....: 

In [24]: %timeit unfold(x)
100000 loops, best of 3: 9.59 µs per loop

In [26]: _RE_FOLDING_WHITE_SPACES = re.compile(r"(?:\n\r?|\r\n?)")

In [27]: def unfold(value):
   ....:         """
   ....:         Unfolding is accomplished by simply removing any CRLF
   ....:         that is immediately followed by WSP.  Each header field should be
   ....:         treated in its unfolded form for further syntactic and semantic
   ....:         evaluation.
   ....:         """
   ....:         return _RE_FOLDING_WHITE_SPACES.sub('', value)
   ....: 

In [29]: %timeit unfold(x)
1000000 loops, best of 3: 598 ns per loop

@mailgun-ci
Copy link

Can one of the admins verify this patch?

@thrawn01 thrawn01 merged commit efe6e46 into mailgun:master Oct 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants