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

Conversation

Projects
None yet
2 participants
@carsonip
Copy link
Contributor

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.

Faster utf7 encode
~40% faster for input with a mix of unicode and ascii chars
@carsonip

This comment has been minimized.

Copy link
Contributor Author

commented Mar 25, 2019

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

@mjs

mjs approved these changes Apr 5, 2019

Copy link
Owner

left a comment

Brilliant, thanks!

@mjs mjs merged commit 46a3ee6 into mjs:master Apr 5, 2019

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@carsonip

This comment has been minimized.

Copy link
Contributor Author

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
You can’t perform that action at this time.