Skip to content

Commit 5324129

Browse files
committed
Improve the Cursor using deque PYTHON-351
Tested faster than using pop and manually using a pointer
1 parent a65852a commit 5324129

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

pymongo/cursor.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
"""Cursor class to iterate over Mongo query results."""
16+
from collections import deque
1617

1718
from bson.code import Code
1819
from bson.son import SON
@@ -120,7 +121,7 @@ def __init__(self, collection, spec=None, fields=None, skip=0, limit=0,
120121
self.__uuid_subtype = _uuid_subtype or collection.uuid_subtype
121122
self.__query_flags = 0
122123

123-
self.__data = []
124+
self.__data = deque()
124125
self.__connection_id = None
125126
self.__retrieved = 0
126127
self.__killed = False
@@ -151,7 +152,7 @@ def rewind(self):
151152
be sent to the server, even if the resultant data has already been
152153
retrieved by this cursor.
153154
"""
154-
self.__data = []
155+
self.__data = deque()
155156
self.__id = None
156157
self.__connection_id = None
157158
self.__retrieved = 0
@@ -697,7 +698,7 @@ def __send_message(self, message):
697698
response['starting_from'], self.__retrieved))
698699

699700
self.__retrieved += response["number_returned"]
700-
self.__data = response["data"]
701+
self.__data = deque(response["data"])
701702

702703
if self.__limit and self.__id and self.__limit <= self.__retrieved:
703704
self.__die()
@@ -775,9 +776,10 @@ def next(self):
775776
db = self.__collection.database
776777
if len(self.__data) or self._refresh():
777778
if self.__manipulate:
778-
return db._fix_outgoing(self.__data.pop(0), self.__collection)
779+
return db._fix_outgoing(self.__data.popleft(),
780+
self.__collection)
779781
else:
780-
return self.__data.pop(0)
782+
return self.__data.popleft()
781783
else:
782784
raise StopIteration
783785

@@ -786,4 +788,3 @@ def __enter__(self):
786788

787789
def __exit__(self, exc_type, exc_val, exc_tb):
788790
self.__die()
789-

0 commit comments

Comments
 (0)