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

Cache regex in _process_select_response #381

Merged
merged 1 commit into from Jul 6, 2019

Conversation

Projects
None yet
2 participants
@carsonip
Copy link
Contributor

commented Jul 3, 2019

Cache the regex object explicitly instead of relying on Python re's internal cache. Using .match from object instead of re skips the checking of internal cache so this change is faster either the internal cache is full or not. Since this is more like a micro-optimization, the benchmark may not be stable but the avg results are definitely better (by at least 10%). After all, it is always more reasonable to explicitly cache a regex object.

Before: 1.49s
After: 1.27s

Benchmark code:

import mock
import time
from timeit import timeit
import re
class PatchedDict(dict):
    def __get__(self, *args, **kwargs):
        return None
re._cache = PatchedDict()
from imapclient import IMAPClient

N = 100000

resp = {'OK': ['[PERMANENTFLAGS ()] Flags permitted.', '[UIDVALIDITY 2] UIDs valid.', '[UIDNEXT 102] Predicted next UID.', '[HIGHESTMODSEQ 1097480]'], 'EXISTS': ['0'], 'PERMANENTFLAGS': ['()'], 'HIGHESTMODSEQ': ['1097480'], 'UIDNEXT': ['102'], 'FLAGS': ['(\\Answered \\Flagged \\Draft \\Deleted \\Seen $NotPhishing $Phishing)'], 'UIDVALIDITY': ['2'], 'READ-ONLY': [''], 'RECENT': ['0']}
IMAPClient._create_IMAP4 = mock.Mock()
c = IMAPClient('127.0.0.1')
t = timeit('c._process_select_response(resp)', 'from __main__ import c, resp', number=N)
print(t)

@NicolasLM NicolasLM merged commit d73bd1f into mjs:master Jul 6, 2019

1 check passed

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

This comment has been minimized.

Copy link
Collaborator

commented Jul 6, 2019

Looks good, thanks.

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.