Skip to content

perf(mock): avoid copying call slice in AssertNumberOfCalls - #1979

Closed
AdamMagued wants to merge 1 commit into
stretchr:masterfrom
AdamMagued:perf-mock-avoid-copying-calls-in-assert
Closed

AdamMagued wants to merge 1 commit into
stretchr:masterfrom
AdamMagued:perf-mock-avoid-copying-calls-in-assert

Conversation

@AdamMagued

Copy link
Copy Markdown

AssertNumberOfCalls currently calls m.calls(), which creates a defensive copy of the entire m.Calls slice before iterating over it to count method invocations.

AssertNumberOfCalls already holds m.mutex across its entire execution and only reads call.Method. Iterating directly over m.Calls avoids allocating and copying the slice of calls without changing concurrency safety or assertion semantics.

Fixes #1957

AssertNumberOfCalls previously invoked m.calls(), which creates a defensive clone of the entire m.Calls slice before iterating over it to count matching method calls.

AssertNumberOfCalls already acquires and holds m.mutex across its execution and only reads call.Method. Iterating directly over m.Calls under the mutex lock eliminates redundant heap allocation and slice copying proportional to the call history length.

Fixes stretchr#1957
@AdamMagued

Copy link
Copy Markdown
Author

Closing in favor of earlier community PR #1958 to keep the review queue clean.

@AdamMagued AdamMagued closed this Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mock: avoid copying call history in AssertNumberOfCalls

1 participant