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 utf7 encode #373

Merged
merged 1 commit into from
Apr 5, 2019
Merged

Faster utf7 encode #373

merged 1 commit into from
Apr 5, 2019

Conversation

carsonip
Copy link
Contributor

@carsonip carsonip commented Mar 25, 2019

~40% faster for input with a mix of unicode and ascii chars. The improvement is more significant for pure ascii chars.

This is achieved by removing the .encode() in the most common path (ascii char) and comparing ordinal values instead of unicode objects.

Actually we can get 10% more by eliminating dot notation, 10-20% more by inlining consume_b64_buffer function, some more by caching ord global function, but then the code will get quite ugly and I'm not sure if that's welcomed.

~40% faster for input with a mix of unicode and ascii chars
@carsonip
Copy link
Contributor Author

As a side note, combining with Cython, this can achieve a total of 10x improvement.

Copy link
Owner

@mjs mjs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant, thanks!

@mjs mjs merged commit 46a3ee6 into mjs:master Apr 5, 2019
@carsonip
Copy link
Contributor Author

carsonip commented Apr 5, 2019

Forgot to attach the benchmark script and results:

# -*- coding: utf-8 -*-
import time
from imapclient.imap_utf7 import encode, decode

q = u'你好嗎你好嗎ab 123&123 abc你' * 50

start = time.time()
for i in xrange(5000):
   encode(q)
print(time.time() - start)

CPython 2.7.12
before: 2.45s
after: 1.48s

PyPy2.7 v7.1
before: 0.284s
after: 0.198s

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.

2 participants