8000
Skip to content

Add Map.DeleteMatching method for batch entry deletion#186

Merged
puzpuzpuz merged 4 commits intomainfrom
batch-delete
Jan 24, 2026
Merged

Add Map.DeleteMatching method for batch entry deletion#186
puzpuzpuz merged 4 commits intomainfrom
batch-delete

Conversation

@puzpuzpuz
Copy link
Copy Markdown
Owner
@puzpuzpuz puzpuzpuz commented Jan 24, 2026

Adds DeleteMatching method to Map. DeleteMatching deletes all entries for which the delete return value of the input function is true. If the cancel return value is true, the iteration stops immediately. The function returns the number of deleted entries.

This call locks a hash table bucket for the duration of evaluating the function for all entries in the bucket and performing deletions. It performs up to 20% faster than Range + Delete. On the other hand, it means that modifications on other entries in the bucket will be blocked until the function executes. Consider this when the function includes long-running operations.

m := xsync.NewMap[string, int]()
m.Store("alice", 10)
m.Store("bob", 20)
m.Store("carol", 30)
m.Store("dave", 40)

// Delete entries with value greater than 25.
deleted := m.DeleteMatching(func(key string, value int) (delete, cancel bool) {
	return value > 25, false
})

DeleteMatching vs Range+Delete benchmarks

Environment

  • OS: Linux
  • Arch: amd64
  • CPU: AMD Ryzen 9 7900 12-Core Processor

Results

Entries Delete % DeleteMatching Range+Delete Improvement
1K 10% 35.4 µs 36.7 µs ~3% faster
1K 50% 37.8 µs 44.1 µs 14% faster
1K 100% 37.3 µs 48.8 µs 24% faster
100K 10% 4.52 ms 4.32 ms ~same
100K 50% 4.80 ms 5.25 ms 9% faster
100K 100% 4.34 ms 6.15 ms 29% faster
1M 10% 102 ms 107 ms ~5% faster
1M 50% 110 ms 136 ms 19% faster
1M 100% 108 ms 151 ms 28% faster

@puzpuzpuz puzpuzpuz self-assigned this Jan 24, 2026
@puzpuzpuz puzpuzpuz added the enhancement New feature or request label Jan 24, 2026
@coderabbitai
Copy link
Copy Markdown
coderabbitai bot commented Jan 24, 2026

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

This pull request introduces a new DeleteMatching method to the Map type that enables selective deletion of entries based on a user-provided predicate function. The changes include updated documentation, implementation with concurrency handling, example usage, and comprehensive test coverage.

Changes

Cohort / File(s) Summary
Documentation & Examples
README.md, example_test.go
Updated README with details about Map's hash function (maphash.Comparable with per-instance seeds), cooperative rehashing, SwissTable reference, and DeleteMatching usage. Added example demonstrating predicate-based deletion with delete/cancel decision logic.
Implementation
map.go
Added public method DeleteMatching(f func(key K, value V) (delete, cancel bool)) int with per-bucket locking, concurrent resize handling via retry logic, bucket shrinking, and accumulation of total deletions.
Test Suite
map_test.go
Added comprehensive tests including basic deletion validation, cancellation behavior, edge cases (empty map, no deletions, all deleted), parallel concurrent scenarios with state verification, and benchmarks for deletion-matching and range-deletion patterns across varying entry counts and deletion percentages.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding a DeleteMatching method to Map for batch entry deletion.
Description check ✅ Passed The pull request description clearly and comprehensively explains the new DeleteMatching method, its behavior, performance characteristics, and includes a code example.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@puzpuzpuz
Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
coderabbitai bot commented Jan 24, 2026
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

@puzpuzpuz puzpuzpuz merged commit f818a1b into main Jan 24, 2026
5 checks passed
@puzpuzpuz puzpuzpuz deleted the batch-delete branch January 24, 2026 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0