Skip to content

Commit ec6b32c

Browse files
carsonipmjs
authored andcommitted
Fix _proc_folder_list quadratic runtime (mjs#374)
1 parent 6df7749 commit ec6b32c

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

imapclient/imapclient.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from .datetime_util import datetime_to_INTERNALDATE, format_criteria_date
2626
from .imap_utf7 import encode as encode_utf7, decode as decode_utf7
2727
from .response_parser import parse_response, parse_message_list, parse_fetch_response
28-
from .util import to_bytes, to_unicode, assert_imap_protocol
28+
from .util import to_bytes, to_unicode, assert_imap_protocol, chunk
2929
xrange = moves.xrange
3030

3131
if PY3:
@@ -609,11 +609,7 @@ def _proc_folder_list(self, folder_data):
609609

610610
ret = []
611611
parsed = parse_response(folder_data)
612-
while parsed:
613-
# TODO: could be more efficient
614-
flags, delim, name = parsed[:3]
615-
parsed = parsed[3:]
616-
612+
for flags, delim, name in chunk(parsed, size=3):
617613
if isinstance(name, (int, long)):
618614
# Some IMAP implementations return integer folder names
619615
# with quotes. These get parsed to ints so convert them

imapclient/util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import unicode_literals
66

7+
import six
78
import logging
89
from six import binary_type, text_type
910

@@ -37,3 +38,8 @@ def assert_imap_protocol(condition, message=None):
3738
if message:
3839
msg += "{}: {}".format(msg, message)
3940
raise exceptions.ProtocolError(msg)
41+
42+
43+
def chunk(lst, size):
44+
for i in six.moves.range(0, len(lst), size):
45+
yield lst[i:i + size]

0 commit comments

Comments
 (0)