-
Notifications
You must be signed in to change notification settings - Fork 125
Description
Summary
This was found by mypy primer in my PR.
The following program has a high execution time:
from typing import Self
EMPTY = b""
class GridOut:
def __init__(self: Self) -> None:
self._buffer_pos = 0
self._buffer = b""
def readchunk(self: Self) -> bytes:
if not len(self._buffer) - self._buffer_pos:
raise Exception("truncated chunk")
self._buffer_pos = 0
return EMPTY
def _read_size_or_line(self: Self, size: int = -1) -> bytes:
if size > self._position:
size = self._position
if size == 0:
return bytes()
received = 0
needed = size - received
while received < size:
if self._buffer:
buf = self._buffer
chunk_start = self._buffer_pos
chunk_data = buf[self._buffer_pos :]
self._buffer = EMPTY
else:
buf = self.readchunk()
chunk_start = 0
chunk_data = buf
needed = buf.find(EMPTY, chunk_start, chunk_start + needed)
if len(chunk_data) > needed:
self._buffer = buf
self._buffer_pos = chunk_start + needed
self._position -= len(self._buffer) - self._buffer_pos
return b""
https://play.ty.dev/80ab85b0-a6d7-4aa2-8de4-b4404d6a5b4e
Check time with release is around 1 sec and in debug it's 16 seconds for me(if you start editing the code the delay visible.)
Any line I remove reduces the check time significantly. So I left them in there.
This is the original code that took 2 seconds in release and 40 seconds in debug:
https://play.ty.dev/82349c52-7963-4388-9e45-a65a90032951
From the traces I found that there is a cycle event on member_lookup_with_policy
.
TRACE ty_project::db: Salsa event: Event { thread_id: ThreadId(2), kind: WillIterateCycle { database_key: member_lookup_with_policy_(Id(f4e2)), iteration_count: IterationCount(1), fell_back: false } }
I haven't done a good investigation yet, I'm creating this issue so it's not lost. I'll do more investigation in a few days.
The original files that uncovered this issue:
https://github.com/mongodb/mongo-python-driver/blob/65f7c542088356bba78bd70d68b7a4881cab7f8b/gridfs/synchronous/grid_file.py#L1622
https://github.com/mongodb/mongo-python-driver/blob/65f7c542088356bba78bd70d68b7a4881cab7f8b/gridfs/asynchronous/grid_file.py#L1634
Version
f76d3f8 2025-07-02