Skip to content

Perf: avoiding epoch.__eq__#224

Open
sneakers-the-rat wants to merge 2 commits into
mainfrom
perf-epoch-eq
Open

Perf: avoiding epoch.__eq__#224
sneakers-the-rat wants to merge 2 commits into
mainfrom
perf-epoch-eq

Conversation

@sneakers-the-rat
Copy link
Copy Markdown
Collaborator

@sneakers-the-rat sneakers-the-rat commented May 20, 2026

doing some simple profiling and perf improvements, Epoch.__eq__ was taking an astonishingly long time, 6-7% of runtime for the kitchen sink with the async runner in 1000 process calls.

Screenshot 2026-05-19 at 7 32 32 PM

turns out that can totally be eliminated in normal runs by just reordering things s.t. we don't need to call == when we are given an epoch. comments inline


📚 Documentation preview 📚: https://noob--224.org.readthedocs.build/en/224/

Comment on lines 106 to +107
# if we create epochs out of order
self._clock = count(
max([this_epoch[0].epoch, *[ep[0].epoch for ep in self._epochs], *self._epoch_log])
+ 1
)
self._last_epoch = max(self._last_epoch, this_epoch[0].epoch)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoiding needing to iterate through all epochs to find the max - we only need to consider the last epoch we've created and have an integer rather than an iterator.

Comment on lines -107 to -110
if this_epoch in self._epochs:
raise EpochExistsError(f"Epoch {this_epoch} is already scheduled")
elif this_epoch in self._epoch_log:
raise EpochCompletedError(f"Epoch {this_epoch} has already been completed!")
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid needing to use eq when searching through the epoch log (deque, epochs is a dict and key lookup is O(1)) - we only need to check this when we are passed an epoch explicitly. if we are just making 'the next one' then we can trust the internal counter (someone can come along and purposely fuck this up that that's on them lol)

Comment on lines +416 to +417
if isinstance(epoch, Epoch):
ep = epoch
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simple reordering

@sneakers-the-rat sneakers-the-rat marked this pull request as ready for review May 20, 2026 03:44
@sneakers-the-rat sneakers-the-rat added the perf gotta go fast label May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf gotta go fast

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant