Skip to content

fix(find): invoke FindUniquesBy/FindDuplicatesBy iteratee once per element - #1064

Open
ump45nose wants to merge 1 commit into
samber:masterfrom
ump45nose:fix-findby-single-invocation
Open

ump45nose wants to merge 1 commit into
samber:masterfrom
ump45nose:fix-findby-single-invocation

Conversation

@ump45nose

Copy link
Copy Markdown

Describe your changes

FindUniquesBy and FindDuplicatesBy dispatch on len(collection) <= findSmallThreshold:

  • the ...Small paths precompute the derived keys and invoke the iteratee exactly once per element;
  • the map-based ...Large paths invoked the iteratee twice per element (once in the classification pass, once in the emission pass).

As a result the number of iteratee calls jumps discontinuously at the size threshold, and the large path does redundant work. The doc for the exported helpers states the iteratee "is invoked for each element", and findUniquesBySmall even says it matches "the single-invocation contract of the map-based path" — which the map path did not honor.

This change precomputes the keys once in findUniquesByLarge and findDuplicatesByLarge, so the iteratee is invoked exactly once per element regardless of collection size. Output is unchanged. The ...ByErr variants are intentionally left as-is (they have no small/large split and their tests rely on second-pass error behavior).

Repro on current master (n = collection size):

n iteratee calls expected
8 8 8
9 18 9
100 200 100

Related issues

None filed; found by inspection. Note this is the slice API in find.go. The same class of double-consumption in the separate it (iterator) package is handled by #1026 / #1032; those do not touch find.go, so there is no overlap with this PR.

Checklist

  • 👓 I have performed a self-review of my code
  • 🧪 This is tested (added TestFindUniquesBy_singleInvocation and TestFindDuplicatesBy_singleInvocation, asserting exactly-once on both sides of the boundary)

Tests

gofmt -l find.go find_test.go   # clean
go vet ./...                    # no issues
go test -p 1 -count=1 ./...     # all packages pass

Prepared with AI assistance and manually reviewed/verified locally.

…ement

The map-based paths (findUniquesByLarge/findDuplicatesByLarge) called the
iteratee twice per element - once to classify keys, once to emit results -
while the small-scan paths called it exactly once. This made the callback
count depend on whether the collection crossed findSmallThreshold, and did
redundant work on the common large path. Precompute the keys once, so the
iteratee is invoked exactly once per element regardless of collection size,
matching findUniquesBySmall/findDuplicatesBySmall and the public doc
("invoked for each element").

This branch has not been deployed

No deployments
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.

1 participant