perf: optimize RemoveDuplicateElement for small slices by knyk-dev · Pull Request #1701 · apache/casbin · GitHub
Skip to content

perf: optimize RemoveDuplicateElement for small slices - #1701

Closed
knyk-dev wants to merge 1 commit into
apache:masterfrom
knyk-dev:util-test
Closed

knyk-dev wants to merge 1 commit into
apache:masterfrom
knyk-dev:util-test

Conversation

@knyk-dev

Copy link
Copy Markdown
Contributor

This PR optimizes the RemoveDuplicateElement function in util/util.go to improve performance for small slices.

  • Introduces a hybrid approach using linear scan for small slices (<= 10 elements) and map-based deduplication for larger ones.
  • Avoids map allocation overhead for small inputs, significantly reducing operation time and memory usage.
  • Benchmarks demonstrate a 44.2% performance improvement for small slice operations: "BenchmarkProfile_Old-20 85.29 ns/op" vs "BenchmarkProfile_New-20 47.57 ns/op".
  • Maintains original logic for larger datasets to ensure scalability without regression.
  • Fixes linter warnings in util/util.go by adding missing periods to function comments.
  • Keeps util/util_test.go unchanged as no new tests were required to maintain existing coverage.

@github-actions

Copy link
Copy Markdown

@hsluoyz hsluoyz closed this Aug 4, 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.

2 participants