perf(client): make getTrades pagination O(N) instead of O(N^2)#40
Open
KENILSHAHH wants to merge 1 commit intoPolymarket:mainfrom
Open
perf(client): make getTrades pagination O(N) instead of O(N^2)#40KENILSHAHH wants to merge 1 commit intoPolymarket:mainfrom
KENILSHAHH wants to merge 1 commit intoPolymarket:mainfrom
Conversation
Author
|
@poly-rodr , @arun-poly any chance to get this reviewed ? |
Contributor
@Pradeep-selva can review this pls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ClobClient.getTradesaccumulated paginated results withresults = [...results, ...response.data]inside thewhileloop, re-allocating and copying the entire accumulator onevery page — O(N²) total work in trade count, plus heavy GC churn.
results.push(trade)appends. Same semantics, now O(N).response.datapages (defensive; matches original behavior).Context
For accounts with large trade histories (e.g. 50 pages × 500 trades = 25k), the old code
performed ~1275× more element copies than necessary and allocated a fresh array per page. This
was the only hot paginated loop with the issue —
getTradesPaginatedandgetBuilderTradesalready use single-page returns.
Note
Low Risk
Low risk: change is limited to how
getTradesaccumulates paginated results and adds defensive handling for missing page data, with new tests covering behavior and performance.Overview
ClobClient.getTradesnow accumulates paginated trades via in-place appends (push) instead of rebuilding arrays each page, eliminating quadratic copying/GC overhead on large histories.Adds a guard to skip empty/undefined
response.datapages, and introduces avitestsuite that mocks paging to verify ordering, cursor/only_first_pagebehavior, and includes a loose performance regression test to catch a return to O(N^2).Reviewed by Cursor Bugbot for commit c17cc40. Bugbot is set up for automated code reviews on this repo. Configure here.