pax_global_header00006660000000000000000000000064147452066230014523gustar00rootroot0000000000000052 comment=6947fae1cf001fd7cb6ab8e2359cd667e7a57cc3 xsync-3.5.0/000077500000000000000000000000001474520662300126745ustar00rootroot00000000000000xsync-3.5.0/.github/000077500000000000000000000000001474520662300142345ustar00rootroot00000000000000xsync-3.5.0/.github/workflows/000077500000000000000000000000001474520662300162715ustar00rootroot00000000000000xsync-3.5.0/.github/workflows/build-1.18.yml000066400000000000000000000007521474520662300205040ustar00rootroot00000000000000name: build-1.18 on: [push] jobs: test: runs-on: ubuntu-latest strategy: matrix: go-version: [1.18.x] name: Build with Go ${{ matrix.go-version }} steps: - uses: actions/checkout@v3 - name: Install Go uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} - name: Run tests run: go test -v ./... - name: Run tests with race detector run: go test -timeout 10m -race -v ./... xsync-3.5.0/.github/workflows/build-32-bit.yml000066400000000000000000000007011474520662300211070ustar00rootroot00000000000000name: build-32-bit on: [push] jobs: test: runs-on: ubuntu-latest strategy: matrix: go-version: [1.19.x, 1.20.x, 1.21.x, 1.22.x, 1.23.x] name: Build with Go ${{ matrix.go-version }} 32-bit steps: - uses: actions/checkout@v3 - name: Install Go uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} - name: Run tests run: GOARCH=386 go test -v ./... xsync-3.5.0/.github/workflows/build.yml000066400000000000000000000010661474520662300201160ustar00rootroot00000000000000name: build on: [push] jobs: test: runs-on: ubuntu-latest strategy: matrix: go-version: [1.19.x, 1.20.x, 1.21.x, 1.22.x, 1.23.x] name: Build with Go ${{ matrix.go-version }} steps: - uses: actions/checkout@v3 - name: Install Go uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} - name: Run vet run: go vet ./... - name: Run tests run: go test -v ./... - name: Run tests with race detector run: go test -timeout 10m -race -v ./... xsync-3.5.0/.github/workflows/coverage.yml000066400000000000000000000010651474520662300206110ustar00rootroot00000000000000name: report-coverage on: [push] jobs: test: runs-on: ubuntu-latest strategy: matrix: go-version: [1.23.x] name: Build with Go ${{ matrix.go-version }} steps: - uses: actions/checkout@v3 - name: Install Go uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} - name: Run coverage run: go test -race -coverprofile=coverage.out -covermode=atomic - name: Publish to Codecov uses: codecov/codecov-action@v3 with: files: coverage.out xsync-3.5.0/.github/workflows/lint.yml000066400000000000000000000010261474520662300177610ustar00rootroot00000000000000name: lint on: [push] jobs: test: runs-on: ubuntu-latest strategy: matrix: go-version: [1.21.x] name: Build with Go ${{ matrix.go-version }} steps: - uses: actions/checkout@v3 - name: Install Go uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} - name: Run staticcheck uses: dominikh/staticcheck-action@v1.3.1 with: version: "2023.1.7" install-go: false cache-key: ${{ matrix.go-version }} xsync-3.5.0/.gitignore000066400000000000000000000004151474520662300146640ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Dependency directories (remove the comment below to include it) # vendor/ xsync-3.5.0/BENCHMARKS.md000066400000000000000000000160451474520662300146410ustar00rootroot00000000000000# xsync benchmarks If you're interested in `MapOf` comparison with some of the popular concurrent hash maps written in Go, check [this](https://github.com/cornelk/hashmap/pull/70) and [this](https://github.com/alphadose/haxmap/pull/22) PRs. The below results were obtained for xsync v2.3.1 on a c6g.metal EC2 instance (64 CPU, 128GB RAM) running Linux and Go 1.19.3. I'd like to thank [@felixge](https://github.com/felixge) who kindly ran the benchmarks. The following commands were used to run the benchmarks: ```bash $ go test -run='^$' -cpu=1,2,4,8,16,32,64 -bench . -count=30 -timeout=0 | tee bench.txt $ benchstat bench.txt | tee benchstat.txt ``` The below sections contain some of the results. Refer to [this gist](https://gist.github.com/puzpuzpuz/e62e38e06feadecfdc823c0f941ece0b) for the complete output. Please note that `MapOf` got a number of optimizations since v2.3.1, so the current result is likely to be different. ### Counter vs. atomic int64 ``` name time/op Counter 27.3ns ± 1% Counter-2 27.2ns ±11% Counter-4 15.3ns ± 8% Counter-8 7.43ns ± 7% Counter-16 3.70ns ±10% Counter-32 1.77ns ± 3% Counter-64 0.96ns ±10% AtomicInt64 7.60ns ± 0% AtomicInt64-2 12.6ns ±13% AtomicInt64-4 13.5ns ±14% AtomicInt64-8 12.7ns ± 9% AtomicInt64-16 12.8ns ± 8% AtomicInt64-32 13.0ns ± 6% AtomicInt64-64 12.9ns ± 7% ``` Here `time/op` stands for average time spent on operation. If you divide `10^9` by the result in nanoseconds per operation, you'd get the throughput in operations per second. Thus, the ideal theoretical scalability of a concurrent data structure implies that the reported `time/op` decreases proportionally with the increased number of CPU cores. On the contrary, if the measured time per operation increases when run on more cores, it means performance degradation. ### MapOf vs. sync.Map 1,000 `[int, int]` entries with a warm-up, 100% Loads: ``` IntegerMapOf_WarmUp/reads=100% 24.0ns ± 0% IntegerMapOf_WarmUp/reads=100%-2 12.0ns ± 0% IntegerMapOf_WarmUp/reads=100%-4 6.02ns ± 0% IntegerMapOf_WarmUp/reads=100%-8 3.01ns ± 0% IntegerMapOf_WarmUp/reads=100%-16 1.50ns ± 0% IntegerMapOf_WarmUp/reads=100%-32 0.75ns ± 0% IntegerMapOf_WarmUp/reads=100%-64 0.38ns ± 0% IntegerMapStandard_WarmUp/reads=100% 55.3ns ± 0% IntegerMapStandard_WarmUp/reads=100%-2 27.6ns ± 0% IntegerMapStandard_WarmUp/reads=100%-4 16.1ns ± 3% IntegerMapStandard_WarmUp/reads=100%-8 8.35ns ± 7% IntegerMapStandard_WarmUp/reads=100%-16 4.24ns ± 7% IntegerMapStandard_WarmUp/reads=100%-32 2.18ns ± 6% IntegerMapStandard_WarmUp/reads=100%-64 1.11ns ± 3% ``` 1,000 `[int, int]` entries with a warm-up, 99% Loads, 0.5% Stores, 0.5% Deletes: ``` IntegerMapOf_WarmUp/reads=99% 31.0ns ± 0% IntegerMapOf_WarmUp/reads=99%-2 16.4ns ± 1% IntegerMapOf_WarmUp/reads=99%-4 8.42ns ± 0% IntegerMapOf_WarmUp/reads=99%-8 4.41ns ± 0% IntegerMapOf_WarmUp/reads=99%-16 2.38ns ± 2% IntegerMapOf_WarmUp/reads=99%-32 1.37ns ± 4% IntegerMapOf_WarmUp/reads=99%-64 0.85ns ± 2% IntegerMapStandard_WarmUp/reads=99% 121ns ± 1% IntegerMapStandard_WarmUp/reads=99%-2 109ns ± 3% IntegerMapStandard_WarmUp/reads=99%-4 115ns ± 4% IntegerMapStandard_WarmUp/reads=99%-8 114ns ± 2% IntegerMapStandard_WarmUp/reads=99%-16 105ns ± 2% IntegerMapStandard_WarmUp/reads=99%-32 97.0ns ± 3% IntegerMapStandard_WarmUp/reads=99%-64 98.0ns ± 2% ``` 1,000 `[int, int]` entries with a warm-up, 75% Loads, 12.5% Stores, 12.5% Deletes: ``` IntegerMapOf_WarmUp/reads=75%-reads 46.2ns ± 1% IntegerMapOf_WarmUp/reads=75%-reads-2 36.7ns ± 2% IntegerMapOf_WarmUp/reads=75%-reads-4 22.0ns ± 1% IntegerMapOf_WarmUp/reads=75%-reads-8 12.8ns ± 2% IntegerMapOf_WarmUp/reads=75%-reads-16 7.69ns ± 1% IntegerMapOf_WarmUp/reads=75%-reads-32 5.16ns ± 1% IntegerMapOf_WarmUp/reads=75%-reads-64 4.91ns ± 1% IntegerMapStandard_WarmUp/reads=75%-reads 156ns ± 0% IntegerMapStandard_WarmUp/reads=75%-reads-2 177ns ± 1% IntegerMapStandard_WarmUp/reads=75%-reads-4 197ns ± 1% IntegerMapStandard_WarmUp/reads=75%-reads-8 221ns ± 2% IntegerMapStandard_WarmUp/reads=75%-reads-16 242ns ± 1% IntegerMapStandard_WarmUp/reads=75%-reads-32 258ns ± 1% IntegerMapStandard_WarmUp/reads=75%-reads-64 264ns ± 1% ``` ### MPMCQueue vs. Go channels Concurrent producers and consumers (1:1), queue/channel size 1,000, some work done by both producers and consumers: ``` QueueProdConsWork100 252ns ± 0% QueueProdConsWork100-2 206ns ± 5% QueueProdConsWork100-4 136ns ±12% QueueProdConsWork100-8 110ns ± 6% QueueProdConsWork100-16 108ns ± 2% QueueProdConsWork100-32 102ns ± 2% QueueProdConsWork100-64 101ns ± 0% ChanProdConsWork100 283ns ± 0% ChanProdConsWork100-2 406ns ±21% ChanProdConsWork100-4 549ns ± 7% ChanProdConsWork100-8 754ns ± 7% ChanProdConsWork100-16 828ns ± 7% ChanProdConsWork100-32 810ns ± 8% ChanProdConsWork100-64 832ns ± 4% ``` ### RBMutex vs. sync.RWMutex The writer locks on each 100,000 iteration with some work in the critical section for both readers and the writer: ``` RBMutexWorkWrite100000 146ns ± 0% RBMutexWorkWrite100000-2 73.3ns ± 0% RBMutexWorkWrite100000-4 36.7ns ± 0% RBMutexWorkWrite100000-8 18.6ns ± 0% RBMutexWorkWrite100000-16 9.83ns ± 3% RBMutexWorkWrite100000-32 5.53ns ± 0% RBMutexWorkWrite100000-64 4.04ns ± 3% RWMutexWorkWrite100000 121ns ± 0% RWMutexWorkWrite100000-2 128ns ± 1% RWMutexWorkWrite100000-4 124ns ± 2% RWMutexWorkWrite100000-8 101ns ± 1% RWMutexWorkWrite100000-16 92.9ns ± 1% RWMutexWorkWrite100000-32 89.9ns ± 1% RWMutexWorkWrite100000-64 88.4ns ± 1% ``` xsync-3.5.0/LICENSE000066400000000000000000000261351474520662300137100ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. xsync-3.5.0/README.md000066400000000000000000000224771474520662300141670ustar00rootroot00000000000000[![GoDoc reference](https://img.shields.io/badge/godoc-reference-blue.svg)](https://pkg.go.dev/github.com/puzpuzpuz/xsync/v3) [![GoReport](https://goreportcard.com/badge/github.com/puzpuzpuz/xsync/v3)](https://goreportcard.com/report/github.com/puzpuzpuz/xsync/v3) [![codecov](https://codecov.io/gh/puzpuzpuz/xsync/branch/main/graph/badge.svg)](https://codecov.io/gh/puzpuzpuz/xsync) # xsync Concurrent data structures for Go. Aims to provide more scalable alternatives for some of the data structures from the standard `sync` package, but not only. Covered with tests following the approach described [here](https://puzpuzpuz.dev/testing-concurrent-code-for-fun-and-profit). ## Benchmarks Benchmark results may be found [here](BENCHMARKS.md). I'd like to thank [@felixge](https://github.com/felixge) who kindly ran the benchmarks on a beefy multicore machine. Also, a non-scientific, unfair benchmark comparing Java's [j.u.c.ConcurrentHashMap](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/ConcurrentHashMap.html) and `xsync.MapOf` is available [here](https://puzpuzpuz.dev/concurrent-map-in-go-vs-java-yet-another-meaningless-benchmark). ## Usage The latest xsync major version is v3, so `/v3` suffix should be used when importing the library: ```go import ( "github.com/puzpuzpuz/xsync/v3" ) ``` *Note for pre-v3 users*: v1 and v2 support is discontinued, so please upgrade to v3. While the API has some breaking changes, the migration should be trivial. ### Counter A `Counter` is a striped `int64` counter inspired by the `j.u.c.a.LongAdder` class from the Java standard library. ```go c := xsync.NewCounter() // increment and decrement the counter c.Inc() c.Dec() // read the current value v := c.Value() ``` Works better in comparison with a single atomically updated `int64` counter in high contention scenarios. ### Map A `Map` is like a concurrent hash table-based map. It follows the interface of `sync.Map` with a number of valuable extensions like `Compute` or `Size`. ```go m := xsync.NewMap() m.Store("foo", "bar") v, ok := m.Load("foo") s := m.Size() ``` `Map` uses a modified version of Cache-Line Hash Table (CLHT) data structure: https://github.com/LPD-EPFL/CLHT CLHT is built around the idea of organizing the hash table in cache-line-sized buckets, so that on all modern CPUs update operations complete with minimal cache-line transfer. Also, `Get` operations are obstruction-free and involve no writes to shared memory, hence no mutexes or any other sort of locks. Due to this design, in all considered scenarios `Map` outperforms `sync.Map`. One important difference with `sync.Map` is that only string keys are supported. That's because Golang standard library does not expose the built-in hash functions for `interface{}` values. `MapOf[K, V]` is an implementation with parametrized key and value types. While it's still a CLHT-inspired hash map, `MapOf`'s design is quite different from `Map`. As a result, less GC pressure and fewer atomic operations on reads. ```go m := xsync.NewMapOf[string, string]() m.Store("foo", "bar") v, ok := m.Load("foo") ``` Apart from CLHT, `MapOf` borrows ideas from Java's `j.u.c.ConcurrentHashMap` (immutable K/V pair structs instead of atomic snapshots) and C++'s `absl::flat_hash_map` (meta memory and SWAR-based lookups). It also has more dense memory layout when compared with `Map`. Long story short, `MapOf` should be preferred over `Map` when possible. An important difference with `Map` is that `MapOf` supports arbitrary `comparable` key types: ```go type Point struct { x int32 y int32 } m := NewMapOf[Point, int]() m.Store(Point{42, 42}, 42) v, ok := m.Load(point{42, 42}) ``` Apart from `Range` method available for map iteration, there are also `ToPlainMap`/`ToPlainMapOf` utility functions to convert a `Map`/`MapOf` to a built-in Go's `map`: ```go m := xsync.NewMapOf[int, int]() m.Store(42, 42) pm := xsync.ToPlainMapOf(m) ``` Both `Map` and `MapOf` use the built-in Golang's hash function which has DDOS protection. This means that each map instance gets its own seed number and the hash function uses that seed for hash code calculation. However, for smaller keys this hash function has some overhead. So, if you don't need DDOS protection, you may provide a custom hash function when creating a `MapOf`. For instance, Murmur3 finalizer does a decent job when it comes to integers: ```go m := NewMapOfWithHasher[int, int](func(i int, _ uint64) uint64 { h := uint64(i) h = (h ^ (h >> 33)) * 0xff51afd7ed558ccd h = (h ^ (h >> 33)) * 0xc4ceb9fe1a85ec53 return h ^ (h >> 33) }) ``` When benchmarking concurrent maps, make sure to configure all of the competitors with the same hash function or, at least, take hash function performance into the consideration. ### SPSCQueue A `SPSCQueue` is a bounded single-producer single-consumer concurrent queue. This means that not more than a single goroutine must be publishing items to the queue while not more than a single goroutine must be consuming those items. ```go q := xsync.NewSPSCQueue(1024) // producer inserts an item into the queue // optimistic insertion attempt; doesn't block inserted := q.TryEnqueue("bar") // consumer obtains an item from the queue // optimistic obtain attempt; doesn't block item, ok := q.TryDequeue() // interface{} pointing to a string ``` `SPSCQueueOf[I]` is an implementation with parametrized item type. It is available for Go 1.19 or later. ```go q := xsync.NewSPSCQueueOf[string](1024) inserted := q.TryEnqueue("foo") item, ok := q.TryDequeue() // string ``` The queue is based on the data structure from this [article](https://rigtorp.se/ringbuffer). The idea is to reduce the CPU cache coherency traffic by keeping cached copies of read and write indexes used by producer and consumer respectively. ### MPMCQueue A `MPMCQueue` is a bounded multi-producer multi-consumer concurrent queue. ```go q := xsync.NewMPMCQueue(1024) // producer optimistically inserts an item into the queue // optimistic insertion attempt; doesn't block inserted := q.TryEnqueue("bar") // consumer obtains an item from the queue // optimistic obtain attempt; doesn't block item, ok := q.TryDequeue() // interface{} pointing to a string ``` `MPMCQueueOf[I]` is an implementation with parametrized item type. It is available for Go 1.19 or later. ```go q := xsync.NewMPMCQueueOf[string](1024) inserted := q.TryEnqueue("foo") item, ok := q.TryDequeue() // string ``` The queue is based on the algorithm from the [MPMCQueue](https://github.com/rigtorp/MPMCQueue) C++ library which in its turn references D.Vyukov's [MPMC queue](https://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue). According to the following [classification](https://www.1024cores.net/home/lock-free-algorithms/queues), the queue is array-based, fails on overflow, provides causal FIFO, has blocking producers and consumers. The idea of the algorithm is to allow parallelism for concurrent producers and consumers by introducing the notion of tickets, i.e. values of two counters, one per producers/consumers. An atomic increment of one of those counters is the only noticeable contention point in queue operations. The rest of the operation avoids contention on writes thanks to the turn-based read/write access for each of the queue items. In essence, `MPMCQueue` is a specialized queue for scenarios where there are multiple concurrent producers and consumers of a single queue running on a large multicore machine. To get the optimal performance, you may want to set the queue size to be large enough, say, an order of magnitude greater than the number of producers/consumers, to allow producers and consumers to progress with their queue operations in parallel most of the time. ### RBMutex A `RBMutex` is a reader-biased reader/writer mutual exclusion lock. The lock can be held by many readers or a single writer. ```go mu := xsync.NewRBMutex() // reader lock calls return a token t := mu.RLock() // the token must be later used to unlock the mutex mu.RUnlock(t) // writer locks are the same as in sync.RWMutex mu.Lock() mu.Unlock() ``` `RBMutex` is based on a modified version of BRAVO (Biased Locking for Reader-Writer Locks) algorithm: https://arxiv.org/pdf/1810.01553.pdf The idea of the algorithm is to build on top of an existing reader-writer mutex and introduce a fast path for readers. On the fast path, reader lock attempts are sharded over an internal array based on the reader identity (a token in the case of Golang). This means that readers do not contend over a single atomic counter like it's done in, say, `sync.RWMutex` allowing for better scalability in terms of cores. Hence, by the design `RBMutex` is a specialized mutex for scenarios, such as caches, where the vast majority of locks are acquired by readers and write lock acquire attempts are infrequent. In such scenarios, `RBMutex` should perform better than the `sync.RWMutex` on large multicore machines. `RBMutex` extends `sync.RWMutex` internally and uses it as the "reader bias disabled" fallback, so the same semantics apply. The only noticeable difference is in the reader tokens returned from the `RLock`/`RUnlock` methods. Apart from blocking methods, `RBMutex` also has methods for optimistic locking: ```go mu := xsync.NewRBMutex() if locked, t := mu.TryRLock(); locked { // critical reader section... mu.RUnlock(t) } if mu.TryLock() { // critical writer section... mu.Unlock() } ``` ## License Licensed under MIT. xsync-3.5.0/counter.go000066400000000000000000000043771474520662300147150ustar00rootroot00000000000000package xsync import ( "sync" "sync/atomic" ) // pool for P tokens var ptokenPool sync.Pool // a P token is used to point at the current OS thread (P) // on which the goroutine is run; exact identity of the thread, // as well as P migration tolerance, is not important since // it's used to as a best effort mechanism for assigning // concurrent operations (goroutines) to different stripes of // the counter type ptoken struct { idx uint32 //lint:ignore U1000 prevents false sharing pad [cacheLineSize - 4]byte } // A Counter is a striped int64 counter. // // Should be preferred over a single atomically updated int64 // counter in high contention scenarios. // // A Counter must not be copied after first use. type Counter struct { stripes []cstripe mask uint32 } type cstripe struct { c int64 //lint:ignore U1000 prevents false sharing pad [cacheLineSize - 8]byte } // NewCounter creates a new Counter instance. func NewCounter() *Counter { nstripes := nextPowOf2(parallelism()) c := Counter{ stripes: make([]cstripe, nstripes), mask: nstripes - 1, } return &c } // Inc increments the counter by 1. func (c *Counter) Inc() { c.Add(1) } // Dec decrements the counter by 1. func (c *Counter) Dec() { c.Add(-1) } // Add adds the delta to the counter. func (c *Counter) Add(delta int64) { t, ok := ptokenPool.Get().(*ptoken) if !ok { t = new(ptoken) t.idx = runtime_fastrand() } for { stripe := &c.stripes[t.idx&c.mask] cnt := atomic.LoadInt64(&stripe.c) if atomic.CompareAndSwapInt64(&stripe.c, cnt, cnt+delta) { break } // Give a try with another randomly selected stripe. t.idx = runtime_fastrand() } ptokenPool.Put(t) } // Value returns the current counter value. // The returned value may not include all of the latest operations in // presence of concurrent modifications of the counter. func (c *Counter) Value() int64 { v := int64(0) for i := 0; i < len(c.stripes); i++ { stripe := &c.stripes[i] v += atomic.LoadInt64(&stripe.c) } return v } // Reset resets the counter to zero. // This method should only be used when it is known that there are // no concurrent modifications of the counter. func (c *Counter) Reset() { for i := 0; i < len(c.stripes); i++ { stripe := &c.stripes[i] atomic.StoreInt64(&stripe.c, 0) } } xsync-3.5.0/counter_test.go000066400000000000000000000045151474520662300157460ustar00rootroot00000000000000package xsync_test import ( "runtime" "sync/atomic" "testing" . "github.com/puzpuzpuz/xsync/v3" ) func TestCounterInc(t *testing.T) { c := NewCounter() for i := 0; i < 100; i++ { if v := c.Value(); v != int64(i) { t.Fatalf("got %v, want %d", v, i) } c.Inc() } } func TestCounterDec(t *testing.T) { c := NewCounter() for i := 0; i < 100; i++ { if v := c.Value(); v != int64(-i) { t.Fatalf("got %v, want %d", v, -i) } c.Dec() } } func TestCounterAdd(t *testing.T) { c := NewCounter() for i := 0; i < 100; i++ { if v := c.Value(); v != int64(i*42) { t.Fatalf("got %v, want %d", v, i*42) } c.Add(42) } } func TestCounterReset(t *testing.T) { c := NewCounter() c.Add(42) if v := c.Value(); v != 42 { t.Fatalf("got %v, want %d", v, 42) } c.Reset() if v := c.Value(); v != 0 { t.Fatalf("got %v, want %d", v, 0) } } func parallelIncrementor(c *Counter, numIncs int, cdone chan bool) { for i := 0; i < numIncs; i++ { c.Inc() } cdone <- true } func doTestParallelIncrementors(t *testing.T, numModifiers, gomaxprocs int) { runtime.GOMAXPROCS(gomaxprocs) c := NewCounter() cdone := make(chan bool) numIncs := 10_000 for i := 0; i < numModifiers; i++ { go parallelIncrementor(c, numIncs, cdone) } // Wait for the goroutines to finish. for i := 0; i < numModifiers; i++ { <-cdone } expected := int64(numModifiers * numIncs) if v := c.Value(); v != expected { t.Fatalf("got %d, want %d", v, expected) } } func TestCounterParallelIncrementors(t *testing.T) { defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(-1)) doTestParallelIncrementors(t, 4, 2) doTestParallelIncrementors(t, 16, 4) doTestParallelIncrementors(t, 64, 8) } func benchmarkCounter(b *testing.B, writeRatio int) { c := NewCounter() runParallel(b, func(pb *testing.PB) { foo := 0 for pb.Next() { foo++ if writeRatio > 0 && foo%writeRatio == 0 { c.Value() } else { c.Inc() } } _ = foo }) } func BenchmarkCounter(b *testing.B) { benchmarkCounter(b, 10000) } func benchmarkAtomicInt64(b *testing.B, writeRatio int) { var c int64 runParallel(b, func(pb *testing.PB) { foo := 0 for pb.Next() { foo++ if writeRatio > 0 && foo%writeRatio == 0 { atomic.LoadInt64(&c) } else { atomic.AddInt64(&c, 1) } } _ = foo }) } func BenchmarkAtomicInt64(b *testing.B) { benchmarkAtomicInt64(b, 10000) } xsync-3.5.0/example_test.go000066400000000000000000000032451474520662300157210ustar00rootroot00000000000000package xsync_test import ( "errors" "fmt" "github.com/puzpuzpuz/xsync/v3" ) func ExampleMapOf_Compute() { counts := xsync.NewMapOf[int, int]() // Store a new value. v, ok := counts.Compute(42, func(oldValue int, loaded bool) (newValue int, delete bool) { // loaded is false here. newValue = 42 delete = false return }) // v: 42, ok: true fmt.Printf("v: %v, ok: %v\n", v, ok) // Update an existing value. v, ok = counts.Compute(42, func(oldValue int, loaded bool) (newValue int, delete bool) { // loaded is true here. newValue = oldValue + 42 delete = false return }) // v: 84, ok: true fmt.Printf("v: %v, ok: %v\n", v, ok) // Set a new value or keep the old value conditionally. var oldVal int minVal := 63 v, ok = counts.Compute(42, func(oldValue int, loaded bool) (newValue int, delete bool) { oldVal = oldValue if !loaded || oldValue < minVal { newValue = minVal delete = false return } newValue = oldValue delete = false return }) // v: 84, ok: true, oldVal: 84 fmt.Printf("v: %v, ok: %v, oldVal: %v\n", v, ok, oldVal) // Delete an existing value. v, ok = counts.Compute(42, func(oldValue int, loaded bool) (newValue int, delete bool) { // loaded is true here. delete = true return }) // v: 84, ok: false fmt.Printf("v: %v, ok: %v\n", v, ok) // Propagate an error from the compute function to the outer scope. var err error v, ok = counts.Compute(42, func(oldValue int, loaded bool) (newValue int, delete bool) { if oldValue == 42 { err = errors.New("something went wrong") return 0, true // no need to create a key/value pair } newValue = 0 delete = false return }) fmt.Printf("err: %v\n", err) } xsync-3.5.0/export_test.go000066400000000000000000000031021474520662300155770ustar00rootroot00000000000000package xsync const ( EntriesPerMapBucket = entriesPerMapBucket EntriesPerMapOfBucket = entriesPerMapOfBucket MapLoadFactor = mapLoadFactor DefaultMinMapTableLen = defaultMinMapTableLen DefaultMinMapTableCap = defaultMinMapTableLen * entriesPerMapBucket DefaultMinMapOfTableCap = defaultMinMapTableLen * entriesPerMapOfBucket MaxMapCounterLen = maxMapCounterLen ) type ( BucketPadded = bucketPadded BucketOfPadded = bucketOfPadded ) func LockBucket(mu *uint64) { lockBucket(mu) } func UnlockBucket(mu *uint64) { unlockBucket(mu) } func TopHashMatch(hash, topHashes uint64, idx int) bool { return topHashMatch(hash, topHashes, idx) } func StoreTopHash(hash, topHashes uint64, idx int) uint64 { return storeTopHash(hash, topHashes, idx) } func EraseTopHash(topHashes uint64, idx int) uint64 { return eraseTopHash(topHashes, idx) } func EnableAssertions() { assertionsEnabled = true } func DisableAssertions() { assertionsEnabled = false } func Fastrand() uint32 { return runtime_fastrand() } func Broadcast(b uint8) uint64 { return broadcast(b) } func FirstMarkedByteIndex(w uint64) int { return firstMarkedByteIndex(w) } func MarkZeroBytes(w uint64) uint64 { return markZeroBytes(w) } func SetByte(w uint64, b uint8, idx int) uint64 { return setByte(w, b, idx) } func NextPowOf2(v uint32) uint32 { return nextPowOf2(v) } func MakeSeed() uint64 { return makeSeed() } func HashString(s string, seed uint64) uint64 { return hashString(s, seed) } func DefaultHasher[T comparable]() func(T, uint64) uint64 { return defaultHasher[T]() } xsync-3.5.0/go.mod000066400000000000000000000000561474520662300140030ustar00rootroot00000000000000module github.com/puzpuzpuz/xsync/v3 go 1.18 xsync-3.5.0/map.go000066400000000000000000000660011474520662300140030ustar00rootroot00000000000000package xsync import ( "fmt" "math" "runtime" "strings" "sync" "sync/atomic" "unsafe" ) type mapResizeHint int const ( mapGrowHint mapResizeHint = 0 mapShrinkHint mapResizeHint = 1 mapClearHint mapResizeHint = 2 ) const ( // number of Map entries per bucket; 3 entries lead to size of 64B // (one cache line) on 64-bit machines entriesPerMapBucket = 3 // threshold fraction of table occupation to start a table shrinking // when deleting the last entry in a bucket chain mapShrinkFraction = 128 // map load factor to trigger a table resize during insertion; // a map holds up to mapLoadFactor*entriesPerMapBucket*mapTableLen // key-value pairs (this is a soft limit) mapLoadFactor = 0.75 // minimal table size, i.e. number of buckets; thus, minimal map // capacity can be calculated as entriesPerMapBucket*defaultMinMapTableLen defaultMinMapTableLen = 32 // minimum counter stripes to use minMapCounterLen = 8 // maximum counter stripes to use; stands for around 4KB of memory maxMapCounterLen = 32 ) var ( topHashMask = uint64((1<<20)-1) << 44 topHashEntryMasks = [3]uint64{ topHashMask, topHashMask >> 20, topHashMask >> 40, } ) // Map is like a Go map[string]interface{} but is safe for concurrent // use by multiple goroutines without additional locking or // coordination. It follows the interface of sync.Map with // a number of valuable extensions like Compute or Size. // // A Map must not be copied after first use. // // Map uses a modified version of Cache-Line Hash Table (CLHT) // data structure: https://github.com/LPD-EPFL/CLHT // // CLHT is built around idea to organize the hash table in // cache-line-sized buckets, so that on all modern CPUs update // operations complete with at most one cache-line transfer. // Also, Get operations involve no write to memory, as well as no // mutexes or any other sort of locks. Due to this design, in all // considered scenarios Map outperforms sync.Map. // // One important difference with sync.Map is that only string keys // are supported. That's because Golang standard library does not // expose the built-in hash functions for interface{} values. type Map struct { totalGrowths int64 totalShrinks int64 resizing int64 // resize in progress flag; updated atomically resizeMu sync.Mutex // only used along with resizeCond resizeCond sync.Cond // used to wake up resize waiters (concurrent modifications) table unsafe.Pointer // *mapTable minTableLen int growOnly bool } type mapTable struct { buckets []bucketPadded // striped counter for number of table entries; // used to determine if a table shrinking is needed // occupies min(buckets_memory/1024, 64KB) of memory size []counterStripe seed uint64 } type counterStripe struct { c int64 //lint:ignore U1000 prevents false sharing pad [cacheLineSize - 8]byte } type bucketPadded struct { //lint:ignore U1000 ensure each bucket takes two cache lines on both 32 and 64-bit archs pad [cacheLineSize - unsafe.Sizeof(bucket{})]byte bucket } type bucket struct { next unsafe.Pointer // *bucketPadded keys [entriesPerMapBucket]unsafe.Pointer values [entriesPerMapBucket]unsafe.Pointer // topHashMutex is a 2-in-1 value. // // It contains packed top 20 bits (20 MSBs) of hash codes for keys // stored in the bucket: // | key 0's top hash | key 1's top hash | key 2's top hash | bitmap for keys | mutex | // | 20 bits | 20 bits | 20 bits | 3 bits | 1 bit | // // The least significant bit is used for the mutex (TTAS spinlock). topHashMutex uint64 } type rangeEntry struct { key unsafe.Pointer value unsafe.Pointer } // MapConfig defines configurable Map/MapOf options. type MapConfig struct { sizeHint int growOnly bool } // WithPresize configures new Map/MapOf instance with capacity enough // to hold sizeHint entries. The capacity is treated as the minimal // capacity meaning that the underlying hash table will never shrink // to a smaller capacity. If sizeHint is zero or negative, the value // is ignored. func WithPresize(sizeHint int) func(*MapConfig) { return func(c *MapConfig) { c.sizeHint = sizeHint } } // WithGrowOnly configures new Map/MapOf instance to be grow-only. // This means that the underlying hash table grows in capacity when // new keys are added, but does not shrink when keys are deleted. // The only exception to this rule is the Clear method which // shrinks the hash table back to the initial capacity. func WithGrowOnly() func(*MapConfig) { return func(c *MapConfig) { c.growOnly = true } } // NewMap creates a new Map instance configured with the given // options. func NewMap(options ...func(*MapConfig)) *Map { c := &MapConfig{ sizeHint: defaultMinMapTableLen * entriesPerMapBucket, } for _, o := range options { o(c) } m := &Map{} m.resizeCond = *sync.NewCond(&m.resizeMu) var table *mapTable if c.sizeHint <= defaultMinMapTableLen*entriesPerMapBucket { table = newMapTable(defaultMinMapTableLen) } else { tableLen := nextPowOf2(uint32((float64(c.sizeHint) / entriesPerMapBucket) / mapLoadFactor)) table = newMapTable(int(tableLen)) } m.minTableLen = len(table.buckets) m.growOnly = c.growOnly atomic.StorePointer(&m.table, unsafe.Pointer(table)) return m } // NewMapPresized creates a new Map instance with capacity enough to hold // sizeHint entries. The capacity is treated as the minimal capacity // meaning that the underlying hash table will never shrink to // a smaller capacity. If sizeHint is zero or negative, the value // is ignored. // // Deprecated: use NewMap in combination with WithPresize. func NewMapPresized(sizeHint int) *Map { return NewMap(WithPresize(sizeHint)) } func newMapTable(minTableLen int) *mapTable { buckets := make([]bucketPadded, minTableLen) counterLen := minTableLen >> 10 if counterLen < minMapCounterLen { counterLen = minMapCounterLen } else if counterLen > maxMapCounterLen { counterLen = maxMapCounterLen } counter := make([]counterStripe, counterLen) t := &mapTable{ buckets: buckets, size: counter, seed: makeSeed(), } return t } // ToPlainMap returns a native map with a copy of xsync Map's // contents. The copied xsync Map should not be modified while // this call is made. If the copied Map is modified, the copying // behavior is the same as in the Range method. func ToPlainMap(m *Map) map[string]interface{} { pm := make(map[string]interface{}) if m != nil { m.Range(func(key string, value interface{}) bool { pm[key] = value return true }) } return pm } // Load returns the value stored in the map for a key, or nil if no // value is present. // The ok result indicates whether value was found in the map. func (m *Map) Load(key string) (value interface{}, ok bool) { table := (*mapTable)(atomic.LoadPointer(&m.table)) hash := hashString(key, table.seed) bidx := uint64(len(table.buckets)-1) & hash b := &table.buckets[bidx] for { topHashes := atomic.LoadUint64(&b.topHashMutex) for i := 0; i < entriesPerMapBucket; i++ { if !topHashMatch(hash, topHashes, i) { continue } atomic_snapshot: // Start atomic snapshot. vp := atomic.LoadPointer(&b.values[i]) kp := atomic.LoadPointer(&b.keys[i]) if kp != nil && vp != nil { if key == derefKey(kp) { if uintptr(vp) == uintptr(atomic.LoadPointer(&b.values[i])) { // Atomic snapshot succeeded. return derefValue(vp), true } // Concurrent update/remove. Go for another spin. goto atomic_snapshot } } } bptr := atomic.LoadPointer(&b.next) if bptr == nil { return } b = (*bucketPadded)(bptr) } } // Store sets the value for a key. func (m *Map) Store(key string, value interface{}) { m.doCompute( key, func(interface{}, bool) (interface{}, bool) { return value, false }, false, false, ) } // LoadOrStore returns the existing value for the key if present. // Otherwise, it stores and returns the given value. // The loaded result is true if the value was loaded, false if stored. func (m *Map) LoadOrStore(key string, value interface{}) (actual interface{}, loaded bool) { return m.doCompute( key, func(interface{}, bool) (interface{}, bool) { return value, false }, true, false, ) } // LoadAndStore returns the existing value for the key if present, // while setting the new value for the key. // It stores the new value and returns the existing one, if present. // The loaded result is true if the existing value was loaded, // false otherwise. func (m *Map) LoadAndStore(key string, value interface{}) (actual interface{}, loaded bool) { return m.doCompute( key, func(interface{}, bool) (interface{}, bool) { return value, false }, false, false, ) } // LoadOrTryCompute returns the existing value for the key if present. // Otherwise, it tries to compute the value using the provided function // and, if success, returns the computed value. The loaded result is true // if the value was loaded, false if stored. If the compute attempt was // cancelled, a nil will be returned. // // This call locks a hash table bucket while the compute function // is executed. It means that modifications on other entries in // the bucket will be blocked until the valueFn executes. Consider // this when the function includes long-running operations. func (m *Map) LoadOrTryCompute( key string, valueFn func() (newValue interface{}, cancel bool), ) (value interface{}, loaded bool) { return m.doCompute( key, func(interface{}, bool) (interface{}, bool) { nv, c := valueFn() if !c { return nv, false } return nil, true }, true, false, ) } // LoadOrCompute returns the existing value for the key if present. // Otherwise, it computes the value using the provided function and // returns the computed value. The loaded result is true if the value // was loaded, false if stored. // // This call locks a hash table bucket while the compute function // is executed. It means that modifications on other entries in // the bucket will be blocked until the valueFn executes. Consider // this when the function includes long-running operations. func (m *Map) LoadOrCompute(key string, valueFn func() interface{}) (actual interface{}, loaded bool) { return m.doCompute( key, func(interface{}, bool) (interface{}, bool) { return valueFn(), false }, true, false, ) } // Compute either sets the computed new value for the key or deletes // the value for the key. When the delete result of the valueFn function // is set to true, the value will be deleted, if it exists. When delete // is set to false, the value is updated to the newValue. // The ok result indicates whether value was computed and stored, thus, is // present in the map. The actual result contains the new value in cases where // the value was computed and stored. See the example for a few use cases. // // This call locks a hash table bucket while the compute function // is executed. It means that modifications on other entries in // the bucket will be blocked until the valueFn executes. Consider // this when the function includes long-running operations. func (m *Map) Compute( key string, valueFn func(oldValue interface{}, loaded bool) (newValue interface{}, delete bool), ) (actual interface{}, ok bool) { return m.doCompute(key, valueFn, false, true) } // LoadAndDelete deletes the value for a key, returning the previous // value if any. The loaded result reports whether the key was // present. func (m *Map) LoadAndDelete(key string) (value interface{}, loaded bool) { return m.doCompute( key, func(value interface{}, loaded bool) (interface{}, bool) { return value, true }, false, false, ) } // Delete deletes the value for a key. func (m *Map) Delete(key string) { m.doCompute( key, func(value interface{}, loaded bool) (interface{}, bool) { return value, true }, false, false, ) } func (m *Map) doCompute( key string, valueFn func(oldValue interface{}, loaded bool) (interface{}, bool), loadIfExists, computeOnly bool, ) (interface{}, bool) { // Read-only path. if loadIfExists { if v, ok := m.Load(key); ok { return v, !computeOnly } } // Write path. for { compute_attempt: var ( emptyb *bucketPadded emptyidx int hintNonEmpty int ) table := (*mapTable)(atomic.LoadPointer(&m.table)) tableLen := len(table.buckets) hash := hashString(key, table.seed) bidx := uint64(len(table.buckets)-1) & hash rootb := &table.buckets[bidx] lockBucket(&rootb.topHashMutex) // The following two checks must go in reverse to what's // in the resize method. if m.resizeInProgress() { // Resize is in progress. Wait, then go for another attempt. unlockBucket(&rootb.topHashMutex) m.waitForResize() goto compute_attempt } if m.newerTableExists(table) { // Someone resized the table. Go for another attempt. unlockBucket(&rootb.topHashMutex) goto compute_attempt } b := rootb for { topHashes := atomic.LoadUint64(&b.topHashMutex) for i := 0; i < entriesPerMapBucket; i++ { if b.keys[i] == nil { if emptyb == nil { emptyb = b emptyidx = i } continue } if !topHashMatch(hash, topHashes, i) { hintNonEmpty++ continue } if key == derefKey(b.keys[i]) { vp := b.values[i] if loadIfExists { unlockBucket(&rootb.topHashMutex) return derefValue(vp), !computeOnly } // In-place update/delete. // We get a copy of the value via an interface{} on each call, // thus the live value pointers are unique. Otherwise atomic // snapshot won't be correct in case of multiple Store calls // using the same value. oldValue := derefValue(vp) newValue, del := valueFn(oldValue, true) if del { // Deletion. // First we update the value, then the key. // This is important for atomic snapshot states. atomic.StoreUint64(&b.topHashMutex, eraseTopHash(topHashes, i)) atomic.StorePointer(&b.values[i], nil) atomic.StorePointer(&b.keys[i], nil) leftEmpty := false if hintNonEmpty == 0 { leftEmpty = isEmptyBucket(b) } unlockBucket(&rootb.topHashMutex) table.addSize(bidx, -1) // Might need to shrink the table. if leftEmpty { m.resize(table, mapShrinkHint) } return oldValue, !computeOnly } nvp := unsafe.Pointer(&newValue) if assertionsEnabled && vp == nvp { panic("non-unique value pointer") } atomic.StorePointer(&b.values[i], nvp) unlockBucket(&rootb.topHashMutex) if computeOnly { // Compute expects the new value to be returned. return newValue, true } // LoadAndStore expects the old value to be returned. return oldValue, true } hintNonEmpty++ } if b.next == nil { if emptyb != nil { // Insertion into an existing bucket. var zeroV interface{} newValue, del := valueFn(zeroV, false) if del { unlockBucket(&rootb.topHashMutex) return zeroV, false } // First we update the value, then the key. // This is important for atomic snapshot states. topHashes = atomic.LoadUint64(&emptyb.topHashMutex) atomic.StoreUint64(&emptyb.topHashMutex, storeTopHash(hash, topHashes, emptyidx)) atomic.StorePointer(&emptyb.values[emptyidx], unsafe.Pointer(&newValue)) atomic.StorePointer(&emptyb.keys[emptyidx], unsafe.Pointer(&key)) unlockBucket(&rootb.topHashMutex) table.addSize(bidx, 1) return newValue, computeOnly } growThreshold := float64(tableLen) * entriesPerMapBucket * mapLoadFactor if table.sumSize() > int64(growThreshold) { // Need to grow the table. Then go for another attempt. unlockBucket(&rootb.topHashMutex) m.resize(table, mapGrowHint) goto compute_attempt } // Insertion into a new bucket. var zeroV interface{} newValue, del := valueFn(zeroV, false) if del { unlockBucket(&rootb.topHashMutex) return newValue, false } // Create and append a bucket. newb := new(bucketPadded) newb.keys[0] = unsafe.Pointer(&key) newb.values[0] = unsafe.Pointer(&newValue) newb.topHashMutex = storeTopHash(hash, newb.topHashMutex, 0) atomic.StorePointer(&b.next, unsafe.Pointer(newb)) unlockBucket(&rootb.topHashMutex) table.addSize(bidx, 1) return newValue, computeOnly } b = (*bucketPadded)(b.next) } } } func (m *Map) newerTableExists(table *mapTable) bool { curTablePtr := atomic.LoadPointer(&m.table) return uintptr(curTablePtr) != uintptr(unsafe.Pointer(table)) } func (m *Map) resizeInProgress() bool { return atomic.LoadInt64(&m.resizing) == 1 } func (m *Map) waitForResize() { m.resizeMu.Lock() for m.resizeInProgress() { m.resizeCond.Wait() } m.resizeMu.Unlock() } func (m *Map) resize(knownTable *mapTable, hint mapResizeHint) { knownTableLen := len(knownTable.buckets) // Fast path for shrink attempts. if hint == mapShrinkHint { if m.growOnly || m.minTableLen == knownTableLen || knownTable.sumSize() > int64((knownTableLen*entriesPerMapBucket)/mapShrinkFraction) { return } } // Slow path. if !atomic.CompareAndSwapInt64(&m.resizing, 0, 1) { // Someone else started resize. Wait for it to finish. m.waitForResize() return } var newTable *mapTable table := (*mapTable)(atomic.LoadPointer(&m.table)) tableLen := len(table.buckets) switch hint { case mapGrowHint: // Grow the table with factor of 2. atomic.AddInt64(&m.totalGrowths, 1) newTable = newMapTable(tableLen << 1) case mapShrinkHint: shrinkThreshold := int64((tableLen * entriesPerMapBucket) / mapShrinkFraction) if tableLen > m.minTableLen && table.sumSize() <= shrinkThreshold { // Shrink the table with factor of 2. atomic.AddInt64(&m.totalShrinks, 1) newTable = newMapTable(tableLen >> 1) } else { // No need to shrink. Wake up all waiters and give up. m.resizeMu.Lock() atomic.StoreInt64(&m.resizing, 0) m.resizeCond.Broadcast() m.resizeMu.Unlock() return } case mapClearHint: newTable = newMapTable(m.minTableLen) default: panic(fmt.Sprintf("unexpected resize hint: %d", hint)) } // Copy the data only if we're not clearing the map. if hint != mapClearHint { for i := 0; i < tableLen; i++ { copied := copyBucket(&table.buckets[i], newTable) newTable.addSizePlain(uint64(i), copied) } } // Publish the new table and wake up all waiters. atomic.StorePointer(&m.table, unsafe.Pointer(newTable)) m.resizeMu.Lock() atomic.StoreInt64(&m.resizing, 0) m.resizeCond.Broadcast() m.resizeMu.Unlock() } func copyBucket(b *bucketPadded, destTable *mapTable) (copied int) { rootb := b lockBucket(&rootb.topHashMutex) for { for i := 0; i < entriesPerMapBucket; i++ { if b.keys[i] != nil { k := derefKey(b.keys[i]) hash := hashString(k, destTable.seed) bidx := uint64(len(destTable.buckets)-1) & hash destb := &destTable.buckets[bidx] appendToBucket(hash, b.keys[i], b.values[i], destb) copied++ } } if b.next == nil { unlockBucket(&rootb.topHashMutex) return } b = (*bucketPadded)(b.next) } } func appendToBucket(hash uint64, keyPtr, valPtr unsafe.Pointer, b *bucketPadded) { for { for i := 0; i < entriesPerMapBucket; i++ { if b.keys[i] == nil { b.keys[i] = keyPtr b.values[i] = valPtr b.topHashMutex = storeTopHash(hash, b.topHashMutex, i) return } } if b.next == nil { newb := new(bucketPadded) newb.keys[0] = keyPtr newb.values[0] = valPtr newb.topHashMutex = storeTopHash(hash, newb.topHashMutex, 0) b.next = unsafe.Pointer(newb) return } b = (*bucketPadded)(b.next) } } func isEmptyBucket(rootb *bucketPadded) bool { b := rootb for { for i := 0; i < entriesPerMapBucket; i++ { if b.keys[i] != nil { return false } } if b.next == nil { return true } b = (*bucketPadded)(b.next) } } // Range calls f sequentially for each key and value present in the // map. If f returns false, range stops the iteration. // // Range does not necessarily correspond to any consistent snapshot // of the Map's contents: no key will be visited more than once, but // if the value for any key is stored or deleted concurrently, Range // may reflect any mapping for that key from any point during the // Range call. // // It is safe to modify the map while iterating it, including entry // creation, modification and deletion. However, the concurrent // modification rule apply, i.e. the changes may be not reflected // in the subsequently iterated entries. func (m *Map) Range(f func(key string, value interface{}) bool) { var zeroEntry rangeEntry // Pre-allocate array big enough to fit entries for most hash tables. bentries := make([]rangeEntry, 0, 16*entriesPerMapBucket) tablep := atomic.LoadPointer(&m.table) table := *(*mapTable)(tablep) for i := range table.buckets { rootb := &table.buckets[i] b := rootb // Prevent concurrent modifications and copy all entries into // the intermediate slice. lockBucket(&rootb.topHashMutex) for { for i := 0; i < entriesPerMapBucket; i++ { if b.keys[i] != nil { bentries = append(bentries, rangeEntry{ key: b.keys[i], value: b.values[i], }) } } if b.next == nil { unlockBucket(&rootb.topHashMutex) break } b = (*bucketPadded)(b.next) } // Call the function for all copied entries. for j := range bentries { k := derefKey(bentries[j].key) v := derefValue(bentries[j].value) if !f(k, v) { return } // Remove the reference to avoid preventing the copied // entries from being GCed until this method finishes. bentries[j] = zeroEntry } bentries = bentries[:0] } } // Clear deletes all keys and values currently stored in the map. func (m *Map) Clear() { table := (*mapTable)(atomic.LoadPointer(&m.table)) m.resize(table, mapClearHint) } // Size returns current size of the map. func (m *Map) Size() int { table := (*mapTable)(atomic.LoadPointer(&m.table)) return int(table.sumSize()) } func derefKey(keyPtr unsafe.Pointer) string { return *(*string)(keyPtr) } func derefValue(valuePtr unsafe.Pointer) interface{} { return *(*interface{})(valuePtr) } func lockBucket(mu *uint64) { for { var v uint64 for { v = atomic.LoadUint64(mu) if v&1 != 1 { break } runtime.Gosched() } if atomic.CompareAndSwapUint64(mu, v, v|1) { return } runtime.Gosched() } } func unlockBucket(mu *uint64) { v := atomic.LoadUint64(mu) atomic.StoreUint64(mu, v&^1) } func topHashMatch(hash, topHashes uint64, idx int) bool { if topHashes&(1<<(idx+1)) == 0 { // Entry is not present. return false } hash = hash & topHashMask topHashes = (topHashes & topHashEntryMasks[idx]) << (20 * idx) return hash == topHashes } func storeTopHash(hash, topHashes uint64, idx int) uint64 { // Zero out top hash at idx. topHashes = topHashes &^ topHashEntryMasks[idx] // Chop top 20 MSBs of the given hash and position them at idx. hash = (hash & topHashMask) >> (20 * idx) // Store the MSBs. topHashes = topHashes | hash // Mark the entry as present. return topHashes | (1 << (idx + 1)) } func eraseTopHash(topHashes uint64, idx int) uint64 { return topHashes &^ (1 << (idx + 1)) } func (table *mapTable) addSize(bucketIdx uint64, delta int) { cidx := uint64(len(table.size)-1) & bucketIdx atomic.AddInt64(&table.size[cidx].c, int64(delta)) } func (table *mapTable) addSizePlain(bucketIdx uint64, delta int) { cidx := uint64(len(table.size)-1) & bucketIdx table.size[cidx].c += int64(delta) } func (table *mapTable) sumSize() int64 { sum := int64(0) for i := range table.size { sum += atomic.LoadInt64(&table.size[i].c) } return sum } // MapStats is Map/MapOf statistics. // // Warning: map statistics are intented to be used for diagnostic // purposes, not for production code. This means that breaking changes // may be introduced into this struct even between minor releases. type MapStats struct { // RootBuckets is the number of root buckets in the hash table. // Each bucket holds a few entries. RootBuckets int // TotalBuckets is the total number of buckets in the hash table, // including root and their chained buckets. Each bucket holds // a few entries. TotalBuckets int // EmptyBuckets is the number of buckets that hold no entries. EmptyBuckets int // Capacity is the Map/MapOf capacity, i.e. the total number of // entries that all buckets can physically hold. This number // does not consider the load factor. Capacity int // Size is the exact number of entries stored in the map. Size int // Counter is the number of entries stored in the map according // to the internal atomic counter. In case of concurrent map // modifications this number may be different from Size. Counter int // CounterLen is the number of internal atomic counter stripes. // This number may grow with the map capacity to improve // multithreaded scalability. CounterLen int // MinEntries is the minimum number of entries per a chain of // buckets, i.e. a root bucket and its chained buckets. MinEntries int // MinEntries is the maximum number of entries per a chain of // buckets, i.e. a root bucket and its chained buckets. MaxEntries int // TotalGrowths is the number of times the hash table grew. TotalGrowths int64 // TotalGrowths is the number of times the hash table shrinked. TotalShrinks int64 } // ToString returns string representation of map stats. func (s *MapStats) ToString() string { var sb strings.Builder sb.WriteString("MapStats{\n") sb.WriteString(fmt.Sprintf("RootBuckets: %d\n", s.RootBuckets)) sb.WriteString(fmt.Sprintf("TotalBuckets: %d\n", s.TotalBuckets)) sb.WriteString(fmt.Sprintf("EmptyBuckets: %d\n", s.EmptyBuckets)) sb.WriteString(fmt.Sprintf("Capacity: %d\n", s.Capacity)) sb.WriteString(fmt.Sprintf("Size: %d\n", s.Size)) sb.WriteString(fmt.Sprintf("Counter: %d\n", s.Counter)) sb.WriteString(fmt.Sprintf("CounterLen: %d\n", s.CounterLen)) sb.WriteString(fmt.Sprintf("MinEntries: %d\n", s.MinEntries)) sb.WriteString(fmt.Sprintf("MaxEntries: %d\n", s.MaxEntries)) sb.WriteString(fmt.Sprintf("TotalGrowths: %d\n", s.TotalGrowths)) sb.WriteString(fmt.Sprintf("TotalShrinks: %d\n", s.TotalShrinks)) sb.WriteString("}\n") return sb.String() } // Stats returns statistics for the Map. Just like other map // methods, this one is thread-safe. Yet it's an O(N) operation, // so it should be used only for diagnostics or debugging purposes. func (m *Map) Stats() MapStats { stats := MapStats{ TotalGrowths: atomic.LoadInt64(&m.totalGrowths), TotalShrinks: atomic.LoadInt64(&m.totalShrinks), MinEntries: math.MaxInt32, } table := (*mapTable)(atomic.LoadPointer(&m.table)) stats.RootBuckets = len(table.buckets) stats.Counter = int(table.sumSize()) stats.CounterLen = len(table.size) for i := range table.buckets { nentries := 0 b := &table.buckets[i] stats.TotalBuckets++ for { nentriesLocal := 0 stats.Capacity += entriesPerMapBucket for i := 0; i < entriesPerMapBucket; i++ { if atomic.LoadPointer(&b.keys[i]) != nil { stats.Size++ nentriesLocal++ } } nentries += nentriesLocal if nentriesLocal == 0 { stats.EmptyBuckets++ } if b.next == nil { break } b = (*bucketPadded)(atomic.LoadPointer(&b.next)) stats.TotalBuckets++ } if nentries < stats.MinEntries { stats.MinEntries = nentries } if nentries > stats.MaxEntries { stats.MaxEntries = nentries } } return stats } xsync-3.5.0/map_test.go000066400000000000000000001121471474520662300150450ustar00rootroot00000000000000package xsync_test import ( "fmt" "math" "math/bits" "math/rand" "strconv" "sync" "sync/atomic" "testing" "time" "unsafe" . "github.com/puzpuzpuz/xsync/v3" ) const ( // number of entries to use in benchmarks benchmarkNumEntries = 1_000 // key prefix used in benchmarks benchmarkKeyPrefix = "what_a_looooooooooooooooooooooong_key_prefix_" ) var benchmarkCases = []struct { name string readPercentage int }{ {"reads=100%", 100}, // 100% loads, 0% stores, 0% deletes {"reads=99%", 99}, // 99% loads, 0.5% stores, 0.5% deletes {"reads=90%", 90}, // 90% loads, 5% stores, 5% deletes {"reads=75%", 75}, // 75% loads, 12.5% stores, 12.5% deletes } var benchmarkKeys []string func init() { benchmarkKeys = make([]string, benchmarkNumEntries) for i := 0; i < benchmarkNumEntries; i++ { benchmarkKeys[i] = benchmarkKeyPrefix + strconv.Itoa(i) } } func runParallel(b *testing.B, benchFn func(pb *testing.PB)) { b.ResetTimer() start := time.Now() b.RunParallel(benchFn) opsPerSec := float64(b.N) / float64(time.Since(start).Seconds()) b.ReportMetric(opsPerSec, "ops/s") } func TestMap_BucketStructSize(t *testing.T) { size := unsafe.Sizeof(BucketPadded{}) if size != 64 { t.Fatalf("size of 64B (one cache line) is expected, got: %d", size) } } func TestMap_UniqueValuePointers_Int(t *testing.T) { EnableAssertions() m := NewMap() v := 42 m.Store("foo", v) m.Store("foo", v) DisableAssertions() } func TestMap_UniqueValuePointers_Struct(t *testing.T) { type foo struct{} EnableAssertions() m := NewMap() v := foo{} m.Store("foo", v) m.Store("foo", v) DisableAssertions() } func TestMap_UniqueValuePointers_Pointer(t *testing.T) { type foo struct{} EnableAssertions() m := NewMap() v := &foo{} m.Store("foo", v) m.Store("foo", v) DisableAssertions() } func TestMap_UniqueValuePointers_Slice(t *testing.T) { EnableAssertions() m := NewMap() v := make([]int, 13) m.Store("foo", v) m.Store("foo", v) DisableAssertions() } func TestMap_UniqueValuePointers_String(t *testing.T) { EnableAssertions() m := NewMap() v := "bar" m.Store("foo", v) m.Store("foo", v) DisableAssertions() } func TestMap_UniqueValuePointers_Nil(t *testing.T) { EnableAssertions() m := NewMap() m.Store("foo", nil) m.Store("foo", nil) DisableAssertions() } func TestMap_MissingEntry(t *testing.T) { m := NewMap() v, ok := m.Load("foo") if ok { t.Fatalf("value was not expected: %v", v) } if deleted, loaded := m.LoadAndDelete("foo"); loaded { t.Fatalf("value was not expected %v", deleted) } if actual, loaded := m.LoadOrStore("foo", "bar"); loaded { t.Fatalf("value was not expected %v", actual) } } func TestMap_EmptyStringKey(t *testing.T) { m := NewMap() m.Store("", "foobar") v, ok := m.Load("") if !ok { t.Fatal("value was expected") } if vs, ok := v.(string); ok && vs != "foobar" { t.Fatalf("value does not match: %v", v) } } func TestMapStore_NilValue(t *testing.T) { m := NewMap() m.Store("foo", nil) v, ok := m.Load("foo") if !ok { t.Fatal("nil value was expected") } if v != nil { t.Fatalf("value was not nil: %v", v) } } func TestMapLoadOrStore_NilValue(t *testing.T) { m := NewMap() m.LoadOrStore("foo", nil) v, loaded := m.LoadOrStore("foo", nil) if !loaded { t.Fatal("nil value was expected") } if v != nil { t.Fatalf("value was not nil: %v", v) } } func TestMapLoadOrStore_NonNilValue(t *testing.T) { type foo struct{} m := NewMap() newv := &foo{} v, loaded := m.LoadOrStore("foo", newv) if loaded { t.Fatal("no value was expected") } if v != newv { t.Fatalf("value does not match: %v", v) } newv2 := &foo{} v, loaded = m.LoadOrStore("foo", newv2) if !loaded { t.Fatal("value was expected") } if v != newv { t.Fatalf("value does not match: %v", v) } } func TestMapLoadAndStore_NilValue(t *testing.T) { m := NewMap() m.LoadAndStore("foo", nil) v, loaded := m.LoadAndStore("foo", nil) if !loaded { t.Fatal("nil value was expected") } if v != nil { t.Fatalf("value was not nil: %v", v) } v, loaded = m.Load("foo") if !loaded { t.Fatal("nil value was expected") } if v != nil { t.Fatalf("value was not nil: %v", v) } } func TestMapLoadAndStore_NonNilValue(t *testing.T) { type foo struct{} m := NewMap() v1 := &foo{} v, loaded := m.LoadAndStore("foo", v1) if loaded { t.Fatal("no value was expected") } if v != v1 { t.Fatalf("value does not match: %v", v) } v2 := 2 v, loaded = m.LoadAndStore("foo", v2) if !loaded { t.Fatal("value was expected") } if v != v1 { t.Fatalf("value does not match: %v", v) } v, loaded = m.Load("foo") if !loaded { t.Fatal("value was expected") } if v != v2 { t.Fatalf("value does not match: %v", v) } } func TestMapRange(t *testing.T) { const numEntries = 1000 m := NewMap() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } iters := 0 met := make(map[string]int) m.Range(func(key string, value interface{}) bool { if key != strconv.Itoa(value.(int)) { t.Fatalf("got unexpected key/value for iteration %d: %v/%v", iters, key, value) return false } met[key] += 1 iters++ return true }) if iters != numEntries { t.Fatalf("got unexpected number of iterations: %d", iters) } for i := 0; i < numEntries; i++ { if c := met[strconv.Itoa(i)]; c != 1 { t.Fatalf("met key %d multiple times: %d", i, c) } } } func TestMapRange_FalseReturned(t *testing.T) { m := NewMap() for i := 0; i < 100; i++ { m.Store(strconv.Itoa(i), i) } iters := 0 m.Range(func(key string, value interface{}) bool { iters++ return iters != 13 }) if iters != 13 { t.Fatalf("got unexpected number of iterations: %d", iters) } } func TestMapRange_NestedDelete(t *testing.T) { const numEntries = 256 m := NewMap() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } m.Range(func(key string, value interface{}) bool { m.Delete(key) return true }) for i := 0; i < numEntries; i++ { if _, ok := m.Load(strconv.Itoa(i)); ok { t.Fatalf("value found for %d", i) } } } func TestMapStore(t *testing.T) { const numEntries = 128 m := NewMap() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } for i := 0; i < numEntries; i++ { v, ok := m.Load(strconv.Itoa(i)) if !ok { t.Fatalf("value not found for %d", i) } if vi, ok := v.(int); ok && vi != i { t.Fatalf("values do not match for %d: %v", i, v) } } } func TestMapLoadOrStore(t *testing.T) { const numEntries = 1000 m := NewMap() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } for i := 0; i < numEntries; i++ { if _, loaded := m.LoadOrStore(strconv.Itoa(i), i); !loaded { t.Fatalf("value not found for %d", i) } } } func TestMapLoadOrCompute(t *testing.T) { const numEntries = 1000 m := NewMap() for i := 0; i < numEntries; i++ { v, loaded := m.LoadOrCompute(strconv.Itoa(i), func() interface{} { return i }) if loaded { t.Fatalf("value not computed for %d", i) } if vi, ok := v.(int); ok && vi != i { t.Fatalf("values do not match for %d: %v", i, v) } } for i := 0; i < numEntries; i++ { v, loaded := m.LoadOrCompute(strconv.Itoa(i), func() interface{} { return i }) if !loaded { t.Fatalf("value not loaded for %d", i) } if vi, ok := v.(int); ok && vi != i { t.Fatalf("values do not match for %d: %v", i, v) } } } func TestMapLoadOrCompute_FunctionCalledOnce(t *testing.T) { m := NewMap() for i := 0; i < 100; { m.LoadOrCompute(strconv.Itoa(i), func() (v interface{}) { v, i = i, i+1 return v }) } m.Range(func(k string, v interface{}) bool { if vi, ok := v.(int); !ok || strconv.Itoa(vi) != k { t.Fatalf("%sth key is not equal to value %d", k, v) } return true }) } func TestMapLoadOrTryCompute(t *testing.T) { const numEntries = 1000 m := NewMap() for i := 0; i < numEntries; i++ { v, loaded := m.LoadOrTryCompute(strconv.Itoa(i), func() (newValue interface{}, cancel bool) { return i, true }) if loaded { t.Fatalf("value not computed for %d", i) } if v != nil { t.Fatalf("values do not match for %d: %v", i, v) } } if m.Size() != 0 { t.Fatalf("zero map size expected: %d", m.Size()) } for i := 0; i < numEntries; i++ { v, loaded := m.LoadOrTryCompute(strconv.Itoa(i), func() (newValue interface{}, cancel bool) { return i, false }) if loaded { t.Fatalf("value not computed for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } for i := 0; i < numEntries; i++ { v, loaded := m.LoadOrTryCompute(strconv.Itoa(i), func() (newValue interface{}, cancel bool) { return i, false }) if !loaded { t.Fatalf("value not loaded for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } } func TestMapLoadOrTryCompute_FunctionCalledOnce(t *testing.T) { m := NewMap() for i := 0; i < 100; { m.LoadOrTryCompute(strconv.Itoa(i), func() (v interface{}, cancel bool) { v, i = i, i+1 return v, false }) } m.Range(func(k string, v interface{}) bool { if vi, ok := v.(int); !ok || strconv.Itoa(vi) != k { t.Fatalf("%sth key is not equal to value %d", k, v) } return true }) } func TestMapCompute(t *testing.T) { var zeroV interface{} m := NewMap() // Store a new value. v, ok := m.Compute("foobar", func(oldValue interface{}, loaded bool) (newValue interface{}, delete bool) { if oldValue != zeroV { t.Fatalf("oldValue should be empty interface{} when computing a new value: %d", oldValue) } if loaded { t.Fatal("loaded should be false when computing a new value") } newValue = 42 delete = false return }) if v.(int) != 42 { t.Fatalf("v should be 42 when computing a new value: %d", v) } if !ok { t.Fatal("ok should be true when computing a new value") } // Update an existing value. v, ok = m.Compute("foobar", func(oldValue interface{}, loaded bool) (newValue interface{}, delete bool) { if oldValue.(int) != 42 { t.Fatalf("oldValue should be 42 when updating the value: %d", oldValue) } if !loaded { t.Fatal("loaded should be true when updating the value") } newValue = oldValue.(int) + 42 delete = false return }) if v.(int) != 84 { t.Fatalf("v should be 84 when updating the value: %d", v) } if !ok { t.Fatal("ok should be true when updating the value") } // Delete an existing value. v, ok = m.Compute("foobar", func(oldValue interface{}, loaded bool) (newValue interface{}, delete bool) { if oldValue != 84 { t.Fatalf("oldValue should be 84 when deleting the value: %d", oldValue) } if !loaded { t.Fatal("loaded should be true when deleting the value") } delete = true return }) if v.(int) != 84 { t.Fatalf("v should be 84 when deleting the value: %d", v) } if ok { t.Fatal("ok should be false when deleting the value") } // Try to delete a non-existing value. Notice different key. v, ok = m.Compute("barbaz", func(oldValue interface{}, loaded bool) (newValue interface{}, delete bool) { var zeroV interface{} if oldValue != zeroV { t.Fatalf("oldValue should be empty interface{} when trying to delete a non-existing value: %d", oldValue) } if loaded { t.Fatal("loaded should be false when trying to delete a non-existing value") } // We're returning a non-zero value, but the map should ignore it. newValue = 42 delete = true return }) if v != zeroV { t.Fatalf("v should be empty interface{} when trying to delete a non-existing value: %d", v) } if ok { t.Fatal("ok should be false when trying to delete a non-existing value") } } func TestMapStoreThenDelete(t *testing.T) { const numEntries = 1000 m := NewMap() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } for i := 0; i < numEntries; i++ { m.Delete(strconv.Itoa(i)) if _, ok := m.Load(strconv.Itoa(i)); ok { t.Fatalf("value was not expected for %d", i) } } } func TestMapStoreThenLoadAndDelete(t *testing.T) { const numEntries = 1000 m := NewMap() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } for i := 0; i < numEntries; i++ { if v, loaded := m.LoadAndDelete(strconv.Itoa(i)); !loaded || v.(int) != i { t.Fatalf("value was not found or different for %d: %v", i, v) } if _, ok := m.Load(strconv.Itoa(i)); ok { t.Fatalf("value was not expected for %d", i) } } } func TestMapStoreThenParallelDelete_DoesNotShrinkBelowMinTableLen(t *testing.T) { const numEntries = 1000 m := NewMap() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } cdone := make(chan bool) go func() { for i := 0; i < numEntries; i++ { m.Delete(strconv.Itoa(int(i))) } cdone <- true }() go func() { for i := 0; i < numEntries; i++ { m.Delete(strconv.Itoa(int(i))) } cdone <- true }() // Wait for the goroutines to finish. <-cdone <-cdone stats := m.Stats() if stats.RootBuckets != DefaultMinMapTableLen { t.Fatalf("table length was different from the minimum: %d", stats.RootBuckets) } } func sizeBasedOnRange(m *Map) int { size := 0 m.Range(func(key string, value interface{}) bool { size++ return true }) return size } func TestMapSize(t *testing.T) { const numEntries = 1000 m := NewMap() size := m.Size() if size != 0 { t.Fatalf("zero size expected: %d", size) } expectedSize := 0 for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) expectedSize++ size := m.Size() if size != expectedSize { t.Fatalf("size of %d was expected, got: %d", expectedSize, size) } rsize := sizeBasedOnRange(m) if size != rsize { t.Fatalf("size does not match number of entries in Range: %v, %v", size, rsize) } } for i := 0; i < numEntries; i++ { m.Delete(strconv.Itoa(i)) expectedSize-- size := m.Size() if size != expectedSize { t.Fatalf("size of %d was expected, got: %d", expectedSize, size) } rsize := sizeBasedOnRange(m) if size != rsize { t.Fatalf("size does not match number of entries in Range: %v, %v", size, rsize) } } } func TestMapClear(t *testing.T) { const numEntries = 1000 m := NewMap() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } size := m.Size() if size != numEntries { t.Fatalf("size of %d was expected, got: %d", numEntries, size) } m.Clear() size = m.Size() if size != 0 { t.Fatalf("zero size was expected, got: %d", size) } rsize := sizeBasedOnRange(m) if rsize != 0 { t.Fatalf("zero number of entries in Range was expected, got: %d", rsize) } } func assertMapCapacity(t *testing.T, m *Map, expectedCap int) { stats := m.Stats() if stats.Capacity != expectedCap { t.Fatalf("capacity was different from %d: %d", expectedCap, stats.Capacity) } } func TestNewMapPresized(t *testing.T) { assertMapCapacity(t, NewMap(), DefaultMinMapTableCap) assertMapCapacity(t, NewMapPresized(1000), 1536) assertMapCapacity(t, NewMap(WithPresize(1000)), 1536) assertMapCapacity(t, NewMapPresized(0), DefaultMinMapTableCap) assertMapCapacity(t, NewMap(WithPresize(0)), DefaultMinMapTableCap) assertMapCapacity(t, NewMapPresized(-1), DefaultMinMapTableCap) assertMapCapacity(t, NewMap(WithPresize(-1)), DefaultMinMapTableCap) } func TestNewMapPresized_DoesNotShrinkBelowMinTableLen(t *testing.T) { const minTableLen = 1024 const numEntries = int(minTableLen * EntriesPerMapBucket * MapLoadFactor) m := NewMap(WithPresize(numEntries)) for i := 0; i < 2*numEntries; i++ { m.Store(strconv.Itoa(i), i) } stats := m.Stats() if stats.RootBuckets <= minTableLen { t.Fatalf("table did not grow: %d", stats.RootBuckets) } for i := 0; i < 2*numEntries; i++ { m.Delete(strconv.Itoa(int(i))) } stats = m.Stats() if stats.RootBuckets != minTableLen { t.Fatalf("table length was different from the minimum: %d", stats.RootBuckets) } } func TestNewMapGrowOnly_OnlyShrinksOnClear(t *testing.T) { const minTableLen = 128 const numEntries = minTableLen * EntriesPerMapBucket m := NewMap(WithPresize(numEntries), WithGrowOnly()) stats := m.Stats() initialTableLen := stats.RootBuckets for i := 0; i < 2*numEntries; i++ { m.Store(strconv.Itoa(i), i) } stats = m.Stats() maxTableLen := stats.RootBuckets if maxTableLen <= minTableLen { t.Fatalf("table did not grow: %d", maxTableLen) } for i := 0; i < numEntries; i++ { m.Delete(strconv.Itoa(int(i))) } stats = m.Stats() if stats.RootBuckets != maxTableLen { t.Fatalf("table length was different from the expected: %d", stats.RootBuckets) } m.Clear() stats = m.Stats() if stats.RootBuckets != initialTableLen { t.Fatalf("table length was different from the initial: %d", stats.RootBuckets) } } func TestMapResize(t *testing.T) { const numEntries = 100_000 m := NewMap() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } stats := m.Stats() if stats.Size != numEntries { t.Fatalf("size was too small: %d", stats.Size) } expectedCapacity := int(math.RoundToEven(MapLoadFactor+1)) * stats.RootBuckets * EntriesPerMapBucket if stats.Capacity > expectedCapacity { t.Fatalf("capacity was too large: %d, expected: %d", stats.Capacity, expectedCapacity) } if stats.RootBuckets <= DefaultMinMapTableLen { t.Fatalf("table was too small: %d", stats.RootBuckets) } if stats.TotalGrowths == 0 { t.Fatalf("non-zero total growths expected: %d", stats.TotalGrowths) } if stats.TotalShrinks > 0 { t.Fatalf("zero total shrinks expected: %d", stats.TotalShrinks) } // This is useful when debugging table resize and occupancy. // Use -v flag to see the output. t.Log(stats.ToString()) for i := 0; i < numEntries; i++ { m.Delete(strconv.Itoa(i)) } stats = m.Stats() if stats.Size > 0 { t.Fatalf("zero size was expected: %d", stats.Size) } expectedCapacity = stats.RootBuckets * EntriesPerMapBucket if stats.Capacity != expectedCapacity { t.Fatalf("capacity was too large: %d, expected: %d", stats.Capacity, expectedCapacity) } if stats.RootBuckets != DefaultMinMapTableLen { t.Fatalf("table was too large: %d", stats.RootBuckets) } if stats.TotalShrinks == 0 { t.Fatalf("non-zero total shrinks expected: %d", stats.TotalShrinks) } t.Log(stats.ToString()) } func TestMapResize_CounterLenLimit(t *testing.T) { const numEntries = 1_000_000 m := NewMap() for i := 0; i < numEntries; i++ { m.Store("foo"+strconv.Itoa(i), "bar"+strconv.Itoa(i)) } stats := m.Stats() if stats.Size != numEntries { t.Fatalf("size was too small: %d", stats.Size) } if stats.CounterLen != MaxMapCounterLen { t.Fatalf("number of counter stripes was too large: %d, expected: %d", stats.CounterLen, MaxMapCounterLen) } } func parallelSeqResizer(m *Map, numEntries int, positive bool, cdone chan bool) { for i := 0; i < numEntries; i++ { if positive { m.Store(strconv.Itoa(i), i) } else { m.Store(strconv.Itoa(-i), -i) } } cdone <- true } func TestMapParallelResize_GrowOnly(t *testing.T) { const numEntries = 100_000 m := NewMap() cdone := make(chan bool) go parallelSeqResizer(m, numEntries, true, cdone) go parallelSeqResizer(m, numEntries, false, cdone) // Wait for the goroutines to finish. <-cdone <-cdone // Verify map contents. for i := -numEntries + 1; i < numEntries; i++ { v, ok := m.Load(strconv.Itoa(i)) if !ok { t.Fatalf("value not found for %d", i) } if vi, ok := v.(int); ok && vi != i { t.Fatalf("values do not match for %d: %v", i, v) } } if s := m.Size(); s != 2*numEntries-1 { t.Fatalf("unexpected size: %v", s) } } func parallelRandResizer(t *testing.T, m *Map, numIters, numEntries int, cdone chan bool) { r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < numIters; i++ { coin := r.Int63n(2) for j := 0; j < numEntries; j++ { if coin == 1 { m.Store(strconv.Itoa(j), j) } else { m.Delete(strconv.Itoa(j)) } } } cdone <- true } func TestMapParallelResize(t *testing.T) { const numIters = 1_000 const numEntries = 2 * EntriesPerMapBucket * DefaultMinMapTableLen m := NewMap() cdone := make(chan bool) go parallelRandResizer(t, m, numIters, numEntries, cdone) go parallelRandResizer(t, m, numIters, numEntries, cdone) // Wait for the goroutines to finish. <-cdone <-cdone // Verify map contents. for i := 0; i < numEntries; i++ { v, ok := m.Load(strconv.Itoa(i)) if !ok { // The entry may be deleted and that's ok. continue } if vi, ok := v.(int); ok && vi != i { t.Fatalf("values do not match for %d: %v", i, v) } } s := m.Size() if s > numEntries { t.Fatalf("unexpected size: %v", s) } rs := sizeBasedOnRange(m) if s != rs { t.Fatalf("size does not match number of entries in Range: %v, %v", s, rs) } } func parallelRandClearer(t *testing.T, m *Map, numIters, numEntries int, cdone chan bool) { r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < numIters; i++ { coin := r.Int63n(2) for j := 0; j < numEntries; j++ { if coin == 1 { m.Store(strconv.Itoa(j), j) } else { m.Clear() } } } cdone <- true } func TestMapParallelClear(t *testing.T) { const numIters = 100 const numEntries = 1_000 m := NewMap() cdone := make(chan bool) go parallelRandClearer(t, m, numIters, numEntries, cdone) go parallelRandClearer(t, m, numIters, numEntries, cdone) // Wait for the goroutines to finish. <-cdone <-cdone // Verify map size. s := m.Size() if s > numEntries { t.Fatalf("unexpected size: %v", s) } rs := sizeBasedOnRange(m) if s != rs { t.Fatalf("size does not match number of entries in Range: %v, %v", s, rs) } } func parallelSeqStorer(t *testing.T, m *Map, storeEach, numIters, numEntries int, cdone chan bool) { for i := 0; i < numIters; i++ { for j := 0; j < numEntries; j++ { if storeEach == 0 || j%storeEach == 0 { m.Store(strconv.Itoa(j), j) // Due to atomic snapshots we must see a ""/j pair. v, ok := m.Load(strconv.Itoa(j)) if !ok { t.Errorf("value was not found for %d", j) break } if vi, ok := v.(int); !ok || vi != j { t.Errorf("value was not expected for %d: %d", j, vi) break } } } } cdone <- true } func TestMapParallelStores(t *testing.T) { const numStorers = 4 const numIters = 10_000 const numEntries = 100 m := NewMap() cdone := make(chan bool) for i := 0; i < numStorers; i++ { go parallelSeqStorer(t, m, i, numIters, numEntries, cdone) } // Wait for the goroutines to finish. for i := 0; i < numStorers; i++ { <-cdone } // Verify map contents. for i := 0; i < numEntries; i++ { v, ok := m.Load(strconv.Itoa(i)) if !ok { t.Fatalf("value not found for %d", i) } if vi, ok := v.(int); ok && vi != i { t.Fatalf("values do not match for %d: %v", i, v) } } } func parallelRandStorer(t *testing.T, m *Map, numIters, numEntries int, cdone chan bool) { r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < numIters; i++ { j := r.Intn(numEntries) if v, loaded := m.LoadOrStore(strconv.Itoa(j), j); loaded { if vi, ok := v.(int); !ok || vi != j { t.Errorf("value was not expected for %d: %d", j, vi) } } } cdone <- true } func parallelRandDeleter(t *testing.T, m *Map, numIters, numEntries int, cdone chan bool) { r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < numIters; i++ { j := r.Intn(numEntries) if v, loaded := m.LoadAndDelete(strconv.Itoa(j)); loaded { if vi, ok := v.(int); !ok || vi != j { t.Errorf("value was not expected for %d: %d", j, vi) } } } cdone <- true } func parallelLoader(t *testing.T, m *Map, numIters, numEntries int, cdone chan bool) { for i := 0; i < numIters; i++ { for j := 0; j < numEntries; j++ { // Due to atomic snapshots we must either see no entry, or a ""/j pair. if v, ok := m.Load(strconv.Itoa(j)); ok { if vi, ok := v.(int); !ok || vi != j { t.Errorf("value was not expected for %d: %d", j, vi) } } } } cdone <- true } func TestMapAtomicSnapshot(t *testing.T) { const numIters = 100_000 const numEntries = 100 m := NewMap() cdone := make(chan bool) // Update or delete random entry in parallel with loads. go parallelRandStorer(t, m, numIters, numEntries, cdone) go parallelRandDeleter(t, m, numIters, numEntries, cdone) go parallelLoader(t, m, numIters, numEntries, cdone) // Wait for the goroutines to finish. for i := 0; i < 3; i++ { <-cdone } } func TestMapParallelStoresAndDeletes(t *testing.T) { const numWorkers = 2 const numIters = 100_000 const numEntries = 1000 m := NewMap() cdone := make(chan bool) // Update random entry in parallel with deletes. for i := 0; i < numWorkers; i++ { go parallelRandStorer(t, m, numIters, numEntries, cdone) go parallelRandDeleter(t, m, numIters, numEntries, cdone) } // Wait for the goroutines to finish. for i := 0; i < 2*numWorkers; i++ { <-cdone } } func parallelComputer(m *Map, numIters, numEntries int, cdone chan bool) { for i := 0; i < numIters; i++ { for j := 0; j < numEntries; j++ { m.Compute(strconv.Itoa(j), func(oldValue interface{}, loaded bool) (newValue interface{}, delete bool) { if !loaded { return uint64(1), false } return uint64(oldValue.(uint64) + 1), false }) } } cdone <- true } func TestMapParallelComputes(t *testing.T) { const numWorkers = 4 // Also stands for numEntries. const numIters = 10_000 m := NewMap() cdone := make(chan bool) for i := 0; i < numWorkers; i++ { go parallelComputer(m, numIters, numWorkers, cdone) } // Wait for the goroutines to finish. for i := 0; i < numWorkers; i++ { <-cdone } // Verify map contents. for i := 0; i < numWorkers; i++ { v, ok := m.Load(strconv.Itoa(i)) if !ok { t.Fatalf("value not found for %d", i) } if v.(uint64) != numWorkers*numIters { t.Fatalf("values do not match for %d: %v", i, v) } } } func parallelRangeStorer(m *Map, numEntries int, stopFlag *int64, cdone chan bool) { for { for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } if atomic.LoadInt64(stopFlag) != 0 { break } } cdone <- true } func parallelRangeDeleter(m *Map, numEntries int, stopFlag *int64, cdone chan bool) { for { for i := 0; i < numEntries; i++ { m.Delete(strconv.Itoa(i)) } if atomic.LoadInt64(stopFlag) != 0 { break } } cdone <- true } func TestMapParallelRange(t *testing.T) { const numEntries = 10_000 m := NewMap() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } // Start goroutines that would be storing and deleting items in parallel. cdone := make(chan bool) stopFlag := int64(0) go parallelRangeStorer(m, numEntries, &stopFlag, cdone) go parallelRangeDeleter(m, numEntries, &stopFlag, cdone) // Iterate the map and verify that no duplicate keys were met. met := make(map[string]int) m.Range(func(key string, value interface{}) bool { if key != strconv.Itoa(value.(int)) { t.Fatalf("got unexpected value for key %s: %v", key, value) return false } met[key] += 1 return true }) if len(met) == 0 { t.Fatal("no entries were met when iterating") } for k, c := range met { if c != 1 { t.Fatalf("met key %s multiple times: %d", k, c) } } // Make sure that both goroutines finish. atomic.StoreInt64(&stopFlag, 1) <-cdone <-cdone } func parallelShrinker(t *testing.T, m *Map, numIters, numEntries int, stopFlag *int64, cdone chan bool) { for i := 0; i < numIters; i++ { for j := 0; j < numEntries; j++ { if p, loaded := m.LoadOrStore(strconv.Itoa(j), &point{int32(j), int32(j)}); loaded { t.Errorf("value was present for %d: %v", j, p) } } for j := 0; j < numEntries; j++ { m.Delete(strconv.Itoa(j)) } } atomic.StoreInt64(stopFlag, 1) cdone <- true } func parallelUpdater(t *testing.T, m *Map, idx int, stopFlag *int64, cdone chan bool) { for atomic.LoadInt64(stopFlag) != 1 { sleepUs := int(Fastrand() % 10) if p, loaded := m.LoadOrStore(strconv.Itoa(idx), &point{int32(idx), int32(idx)}); loaded { t.Errorf("value was present for %d: %v", idx, p) } time.Sleep(time.Duration(sleepUs) * time.Microsecond) if _, ok := m.Load(strconv.Itoa(idx)); !ok { t.Errorf("value was not found for %d", idx) } m.Delete(strconv.Itoa(idx)) } cdone <- true } func TestMapDoesNotLoseEntriesOnResize(t *testing.T) { const numIters = 10_000 const numEntries = 128 m := NewMap() cdone := make(chan bool) stopFlag := int64(0) go parallelShrinker(t, m, numIters, numEntries, &stopFlag, cdone) go parallelUpdater(t, m, numEntries, &stopFlag, cdone) // Wait for the goroutines to finish. <-cdone <-cdone // Verify map contents. if s := m.Size(); s != 0 { t.Fatalf("map is not empty: %d", s) } } func TestMapTopHashMutex(t *testing.T) { const ( numLockers = 4 numIterations = 1000 ) var ( activity int32 mu uint64 ) cdone := make(chan bool) for i := 0; i < numLockers; i++ { go func() { for i := 0; i < numIterations; i++ { LockBucket(&mu) n := atomic.AddInt32(&activity, 1) if n != 1 { UnlockBucket(&mu) panic(fmt.Sprintf("lock(%d)\n", n)) } atomic.AddInt32(&activity, -1) UnlockBucket(&mu) } cdone <- true }() } // Wait for all lockers to finish. for i := 0; i < numLockers; i++ { <-cdone } } func TestMapTopHashMutex_Store_NoLock(t *testing.T) { mu := uint64(0) testMapTopHashMutex_Store(t, &mu) } func TestMapTopHashMutex_Store_WhileLocked(t *testing.T) { mu := uint64(0) LockBucket(&mu) defer UnlockBucket(&mu) testMapTopHashMutex_Store(t, &mu) } func testMapTopHashMutex_Store(t *testing.T, topHashes *uint64) { hash := uint64(0b1101_0100_1010_1011_1101 << 44) for i := 0; i < EntriesPerMapBucket; i++ { if TopHashMatch(hash, *topHashes, i) { t.Fatalf("top hash match for all zeros for index %d", i) } prevOnes := bits.OnesCount64(*topHashes) *topHashes = StoreTopHash(hash, *topHashes, i) newOnes := bits.OnesCount64(*topHashes) expectedInc := bits.OnesCount64(hash) + 1 if newOnes != prevOnes+expectedInc { t.Fatalf("unexpected bits change after store for index %d: %d, %d, %#b", i, newOnes, prevOnes+expectedInc, *topHashes) } if !TopHashMatch(hash, *topHashes, i) { t.Fatalf("top hash mismatch after store for index %d: %#b", i, *topHashes) } } } func TestMapTopHashMutex_Erase_NoLock(t *testing.T) { mu := uint64(0) testMapTopHashMutex_Erase(t, &mu) } func TestMapTopHashMutex_Erase_WhileLocked(t *testing.T) { mu := uint64(0) LockBucket(&mu) defer UnlockBucket(&mu) testMapTopHashMutex_Erase(t, &mu) } func testMapTopHashMutex_Erase(t *testing.T, topHashes *uint64) { hash := uint64(0xababaaaaaaaaaaaa) // top hash is 1010_1011_1010_1011_1010 for i := 0; i < EntriesPerMapBucket; i++ { *topHashes = StoreTopHash(hash, *topHashes, i) ones := bits.OnesCount64(*topHashes) *topHashes = EraseTopHash(*topHashes, i) if TopHashMatch(hash, *topHashes, i) { t.Fatalf("top hash match after erase for index %d: %#b", i, *topHashes) } erasedBits := ones - bits.OnesCount64(*topHashes) if erasedBits != 1 { t.Fatalf("more than one bit changed after erase: %d, %d", i, erasedBits) } } } func TestMapTopHashMutex_StoreAfterErase_NoLock(t *testing.T) { mu := uint64(0) testMapTopHashMutex_StoreAfterErase(t, &mu) } func TestMapTopHashMutex_StoreAfterErase_WhileLocked(t *testing.T) { mu := uint64(0) LockBucket(&mu) defer UnlockBucket(&mu) testMapTopHashMutex_StoreAfterErase(t, &mu) } func testMapTopHashMutex_StoreAfterErase(t *testing.T, topHashes *uint64) { hashOne := uint64(0b1101_0100_1101_0100_1101_1111 << 40) // top hash is 1101_0100_1101_0100_1101 hashTwo := uint64(0b1010_1011_1010_1011_1010_1111 << 40) // top hash is 1010_1011_1010_1011_1010 idx := 2 *topHashes = StoreTopHash(hashOne, *topHashes, idx) if !TopHashMatch(hashOne, *topHashes, idx) { t.Fatalf("top hash mismatch for hash one: %#b, %#b", hashOne, *topHashes) } if TopHashMatch(hashTwo, *topHashes, idx) { t.Fatalf("top hash match for hash two: %#b, %#b", hashTwo, *topHashes) } *topHashes = EraseTopHash(*topHashes, idx) *topHashes = StoreTopHash(hashTwo, *topHashes, idx) if TopHashMatch(hashOne, *topHashes, idx) { t.Fatalf("top hash match for hash one: %#b, %#b", hashOne, *topHashes) } if !TopHashMatch(hashTwo, *topHashes, idx) { t.Fatalf("top hash mismatch for hash two: %#b, %#b", hashTwo, *topHashes) } } func TestMapStats(t *testing.T) { m := NewMap() stats := m.Stats() if stats.RootBuckets != DefaultMinMapTableLen { t.Fatalf("unexpected number of root buckets: %d", stats.RootBuckets) } if stats.TotalBuckets != stats.RootBuckets { t.Fatalf("unexpected number of total buckets: %d", stats.TotalBuckets) } if stats.EmptyBuckets != stats.RootBuckets { t.Fatalf("unexpected number of empty buckets: %d", stats.EmptyBuckets) } if stats.Capacity != EntriesPerMapBucket*DefaultMinMapTableLen { t.Fatalf("unexpected capacity: %d", stats.Capacity) } if stats.Size != 0 { t.Fatalf("unexpected size: %d", stats.Size) } if stats.Counter != 0 { t.Fatalf("unexpected counter: %d", stats.Counter) } if stats.CounterLen != 8 { t.Fatalf("unexpected counter length: %d", stats.CounterLen) } for i := 0; i < 100; i++ { m.Store(strconv.Itoa(int(i)), i) } stats = m.Stats() if stats.RootBuckets != 2*DefaultMinMapTableLen { t.Fatalf("unexpected number of root buckets: %d", stats.RootBuckets) } if stats.TotalBuckets < stats.RootBuckets { t.Fatalf("unexpected number of total buckets: %d", stats.TotalBuckets) } if stats.EmptyBuckets >= stats.RootBuckets { t.Fatalf("unexpected number of empty buckets: %d", stats.EmptyBuckets) } if stats.Capacity < 2*EntriesPerMapBucket*DefaultMinMapTableLen { t.Fatalf("unexpected capacity: %d", stats.Capacity) } if stats.Size != 100 { t.Fatalf("unexpected size: %d", stats.Size) } if stats.Counter != 100 { t.Fatalf("unexpected counter: %d", stats.Counter) } if stats.CounterLen != 8 { t.Fatalf("unexpected counter length: %d", stats.CounterLen) } } func TestToPlainMap_NilPointer(t *testing.T) { pm := ToPlainMap(nil) if len(pm) != 0 { t.Fatalf("got unexpected size of nil map copy: %d", len(pm)) } } func TestToPlainMap(t *testing.T) { const numEntries = 1000 m := NewMap() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } pm := ToPlainMap(m) if len(pm) != numEntries { t.Fatalf("got unexpected size of nil map copy: %d", len(pm)) } for i := 0; i < numEntries; i++ { if v := pm[strconv.Itoa(i)]; v != i { t.Fatalf("unexpected value for key %d: %d", i, v) } } } func BenchmarkMap_NoWarmUp(b *testing.B) { for _, bc := range benchmarkCases { if bc.readPercentage == 100 { // This benchmark doesn't make sense without a warm-up. continue } b.Run(bc.name, func(b *testing.B) { m := NewMap() benchmarkMap(b, func(k string) (interface{}, bool) { return m.Load(k) }, func(k string, v interface{}) { m.Store(k, v) }, func(k string) { m.Delete(k) }, bc.readPercentage) }) } } func BenchmarkMapStandard_NoWarmUp(b *testing.B) { for _, bc := range benchmarkCases { if bc.readPercentage == 100 { // This benchmark doesn't make sense without a warm-up. continue } b.Run(bc.name, func(b *testing.B) { var m sync.Map benchmarkMap(b, func(k string) (interface{}, bool) { return m.Load(k) }, func(k string, v interface{}) { m.Store(k, v) }, func(k string) { m.Delete(k) }, bc.readPercentage) }) } } func BenchmarkMap_WarmUp(b *testing.B) { for _, bc := range benchmarkCases { b.Run(bc.name, func(b *testing.B) { m := NewMap(WithPresize(benchmarkNumEntries)) for i := 0; i < benchmarkNumEntries; i++ { m.Store(benchmarkKeyPrefix+strconv.Itoa(i), i) } b.ResetTimer() benchmarkMap(b, func(k string) (interface{}, bool) { return m.Load(k) }, func(k string, v interface{}) { m.Store(k, v) }, func(k string) { m.Delete(k) }, bc.readPercentage) }) } } // This is a nice scenario for sync.Map since a lot of updates // will hit the readOnly part of the map. func BenchmarkMapStandard_WarmUp(b *testing.B) { for _, bc := range benchmarkCases { b.Run(bc.name, func(b *testing.B) { var m sync.Map for i := 0; i < benchmarkNumEntries; i++ { m.Store(benchmarkKeyPrefix+strconv.Itoa(i), i) } b.ResetTimer() benchmarkMap(b, func(k string) (interface{}, bool) { return m.Load(k) }, func(k string, v interface{}) { m.Store(k, v) }, func(k string) { m.Delete(k) }, bc.readPercentage) }) } } func benchmarkMap( b *testing.B, loadFn func(k string) (interface{}, bool), storeFn func(k string, v interface{}), deleteFn func(k string), readPercentage int, ) { runParallel(b, func(pb *testing.PB) { // convert percent to permille to support 99% case storeThreshold := 10 * readPercentage deleteThreshold := 10*readPercentage + ((1000 - 10*readPercentage) / 2) for pb.Next() { op := int(Fastrand() % 1000) i := int(Fastrand() % benchmarkNumEntries) if op >= deleteThreshold { deleteFn(benchmarkKeys[i]) } else if op >= storeThreshold { storeFn(benchmarkKeys[i], i) } else { loadFn(benchmarkKeys[i]) } } }) } func BenchmarkMapRange(b *testing.B) { m := NewMap() for i := 0; i < benchmarkNumEntries; i++ { m.Store(benchmarkKeys[i], i) } b.ResetTimer() runParallel(b, func(pb *testing.PB) { foo := 0 for pb.Next() { m.Range(func(key string, value interface{}) bool { // Dereference the value to have an apple-to-apple // comparison with MapOf.Range. _ = value.(int) foo++ return true }) _ = foo } }) } func BenchmarkMapRangeStandard(b *testing.B) { var m sync.Map for i := 0; i < benchmarkNumEntries; i++ { m.Store(benchmarkKeys[i], i) } b.ResetTimer() runParallel(b, func(pb *testing.PB) { foo := 0 for pb.Next() { m.Range(func(key interface{}, value interface{}) bool { // Dereference the key and the value to have an apple-to-apple // comparison with MapOf.Range. _, _ = key.(string), value.(int) foo++ return true }) _ = foo } }) } xsync-3.5.0/mapof.go000066400000000000000000000523371474520662300143370ustar00rootroot00000000000000package xsync import ( "fmt" "math" "sync" "sync/atomic" "unsafe" ) const ( // number of MapOf entries per bucket; 5 entries lead to size of 64B // (one cache line) on 64-bit machines entriesPerMapOfBucket = 5 defaultMeta uint64 = 0x8080808080808080 metaMask uint64 = 0xffffffffff defaultMetaMasked uint64 = defaultMeta & metaMask emptyMetaSlot uint8 = 0x80 ) // MapOf is like a Go map[K]V but is safe for concurrent // use by multiple goroutines without additional locking or // coordination. It follows the interface of sync.Map with // a number of valuable extensions like Compute or Size. // // A MapOf must not be copied after first use. // // MapOf uses a modified version of Cache-Line Hash Table (CLHT) // data structure: https://github.com/LPD-EPFL/CLHT // // CLHT is built around idea to organize the hash table in // cache-line-sized buckets, so that on all modern CPUs update // operations complete with at most one cache-line transfer. // Also, Get operations involve no write to memory, as well as no // mutexes or any other sort of locks. Due to this design, in all // considered scenarios MapOf outperforms sync.Map. // // MapOf also borrows ideas from Java's j.u.c.ConcurrentHashMap // (immutable K/V pair structs instead of atomic snapshots) // and C++'s absl::flat_hash_map (meta memory and SWAR-based // lookups). type MapOf[K comparable, V any] struct { totalGrowths int64 totalShrinks int64 resizing int64 // resize in progress flag; updated atomically resizeMu sync.Mutex // only used along with resizeCond resizeCond sync.Cond // used to wake up resize waiters (concurrent modifications) table unsafe.Pointer // *mapOfTable hasher func(K, uint64) uint64 minTableLen int growOnly bool } type mapOfTable[K comparable, V any] struct { buckets []bucketOfPadded // striped counter for number of table entries; // used to determine if a table shrinking is needed // occupies min(buckets_memory/1024, 64KB) of memory size []counterStripe seed uint64 } // bucketOfPadded is a CL-sized map bucket holding up to // entriesPerMapOfBucket entries. type bucketOfPadded struct { //lint:ignore U1000 ensure each bucket takes two cache lines on both 32 and 64-bit archs pad [cacheLineSize - unsafe.Sizeof(bucketOf{})]byte bucketOf } type bucketOf struct { meta uint64 entries [entriesPerMapOfBucket]unsafe.Pointer // *entryOf next unsafe.Pointer // *bucketOfPadded mu sync.Mutex } // entryOf is an immutable map entry. type entryOf[K comparable, V any] struct { key K value V } // NewMapOf creates a new MapOf instance configured with the given // options. func NewMapOf[K comparable, V any](options ...func(*MapConfig)) *MapOf[K, V] { return NewMapOfWithHasher[K, V](defaultHasher[K](), options...) } // NewMapOfWithHasher creates a new MapOf instance configured with // the given hasher and options. The hash function is used instead // of the built-in hash function configured when a map is created // with the NewMapOf function. func NewMapOfWithHasher[K comparable, V any]( hasher func(K, uint64) uint64, options ...func(*MapConfig), ) *MapOf[K, V] { c := &MapConfig{ sizeHint: defaultMinMapTableLen * entriesPerMapOfBucket, } for _, o := range options { o(c) } m := &MapOf[K, V]{} m.resizeCond = *sync.NewCond(&m.resizeMu) m.hasher = hasher var table *mapOfTable[K, V] if c.sizeHint <= defaultMinMapTableLen*entriesPerMapOfBucket { table = newMapOfTable[K, V](defaultMinMapTableLen) } else { tableLen := nextPowOf2(uint32((float64(c.sizeHint) / entriesPerMapOfBucket) / mapLoadFactor)) table = newMapOfTable[K, V](int(tableLen)) } m.minTableLen = len(table.buckets) m.growOnly = c.growOnly atomic.StorePointer(&m.table, unsafe.Pointer(table)) return m } // NewMapOfPresized creates a new MapOf instance with capacity enough // to hold sizeHint entries. The capacity is treated as the minimal capacity // meaning that the underlying hash table will never shrink to // a smaller capacity. If sizeHint is zero or negative, the value // is ignored. // // Deprecated: use NewMapOf in combination with WithPresize. func NewMapOfPresized[K comparable, V any](sizeHint int) *MapOf[K, V] { return NewMapOf[K, V](WithPresize(sizeHint)) } func newMapOfTable[K comparable, V any](minTableLen int) *mapOfTable[K, V] { buckets := make([]bucketOfPadded, minTableLen) for i := range buckets { buckets[i].meta = defaultMeta } counterLen := minTableLen >> 10 if counterLen < minMapCounterLen { counterLen = minMapCounterLen } else if counterLen > maxMapCounterLen { counterLen = maxMapCounterLen } counter := make([]counterStripe, counterLen) t := &mapOfTable[K, V]{ buckets: buckets, size: counter, seed: makeSeed(), } return t } // ToPlainMapOf returns a native map with a copy of xsync Map's // contents. The copied xsync Map should not be modified while // this call is made. If the copied Map is modified, the copying // behavior is the same as in the Range method. func ToPlainMapOf[K comparable, V any](m *MapOf[K, V]) map[K]V { pm := make(map[K]V) if m != nil { m.Range(func(key K, value V) bool { pm[key] = value return true }) } return pm } // Load returns the value stored in the map for a key, or zero value // of type V if no value is present. // The ok result indicates whether value was found in the map. func (m *MapOf[K, V]) Load(key K) (value V, ok bool) { table := (*mapOfTable[K, V])(atomic.LoadPointer(&m.table)) hash := m.hasher(key, table.seed) h1 := h1(hash) h2w := broadcast(h2(hash)) bidx := uint64(len(table.buckets)-1) & h1 b := &table.buckets[bidx] for { metaw := atomic.LoadUint64(&b.meta) markedw := markZeroBytes(metaw^h2w) & metaMask for markedw != 0 { idx := firstMarkedByteIndex(markedw) eptr := atomic.LoadPointer(&b.entries[idx]) if eptr != nil { e := (*entryOf[K, V])(eptr) if e.key == key { return e.value, true } } markedw &= markedw - 1 } bptr := atomic.LoadPointer(&b.next) if bptr == nil { return } b = (*bucketOfPadded)(bptr) } } // Store sets the value for a key. func (m *MapOf[K, V]) Store(key K, value V) { m.doCompute( key, func(V, bool) (V, bool) { return value, false }, false, false, ) } // LoadOrStore returns the existing value for the key if present. // Otherwise, it stores and returns the given value. // The loaded result is true if the value was loaded, false if stored. func (m *MapOf[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { return m.doCompute( key, func(V, bool) (V, bool) { return value, false }, true, false, ) } // LoadAndStore returns the existing value for the key if present, // while setting the new value for the key. // It stores the new value and returns the existing one, if present. // The loaded result is true if the existing value was loaded, // false otherwise. func (m *MapOf[K, V]) LoadAndStore(key K, value V) (actual V, loaded bool) { return m.doCompute( key, func(V, bool) (V, bool) { return value, false }, false, false, ) } // LoadOrCompute returns the existing value for the key if present. // Otherwise, it computes the value using the provided function and // returns the computed value. The loaded result is true if the value // was loaded, false if stored. // // This call locks a hash table bucket while the compute function // is executed. It means that modifications on other entries in // the bucket will be blocked until the valueFn executes. Consider // this when the function includes long-running operations. func (m *MapOf[K, V]) LoadOrCompute(key K, valueFn func() V) (actual V, loaded bool) { return m.doCompute( key, func(V, bool) (V, bool) { return valueFn(), false }, true, false, ) } // LoadOrTryCompute returns the existing value for the key if present. // Otherwise, it tries to compute the value using the provided function // and, if success, returns the computed value. The loaded result is true // if the value was loaded, false if stored. If the compute attempt was // cancelled, a zero value of type V will be returned. // // This call locks a hash table bucket while the compute function // is executed. It means that modifications on other entries in // the bucket will be blocked until the valueFn executes. Consider // this when the function includes long-running operations. func (m *MapOf[K, V]) LoadOrTryCompute( key K, valueFn func() (newValue V, cancel bool), ) (value V, loaded bool) { return m.doCompute( key, func(V, bool) (V, bool) { nv, c := valueFn() if !c { return nv, false } return nv, true // nv is ignored }, true, false, ) } // Compute either sets the computed new value for the key or deletes // the value for the key. When the delete result of the valueFn function // is set to true, the value will be deleted, if it exists. When delete // is set to false, the value is updated to the newValue. // The ok result indicates whether value was computed and stored, thus, is // present in the map. The actual result contains the new value in cases where // the value was computed and stored. See the example for a few use cases. // // This call locks a hash table bucket while the compute function // is executed. It means that modifications on other entries in // the bucket will be blocked until the valueFn executes. Consider // this when the function includes long-running operations. func (m *MapOf[K, V]) Compute( key K, valueFn func(oldValue V, loaded bool) (newValue V, delete bool), ) (actual V, ok bool) { return m.doCompute(key, valueFn, false, true) } // LoadAndDelete deletes the value for a key, returning the previous // value if any. The loaded result reports whether the key was // present. func (m *MapOf[K, V]) LoadAndDelete(key K) (value V, loaded bool) { return m.doCompute( key, func(value V, loaded bool) (V, bool) { return value, true }, false, false, ) } // Delete deletes the value for a key. func (m *MapOf[K, V]) Delete(key K) { m.doCompute( key, func(value V, loaded bool) (V, bool) { return value, true }, false, false, ) } func (m *MapOf[K, V]) doCompute( key K, valueFn func(oldValue V, loaded bool) (V, bool), loadIfExists, computeOnly bool, ) (V, bool) { // Read-only path. if loadIfExists { if v, ok := m.Load(key); ok { return v, !computeOnly } } // Write path. for { compute_attempt: var ( emptyb *bucketOfPadded emptyidx int ) table := (*mapOfTable[K, V])(atomic.LoadPointer(&m.table)) tableLen := len(table.buckets) hash := m.hasher(key, table.seed) h1 := h1(hash) h2 := h2(hash) h2w := broadcast(h2) bidx := uint64(len(table.buckets)-1) & h1 rootb := &table.buckets[bidx] rootb.mu.Lock() // The following two checks must go in reverse to what's // in the resize method. if m.resizeInProgress() { // Resize is in progress. Wait, then go for another attempt. rootb.mu.Unlock() m.waitForResize() goto compute_attempt } if m.newerTableExists(table) { // Someone resized the table. Go for another attempt. rootb.mu.Unlock() goto compute_attempt } b := rootb for { metaw := b.meta markedw := markZeroBytes(metaw^h2w) & metaMask for markedw != 0 { idx := firstMarkedByteIndex(markedw) eptr := b.entries[idx] if eptr != nil { e := (*entryOf[K, V])(eptr) if e.key == key { if loadIfExists { rootb.mu.Unlock() return e.value, !computeOnly } // In-place update/delete. // We get a copy of the value via an interface{} on each call, // thus the live value pointers are unique. Otherwise atomic // snapshot won't be correct in case of multiple Store calls // using the same value. oldv := e.value newv, del := valueFn(oldv, true) if del { // Deletion. // First we update the hash, then the entry. newmetaw := setByte(metaw, emptyMetaSlot, idx) atomic.StoreUint64(&b.meta, newmetaw) atomic.StorePointer(&b.entries[idx], nil) rootb.mu.Unlock() table.addSize(bidx, -1) // Might need to shrink the table if we left bucket empty. if newmetaw == defaultMeta { m.resize(table, mapShrinkHint) } return oldv, !computeOnly } newe := new(entryOf[K, V]) newe.key = key newe.value = newv atomic.StorePointer(&b.entries[idx], unsafe.Pointer(newe)) rootb.mu.Unlock() if computeOnly { // Compute expects the new value to be returned. return newv, true } // LoadAndStore expects the old value to be returned. return oldv, true } } markedw &= markedw - 1 } if emptyb == nil { // Search for empty entries (up to 5 per bucket). emptyw := metaw & defaultMetaMasked if emptyw != 0 { idx := firstMarkedByteIndex(emptyw) emptyb = b emptyidx = idx } } if b.next == nil { if emptyb != nil { // Insertion into an existing bucket. var zeroV V newValue, del := valueFn(zeroV, false) if del { rootb.mu.Unlock() return zeroV, false } newe := new(entryOf[K, V]) newe.key = key newe.value = newValue // First we update meta, then the entry. atomic.StoreUint64(&emptyb.meta, setByte(emptyb.meta, h2, emptyidx)) atomic.StorePointer(&emptyb.entries[emptyidx], unsafe.Pointer(newe)) rootb.mu.Unlock() table.addSize(bidx, 1) return newValue, computeOnly } growThreshold := float64(tableLen) * entriesPerMapOfBucket * mapLoadFactor if table.sumSize() > int64(growThreshold) { // Need to grow the table. Then go for another attempt. rootb.mu.Unlock() m.resize(table, mapGrowHint) goto compute_attempt } // Insertion into a new bucket. var zeroV V newValue, del := valueFn(zeroV, false) if del { rootb.mu.Unlock() return newValue, false } // Create and append a bucket. newb := new(bucketOfPadded) newb.meta = setByte(defaultMeta, h2, 0) newe := new(entryOf[K, V]) newe.key = key newe.value = newValue newb.entries[0] = unsafe.Pointer(newe) atomic.StorePointer(&b.next, unsafe.Pointer(newb)) rootb.mu.Unlock() table.addSize(bidx, 1) return newValue, computeOnly } b = (*bucketOfPadded)(b.next) } } } func (m *MapOf[K, V]) newerTableExists(table *mapOfTable[K, V]) bool { curTablePtr := atomic.LoadPointer(&m.table) return uintptr(curTablePtr) != uintptr(unsafe.Pointer(table)) } func (m *MapOf[K, V]) resizeInProgress() bool { return atomic.LoadInt64(&m.resizing) == 1 } func (m *MapOf[K, V]) waitForResize() { m.resizeMu.Lock() for m.resizeInProgress() { m.resizeCond.Wait() } m.resizeMu.Unlock() } func (m *MapOf[K, V]) resize(knownTable *mapOfTable[K, V], hint mapResizeHint) { knownTableLen := len(knownTable.buckets) // Fast path for shrink attempts. if hint == mapShrinkHint { if m.growOnly || m.minTableLen == knownTableLen || knownTable.sumSize() > int64((knownTableLen*entriesPerMapOfBucket)/mapShrinkFraction) { return } } // Slow path. if !atomic.CompareAndSwapInt64(&m.resizing, 0, 1) { // Someone else started resize. Wait for it to finish. m.waitForResize() return } var newTable *mapOfTable[K, V] table := (*mapOfTable[K, V])(atomic.LoadPointer(&m.table)) tableLen := len(table.buckets) switch hint { case mapGrowHint: // Grow the table with factor of 2. atomic.AddInt64(&m.totalGrowths, 1) newTable = newMapOfTable[K, V](tableLen << 1) case mapShrinkHint: shrinkThreshold := int64((tableLen * entriesPerMapOfBucket) / mapShrinkFraction) if tableLen > m.minTableLen && table.sumSize() <= shrinkThreshold { // Shrink the table with factor of 2. atomic.AddInt64(&m.totalShrinks, 1) newTable = newMapOfTable[K, V](tableLen >> 1) } else { // No need to shrink. Wake up all waiters and give up. m.resizeMu.Lock() atomic.StoreInt64(&m.resizing, 0) m.resizeCond.Broadcast() m.resizeMu.Unlock() return } case mapClearHint: newTable = newMapOfTable[K, V](m.minTableLen) default: panic(fmt.Sprintf("unexpected resize hint: %d", hint)) } // Copy the data only if we're not clearing the map. if hint != mapClearHint { for i := 0; i < tableLen; i++ { copied := copyBucketOf(&table.buckets[i], newTable, m.hasher) newTable.addSizePlain(uint64(i), copied) } } // Publish the new table and wake up all waiters. atomic.StorePointer(&m.table, unsafe.Pointer(newTable)) m.resizeMu.Lock() atomic.StoreInt64(&m.resizing, 0) m.resizeCond.Broadcast() m.resizeMu.Unlock() } func copyBucketOf[K comparable, V any]( b *bucketOfPadded, destTable *mapOfTable[K, V], hasher func(K, uint64) uint64, ) (copied int) { rootb := b rootb.mu.Lock() for { for i := 0; i < entriesPerMapOfBucket; i++ { if b.entries[i] != nil { e := (*entryOf[K, V])(b.entries[i]) hash := hasher(e.key, destTable.seed) bidx := uint64(len(destTable.buckets)-1) & h1(hash) destb := &destTable.buckets[bidx] appendToBucketOf(h2(hash), b.entries[i], destb) copied++ } } if b.next == nil { rootb.mu.Unlock() return } b = (*bucketOfPadded)(b.next) } } // Range calls f sequentially for each key and value present in the // map. If f returns false, range stops the iteration. // // Range does not necessarily correspond to any consistent snapshot // of the Map's contents: no key will be visited more than once, but // if the value for any key is stored or deleted concurrently, Range // may reflect any mapping for that key from any point during the // Range call. // // It is safe to modify the map while iterating it, including entry // creation, modification and deletion. However, the concurrent // modification rule apply, i.e. the changes may be not reflected // in the subsequently iterated entries. func (m *MapOf[K, V]) Range(f func(key K, value V) bool) { var zeroPtr unsafe.Pointer // Pre-allocate array big enough to fit entries for most hash tables. bentries := make([]unsafe.Pointer, 0, 16*entriesPerMapOfBucket) tablep := atomic.LoadPointer(&m.table) table := *(*mapOfTable[K, V])(tablep) for i := range table.buckets { rootb := &table.buckets[i] b := rootb // Prevent concurrent modifications and copy all entries into // the intermediate slice. rootb.mu.Lock() for { for i := 0; i < entriesPerMapOfBucket; i++ { if b.entries[i] != nil { bentries = append(bentries, b.entries[i]) } } if b.next == nil { rootb.mu.Unlock() break } b = (*bucketOfPadded)(b.next) } // Call the function for all copied entries. for j := range bentries { entry := (*entryOf[K, V])(bentries[j]) if !f(entry.key, entry.value) { return } // Remove the reference to avoid preventing the copied // entries from being GCed until this method finishes. bentries[j] = zeroPtr } bentries = bentries[:0] } } // Clear deletes all keys and values currently stored in the map. func (m *MapOf[K, V]) Clear() { table := (*mapOfTable[K, V])(atomic.LoadPointer(&m.table)) m.resize(table, mapClearHint) } // Size returns current size of the map. func (m *MapOf[K, V]) Size() int { table := (*mapOfTable[K, V])(atomic.LoadPointer(&m.table)) return int(table.sumSize()) } func appendToBucketOf(h2 uint8, entryPtr unsafe.Pointer, b *bucketOfPadded) { for { for i := 0; i < entriesPerMapOfBucket; i++ { if b.entries[i] == nil { b.meta = setByte(b.meta, h2, i) b.entries[i] = entryPtr return } } if b.next == nil { newb := new(bucketOfPadded) newb.meta = setByte(defaultMeta, h2, 0) newb.entries[0] = entryPtr b.next = unsafe.Pointer(newb) return } b = (*bucketOfPadded)(b.next) } } func (table *mapOfTable[K, V]) addSize(bucketIdx uint64, delta int) { cidx := uint64(len(table.size)-1) & bucketIdx atomic.AddInt64(&table.size[cidx].c, int64(delta)) } func (table *mapOfTable[K, V]) addSizePlain(bucketIdx uint64, delta int) { cidx := uint64(len(table.size)-1) & bucketIdx table.size[cidx].c += int64(delta) } func (table *mapOfTable[K, V]) sumSize() int64 { sum := int64(0) for i := range table.size { sum += atomic.LoadInt64(&table.size[i].c) } return sum } func h1(h uint64) uint64 { return h >> 7 } func h2(h uint64) uint8 { return uint8(h & 0x7f) } // Stats returns statistics for the MapOf. Just like other map // methods, this one is thread-safe. Yet it's an O(N) operation, // so it should be used only for diagnostics or debugging purposes. func (m *MapOf[K, V]) Stats() MapStats { stats := MapStats{ TotalGrowths: atomic.LoadInt64(&m.totalGrowths), TotalShrinks: atomic.LoadInt64(&m.totalShrinks), MinEntries: math.MaxInt32, } table := (*mapOfTable[K, V])(atomic.LoadPointer(&m.table)) stats.RootBuckets = len(table.buckets) stats.Counter = int(table.sumSize()) stats.CounterLen = len(table.size) for i := range table.buckets { nentries := 0 b := &table.buckets[i] stats.TotalBuckets++ for { nentriesLocal := 0 stats.Capacity += entriesPerMapOfBucket for i := 0; i < entriesPerMapOfBucket; i++ { if atomic.LoadPointer(&b.entries[i]) != nil { stats.Size++ nentriesLocal++ } } nentries += nentriesLocal if nentriesLocal == 0 { stats.EmptyBuckets++ } if b.next == nil { break } b = (*bucketOfPadded)(atomic.LoadPointer(&b.next)) stats.TotalBuckets++ } if nentries < stats.MinEntries { stats.MinEntries = nentries } if nentries > stats.MaxEntries { stats.MaxEntries = nentries } } return stats } xsync-3.5.0/mapof_test.go000066400000000000000000001124771474520662300154000ustar00rootroot00000000000000package xsync_test import ( "math" "math/rand" "strconv" "sync" "sync/atomic" "testing" "time" "unsafe" . "github.com/puzpuzpuz/xsync/v3" ) type point struct { x int32 y int32 } func TestMap_BucketOfStructSize(t *testing.T) { size := unsafe.Sizeof(BucketOfPadded{}) if size != 64 { t.Fatalf("size of 64B (one cache line) is expected, got: %d", size) } } func TestMapOf_MissingEntry(t *testing.T) { m := NewMapOf[string, string]() v, ok := m.Load("foo") if ok { t.Fatalf("value was not expected: %v", v) } if deleted, loaded := m.LoadAndDelete("foo"); loaded { t.Fatalf("value was not expected %v", deleted) } if actual, loaded := m.LoadOrStore("foo", "bar"); loaded { t.Fatalf("value was not expected %v", actual) } } func TestMapOf_EmptyStringKey(t *testing.T) { m := NewMapOf[string, string]() m.Store("", "foobar") v, ok := m.Load("") if !ok { t.Fatal("value was expected") } if v != "foobar" { t.Fatalf("value does not match: %v", v) } } func TestMapOfStore_NilValue(t *testing.T) { m := NewMapOf[string, *struct{}]() m.Store("foo", nil) v, ok := m.Load("foo") if !ok { t.Fatal("nil value was expected") } if v != nil { t.Fatalf("value was not nil: %v", v) } } func TestMapOfLoadOrStore_NilValue(t *testing.T) { m := NewMapOf[string, *struct{}]() m.LoadOrStore("foo", nil) v, loaded := m.LoadOrStore("foo", nil) if !loaded { t.Fatal("nil value was expected") } if v != nil { t.Fatalf("value was not nil: %v", v) } } func TestMapOfLoadOrStore_NonNilValue(t *testing.T) { type foo struct{} m := NewMapOf[string, *foo]() newv := &foo{} v, loaded := m.LoadOrStore("foo", newv) if loaded { t.Fatal("no value was expected") } if v != newv { t.Fatalf("value does not match: %v", v) } newv2 := &foo{} v, loaded = m.LoadOrStore("foo", newv2) if !loaded { t.Fatal("value was expected") } if v != newv { t.Fatalf("value does not match: %v", v) } } func TestMapOfLoadAndStore_NilValue(t *testing.T) { m := NewMapOf[string, *struct{}]() m.LoadAndStore("foo", nil) v, loaded := m.LoadAndStore("foo", nil) if !loaded { t.Fatal("nil value was expected") } if v != nil { t.Fatalf("value was not nil: %v", v) } v, loaded = m.Load("foo") if !loaded { t.Fatal("nil value was expected") } if v != nil { t.Fatalf("value was not nil: %v", v) } } func TestMapOfLoadAndStore_NonNilValue(t *testing.T) { m := NewMapOf[string, int]() v1 := 1 v, loaded := m.LoadAndStore("foo", v1) if loaded { t.Fatal("no value was expected") } if v != v1 { t.Fatalf("value does not match: %v", v) } v2 := 2 v, loaded = m.LoadAndStore("foo", v2) if !loaded { t.Fatal("value was expected") } if v != v1 { t.Fatalf("value does not match: %v", v) } v, loaded = m.Load("foo") if !loaded { t.Fatal("value was expected") } if v != v2 { t.Fatalf("value does not match: %v", v) } } func TestMapOfRange(t *testing.T) { const numEntries = 1000 m := NewMapOf[string, int]() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } iters := 0 met := make(map[string]int) m.Range(func(key string, value int) bool { if key != strconv.Itoa(value) { t.Fatalf("got unexpected key/value for iteration %d: %v/%v", iters, key, value) return false } met[key] += 1 iters++ return true }) if iters != numEntries { t.Fatalf("got unexpected number of iterations: %d", iters) } for i := 0; i < numEntries; i++ { if c := met[strconv.Itoa(i)]; c != 1 { t.Fatalf("range did not iterate correctly over %d: %d", i, c) } } } func TestMapOfRange_FalseReturned(t *testing.T) { m := NewMapOf[string, int]() for i := 0; i < 100; i++ { m.Store(strconv.Itoa(i), i) } iters := 0 m.Range(func(key string, value int) bool { iters++ return iters != 13 }) if iters != 13 { t.Fatalf("got unexpected number of iterations: %d", iters) } } func TestMapOfRange_NestedDelete(t *testing.T) { const numEntries = 256 m := NewMapOf[string, int]() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } m.Range(func(key string, value int) bool { m.Delete(key) return true }) for i := 0; i < numEntries; i++ { if _, ok := m.Load(strconv.Itoa(i)); ok { t.Fatalf("value found for %d", i) } } } func TestMapOfStringStore(t *testing.T) { const numEntries = 128 m := NewMapOf[string, int]() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } for i := 0; i < numEntries; i++ { v, ok := m.Load(strconv.Itoa(i)) if !ok { t.Fatalf("value not found for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } } func TestMapOfIntStore(t *testing.T) { const numEntries = 128 m := NewMapOf[int, int]() for i := 0; i < numEntries; i++ { m.Store(i, i) } for i := 0; i < numEntries; i++ { v, ok := m.Load(i) if !ok { t.Fatalf("value not found for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } } func TestMapOfStore_StructKeys_IntValues(t *testing.T) { const numEntries = 128 m := NewMapOf[point, int]() for i := 0; i < numEntries; i++ { m.Store(point{int32(i), -int32(i)}, i) } for i := 0; i < numEntries; i++ { v, ok := m.Load(point{int32(i), -int32(i)}) if !ok { t.Fatalf("value not found for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } } func TestMapOfStore_StructKeys_StructValues(t *testing.T) { const numEntries = 128 m := NewMapOf[point, point]() for i := 0; i < numEntries; i++ { m.Store(point{int32(i), -int32(i)}, point{-int32(i), int32(i)}) } for i := 0; i < numEntries; i++ { v, ok := m.Load(point{int32(i), -int32(i)}) if !ok { t.Fatalf("value not found for %d", i) } if v.x != -int32(i) { t.Fatalf("x value does not match for %d: %v", i, v) } if v.y != int32(i) { t.Fatalf("y value does not match for %d: %v", i, v) } } } func TestMapOfWithHasher(t *testing.T) { const numEntries = 10000 m := NewMapOfWithHasher[int, int](murmur3Finalizer) for i := 0; i < numEntries; i++ { m.Store(i, i) } for i := 0; i < numEntries; i++ { v, ok := m.Load(i) if !ok { t.Fatalf("value not found for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } } func murmur3Finalizer(i int, _ uint64) uint64 { h := uint64(i) h = (h ^ (h >> 33)) * 0xff51afd7ed558ccd h = (h ^ (h >> 33)) * 0xc4ceb9fe1a85ec53 return h ^ (h >> 33) } func TestMapOfWithHasher_HashCodeCollisions(t *testing.T) { const numEntries = 1000 m := NewMapOfWithHasher[int, int](func(i int, _ uint64) uint64 { // We intentionally use an awful hash function here to make sure // that the map copes with key collisions. return 42 }, WithPresize(numEntries)) for i := 0; i < numEntries; i++ { m.Store(i, i) } for i := 0; i < numEntries; i++ { v, ok := m.Load(i) if !ok { t.Fatalf("value not found for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } } func TestMapOfLoadOrStore(t *testing.T) { const numEntries = 1000 m := NewMapOf[string, int]() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } for i := 0; i < numEntries; i++ { if _, loaded := m.LoadOrStore(strconv.Itoa(i), i); !loaded { t.Fatalf("value not found for %d", i) } } } func TestMapOfLoadOrCompute(t *testing.T) { const numEntries = 1000 m := NewMapOf[string, int]() for i := 0; i < numEntries; i++ { v, loaded := m.LoadOrCompute(strconv.Itoa(i), func() int { return i }) if loaded { t.Fatalf("value not computed for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } for i := 0; i < numEntries; i++ { v, loaded := m.LoadOrCompute(strconv.Itoa(i), func() int { return i }) if !loaded { t.Fatalf("value not loaded for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } } func TestMapOfLoadOrCompute_FunctionCalledOnce(t *testing.T) { m := NewMapOf[int, int]() for i := 0; i < 100; { m.LoadOrCompute(i, func() (v int) { v, i = i, i+1 return v }) } m.Range(func(k, v int) bool { if k != v { t.Fatalf("%dth key is not equal to value %d", k, v) } return true }) } func TestMapOfLoadOrTryCompute(t *testing.T) { const numEntries = 1000 m := NewMapOf[string, int]() for i := 0; i < numEntries; i++ { v, loaded := m.LoadOrTryCompute(strconv.Itoa(i), func() (newValue int, cancel bool) { return i, true }) if loaded { t.Fatalf("value not computed for %d", i) } if v != 0 { t.Fatalf("values do not match for %d: %v", i, v) } } if m.Size() != 0 { t.Fatalf("zero map size expected: %d", m.Size()) } for i := 0; i < numEntries; i++ { v, loaded := m.LoadOrTryCompute(strconv.Itoa(i), func() (newValue int, cancel bool) { return i, false }) if loaded { t.Fatalf("value not computed for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } for i := 0; i < numEntries; i++ { v, loaded := m.LoadOrTryCompute(strconv.Itoa(i), func() (newValue int, cancel bool) { return i, false }) if !loaded { t.Fatalf("value not loaded for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } } func TestMapOfLoadOrTryCompute_FunctionCalledOnce(t *testing.T) { m := NewMapOf[int, int]() for i := 0; i < 100; { m.LoadOrTryCompute(i, func() (newValue int, cancel bool) { newValue, i = i, i+1 return newValue, false }) } m.Range(func(k, v int) bool { if k != v { t.Fatalf("%dth key is not equal to value %d", k, v) } return true }) } func TestMapOfCompute(t *testing.T) { m := NewMapOf[string, int]() // Store a new value. v, ok := m.Compute("foobar", func(oldValue int, loaded bool) (newValue int, delete bool) { if oldValue != 0 { t.Fatalf("oldValue should be 0 when computing a new value: %d", oldValue) } if loaded { t.Fatal("loaded should be false when computing a new value") } newValue = 42 delete = false return }) if v != 42 { t.Fatalf("v should be 42 when computing a new value: %d", v) } if !ok { t.Fatal("ok should be true when computing a new value") } // Update an existing value. v, ok = m.Compute("foobar", func(oldValue int, loaded bool) (newValue int, delete bool) { if oldValue != 42 { t.Fatalf("oldValue should be 42 when updating the value: %d", oldValue) } if !loaded { t.Fatal("loaded should be true when updating the value") } newValue = oldValue + 42 delete = false return }) if v != 84 { t.Fatalf("v should be 84 when updating the value: %d", v) } if !ok { t.Fatal("ok should be true when updating the value") } // Delete an existing value. v, ok = m.Compute("foobar", func(oldValue int, loaded bool) (newValue int, delete bool) { if oldValue != 84 { t.Fatalf("oldValue should be 84 when deleting the value: %d", oldValue) } if !loaded { t.Fatal("loaded should be true when deleting the value") } delete = true return }) if v != 84 { t.Fatalf("v should be 84 when deleting the value: %d", v) } if ok { t.Fatal("ok should be false when deleting the value") } // Try to delete a non-existing value. Notice different key. v, ok = m.Compute("barbaz", func(oldValue int, loaded bool) (newValue int, delete bool) { if oldValue != 0 { t.Fatalf("oldValue should be 0 when trying to delete a non-existing value: %d", oldValue) } if loaded { t.Fatal("loaded should be false when trying to delete a non-existing value") } // We're returning a non-zero value, but the map should ignore it. newValue = 42 delete = true return }) if v != 0 { t.Fatalf("v should be 0 when trying to delete a non-existing value: %d", v) } if ok { t.Fatal("ok should be false when trying to delete a non-existing value") } } func TestMapOfStringStoreThenDelete(t *testing.T) { const numEntries = 1000 m := NewMapOf[string, int]() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } for i := 0; i < numEntries; i++ { m.Delete(strconv.Itoa(i)) if _, ok := m.Load(strconv.Itoa(i)); ok { t.Fatalf("value was not expected for %d", i) } } } func TestMapOfIntStoreThenDelete(t *testing.T) { const numEntries = 1000 m := NewMapOf[int32, int32]() for i := 0; i < numEntries; i++ { m.Store(int32(i), int32(i)) } for i := 0; i < numEntries; i++ { m.Delete(int32(i)) if _, ok := m.Load(int32(i)); ok { t.Fatalf("value was not expected for %d", i) } } } func TestMapOfStructStoreThenDelete(t *testing.T) { const numEntries = 1000 m := NewMapOf[point, string]() for i := 0; i < numEntries; i++ { m.Store(point{int32(i), 42}, strconv.Itoa(i)) } for i := 0; i < numEntries; i++ { m.Delete(point{int32(i), 42}) if _, ok := m.Load(point{int32(i), 42}); ok { t.Fatalf("value was not expected for %d", i) } } } func TestMapOfStringStoreThenLoadAndDelete(t *testing.T) { const numEntries = 1000 m := NewMapOf[string, int]() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } for i := 0; i < numEntries; i++ { if v, loaded := m.LoadAndDelete(strconv.Itoa(i)); !loaded || v != i { t.Fatalf("value was not found or different for %d: %v", i, v) } if _, ok := m.Load(strconv.Itoa(i)); ok { t.Fatalf("value was not expected for %d", i) } } } func TestMapOfIntStoreThenLoadAndDelete(t *testing.T) { const numEntries = 1000 m := NewMapOf[int, int]() for i := 0; i < numEntries; i++ { m.Store(i, i) } for i := 0; i < numEntries; i++ { if _, loaded := m.LoadAndDelete(i); !loaded { t.Fatalf("value was not found for %d", i) } if _, ok := m.Load(i); ok { t.Fatalf("value was not expected for %d", i) } } } func TestMapOfStructStoreThenLoadAndDelete(t *testing.T) { const numEntries = 1000 m := NewMapOf[point, int]() for i := 0; i < numEntries; i++ { m.Store(point{42, int32(i)}, i) } for i := 0; i < numEntries; i++ { if _, loaded := m.LoadAndDelete(point{42, int32(i)}); !loaded { t.Fatalf("value was not found for %d", i) } if _, ok := m.Load(point{42, int32(i)}); ok { t.Fatalf("value was not expected for %d", i) } } } func TestMapOfStoreThenParallelDelete_DoesNotShrinkBelowMinTableLen(t *testing.T) { const numEntries = 1000 m := NewMapOf[int, int]() for i := 0; i < numEntries; i++ { m.Store(i, i) } cdone := make(chan bool) go func() { for i := 0; i < numEntries; i++ { m.Delete(i) } cdone <- true }() go func() { for i := 0; i < numEntries; i++ { m.Delete(i) } cdone <- true }() // Wait for the goroutines to finish. <-cdone <-cdone stats := m.Stats() if stats.RootBuckets != DefaultMinMapTableLen { t.Fatalf("table length was different from the minimum: %d", stats.RootBuckets) } } func sizeBasedOnTypedRange(m *MapOf[string, int]) int { size := 0 m.Range(func(key string, value int) bool { size++ return true }) return size } func TestMapOfSize(t *testing.T) { const numEntries = 1000 m := NewMapOf[string, int]() size := m.Size() if size != 0 { t.Fatalf("zero size expected: %d", size) } expectedSize := 0 for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) expectedSize++ size := m.Size() if size != expectedSize { t.Fatalf("size of %d was expected, got: %d", expectedSize, size) } rsize := sizeBasedOnTypedRange(m) if size != rsize { t.Fatalf("size does not match number of entries in Range: %v, %v", size, rsize) } } for i := 0; i < numEntries; i++ { m.Delete(strconv.Itoa(i)) expectedSize-- size := m.Size() if size != expectedSize { t.Fatalf("size of %d was expected, got: %d", expectedSize, size) } rsize := sizeBasedOnTypedRange(m) if size != rsize { t.Fatalf("size does not match number of entries in Range: %v, %v", size, rsize) } } } func TestMapOfClear(t *testing.T) { const numEntries = 1000 m := NewMapOf[string, int]() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } size := m.Size() if size != numEntries { t.Fatalf("size of %d was expected, got: %d", numEntries, size) } m.Clear() size = m.Size() if size != 0 { t.Fatalf("zero size was expected, got: %d", size) } rsize := sizeBasedOnTypedRange(m) if rsize != 0 { t.Fatalf("zero number of entries in Range was expected, got: %d", rsize) } } func assertMapOfCapacity[K comparable, V any](t *testing.T, m *MapOf[K, V], expectedCap int) { stats := m.Stats() if stats.Capacity != expectedCap { t.Fatalf("capacity was different from %d: %d", expectedCap, stats.Capacity) } } func TestNewMapOfPresized(t *testing.T) { assertMapOfCapacity(t, NewMapOf[string, string](), DefaultMinMapOfTableCap) assertMapOfCapacity(t, NewMapOfPresized[string, string](0), DefaultMinMapOfTableCap) assertMapOfCapacity(t, NewMapOf[string, string](WithPresize(0)), DefaultMinMapOfTableCap) assertMapOfCapacity(t, NewMapOfPresized[string, string](-100), DefaultMinMapOfTableCap) assertMapOfCapacity(t, NewMapOf[string, string](WithPresize(-100)), DefaultMinMapOfTableCap) assertMapOfCapacity(t, NewMapOfPresized[string, string](500), 1280) assertMapOfCapacity(t, NewMapOf[string, string](WithPresize(500)), 1280) assertMapOfCapacity(t, NewMapOfPresized[int, int](1_000_000), 2621440) assertMapOfCapacity(t, NewMapOf[int, int](WithPresize(1_000_000)), 2621440) assertMapOfCapacity(t, NewMapOfPresized[point, point](100), 160) assertMapOfCapacity(t, NewMapOf[point, point](WithPresize(100)), 160) } func TestNewMapOfPresized_DoesNotShrinkBelowMinTableLen(t *testing.T) { const minTableLen = 1024 const numEntries = int(minTableLen * EntriesPerMapOfBucket * MapLoadFactor) m := NewMapOf[int, int](WithPresize(numEntries)) for i := 0; i < 2*numEntries; i++ { m.Store(i, i) } stats := m.Stats() if stats.RootBuckets <= minTableLen { t.Fatalf("table did not grow: %d", stats.RootBuckets) } for i := 0; i < 2*numEntries; i++ { m.Delete(i) } stats = m.Stats() if stats.RootBuckets != minTableLen { t.Fatalf("table length was different from the minimum: %d", stats.RootBuckets) } } func TestNewMapOfGrowOnly_OnlyShrinksOnClear(t *testing.T) { const minTableLen = 128 const numEntries = minTableLen * EntriesPerMapOfBucket m := NewMapOf[int, int](WithPresize(numEntries), WithGrowOnly()) stats := m.Stats() initialTableLen := stats.RootBuckets for i := 0; i < 2*numEntries; i++ { m.Store(i, i) } stats = m.Stats() maxTableLen := stats.RootBuckets if maxTableLen <= minTableLen { t.Fatalf("table did not grow: %d", maxTableLen) } for i := 0; i < numEntries; i++ { m.Delete(i) } stats = m.Stats() if stats.RootBuckets != maxTableLen { t.Fatalf("table length was different from the expected: %d", stats.RootBuckets) } m.Clear() stats = m.Stats() if stats.RootBuckets != initialTableLen { t.Fatalf("table length was different from the initial: %d", stats.RootBuckets) } } func TestMapOfResize(t *testing.T) { const numEntries = 100_000 m := NewMapOf[string, int]() for i := 0; i < numEntries; i++ { m.Store(strconv.Itoa(i), i) } stats := m.Stats() if stats.Size != numEntries { t.Fatalf("size was too small: %d", stats.Size) } expectedCapacity := int(math.RoundToEven(MapLoadFactor+1)) * stats.RootBuckets * EntriesPerMapOfBucket if stats.Capacity > expectedCapacity { t.Fatalf("capacity was too large: %d, expected: %d", stats.Capacity, expectedCapacity) } if stats.RootBuckets <= DefaultMinMapTableLen { t.Fatalf("table was too small: %d", stats.RootBuckets) } if stats.TotalGrowths == 0 { t.Fatalf("non-zero total growths expected: %d", stats.TotalGrowths) } if stats.TotalShrinks > 0 { t.Fatalf("zero total shrinks expected: %d", stats.TotalShrinks) } // This is useful when debugging table resize and occupancy. // Use -v flag to see the output. t.Log(stats.ToString()) for i := 0; i < numEntries; i++ { m.Delete(strconv.Itoa(i)) } stats = m.Stats() if stats.Size > 0 { t.Fatalf("zero size was expected: %d", stats.Size) } expectedCapacity = stats.RootBuckets * EntriesPerMapOfBucket if stats.Capacity != expectedCapacity { t.Fatalf("capacity was too large: %d, expected: %d", stats.Capacity, expectedCapacity) } if stats.RootBuckets != DefaultMinMapTableLen { t.Fatalf("table was too large: %d", stats.RootBuckets) } if stats.TotalShrinks == 0 { t.Fatalf("non-zero total shrinks expected: %d", stats.TotalShrinks) } t.Log(stats.ToString()) } func TestMapOfResize_CounterLenLimit(t *testing.T) { const numEntries = 1_000_000 m := NewMapOf[string, string]() for i := 0; i < numEntries; i++ { m.Store("foo"+strconv.Itoa(i), "bar"+strconv.Itoa(i)) } stats := m.Stats() if stats.Size != numEntries { t.Fatalf("size was too small: %d", stats.Size) } if stats.CounterLen != MaxMapCounterLen { t.Fatalf("number of counter stripes was too large: %d, expected: %d", stats.CounterLen, MaxMapCounterLen) } } func parallelSeqTypedResizer(m *MapOf[int, int], numEntries int, positive bool, cdone chan bool) { for i := 0; i < numEntries; i++ { if positive { m.Store(i, i) } else { m.Store(-i, -i) } } cdone <- true } func TestMapOfParallelResize_GrowOnly(t *testing.T) { const numEntries = 100_000 m := NewMapOf[int, int]() cdone := make(chan bool) go parallelSeqTypedResizer(m, numEntries, true, cdone) go parallelSeqTypedResizer(m, numEntries, false, cdone) // Wait for the goroutines to finish. <-cdone <-cdone // Verify map contents. for i := -numEntries + 1; i < numEntries; i++ { v, ok := m.Load(i) if !ok { t.Fatalf("value not found for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } if s := m.Size(); s != 2*numEntries-1 { t.Fatalf("unexpected size: %v", s) } } func parallelRandTypedResizer(t *testing.T, m *MapOf[string, int], numIters, numEntries int, cdone chan bool) { r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < numIters; i++ { coin := r.Int63n(2) for j := 0; j < numEntries; j++ { if coin == 1 { m.Store(strconv.Itoa(j), j) } else { m.Delete(strconv.Itoa(j)) } } } cdone <- true } func TestMapOfParallelResize(t *testing.T) { const numIters = 1_000 const numEntries = 2 * EntriesPerMapOfBucket * DefaultMinMapTableLen m := NewMapOf[string, int]() cdone := make(chan bool) go parallelRandTypedResizer(t, m, numIters, numEntries, cdone) go parallelRandTypedResizer(t, m, numIters, numEntries, cdone) // Wait for the goroutines to finish. <-cdone <-cdone // Verify map contents. for i := 0; i < numEntries; i++ { v, ok := m.Load(strconv.Itoa(i)) if !ok { // The entry may be deleted and that's ok. continue } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } s := m.Size() if s > numEntries { t.Fatalf("unexpected size: %v", s) } rs := sizeBasedOnTypedRange(m) if s != rs { t.Fatalf("size does not match number of entries in Range: %v, %v", s, rs) } } func parallelRandTypedClearer(t *testing.T, m *MapOf[string, int], numIters, numEntries int, cdone chan bool) { r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < numIters; i++ { coin := r.Int63n(2) for j := 0; j < numEntries; j++ { if coin == 1 { m.Store(strconv.Itoa(j), j) } else { m.Clear() } } } cdone <- true } func TestMapOfParallelClear(t *testing.T) { const numIters = 100 const numEntries = 1_000 m := NewMapOf[string, int]() cdone := make(chan bool) go parallelRandTypedClearer(t, m, numIters, numEntries, cdone) go parallelRandTypedClearer(t, m, numIters, numEntries, cdone) // Wait for the goroutines to finish. <-cdone <-cdone // Verify map size. s := m.Size() if s > numEntries { t.Fatalf("unexpected size: %v", s) } rs := sizeBasedOnTypedRange(m) if s != rs { t.Fatalf("size does not match number of entries in Range: %v, %v", s, rs) } } func parallelSeqTypedStorer(t *testing.T, m *MapOf[string, int], storeEach, numIters, numEntries int, cdone chan bool) { for i := 0; i < numIters; i++ { for j := 0; j < numEntries; j++ { if storeEach == 0 || j%storeEach == 0 { m.Store(strconv.Itoa(j), j) // Due to atomic snapshots we must see a ""/j pair. v, ok := m.Load(strconv.Itoa(j)) if !ok { t.Errorf("value was not found for %d", j) break } if v != j { t.Errorf("value was not expected for %d: %d", j, v) break } } } } cdone <- true } func TestMapOfParallelStores(t *testing.T) { const numStorers = 4 const numIters = 10_000 const numEntries = 100 m := NewMapOf[string, int]() cdone := make(chan bool) for i := 0; i < numStorers; i++ { go parallelSeqTypedStorer(t, m, i, numIters, numEntries, cdone) } // Wait for the goroutines to finish. for i := 0; i < numStorers; i++ { <-cdone } // Verify map contents. for i := 0; i < numEntries; i++ { v, ok := m.Load(strconv.Itoa(i)) if !ok { t.Fatalf("value not found for %d", i) } if v != i { t.Fatalf("values do not match for %d: %v", i, v) } } } func parallelRandTypedStorer(t *testing.T, m *MapOf[string, int], numIters, numEntries int, cdone chan bool) { r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < numIters; i++ { j := r.Intn(numEntries) if v, loaded := m.LoadOrStore(strconv.Itoa(j), j); loaded { if v != j { t.Errorf("value was not expected for %d: %d", j, v) } } } cdone <- true } func parallelRandTypedDeleter(t *testing.T, m *MapOf[string, int], numIters, numEntries int, cdone chan bool) { r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < numIters; i++ { j := r.Intn(numEntries) if v, loaded := m.LoadAndDelete(strconv.Itoa(j)); loaded { if v != j { t.Errorf("value was not expected for %d: %d", j, v) } } } cdone <- true } func parallelTypedLoader(t *testing.T, m *MapOf[string, int], numIters, numEntries int, cdone chan bool) { for i := 0; i < numIters; i++ { for j := 0; j < numEntries; j++ { // Due to atomic snapshots we must either see no entry, or a ""/j pair. if v, ok := m.Load(strconv.Itoa(j)); ok { if v != j { t.Errorf("value was not expected for %d: %d", j, v) } } } } cdone <- true } func TestMapOfAtomicSnapshot(t *testing.T) { const numIters = 100_000 const numEntries = 100 m := NewMapOf[string, int]() cdone := make(chan bool) // Update or delete random entry in parallel with loads. go parallelRandTypedStorer(t, m, numIters, numEntries, cdone) go parallelRandTypedDeleter(t, m, numIters, numEntries, cdone) go parallelTypedLoader(t, m, numIters, numEntries, cdone) // Wait for the goroutines to finish. for i := 0; i < 3; i++ { <-cdone } } func TestMapOfParallelStoresAndDeletes(t *testing.T) { const numWorkers = 2 const numIters = 100_000 const numEntries = 1000 m := NewMapOf[string, int]() cdone := make(chan bool) // Update random entry in parallel with deletes. for i := 0; i < numWorkers; i++ { go parallelRandTypedStorer(t, m, numIters, numEntries, cdone) go parallelRandTypedDeleter(t, m, numIters, numEntries, cdone) } // Wait for the goroutines to finish. for i := 0; i < 2*numWorkers; i++ { <-cdone } } func parallelTypedComputer(m *MapOf[uint64, uint64], numIters, numEntries int, cdone chan bool) { for i := 0; i < numIters; i++ { for j := 0; j < numEntries; j++ { m.Compute(uint64(j), func(oldValue uint64, loaded bool) (newValue uint64, delete bool) { return oldValue + 1, false }) } } cdone <- true } func TestMapOfParallelComputes(t *testing.T) { const numWorkers = 4 // Also stands for numEntries. const numIters = 10_000 m := NewMapOf[uint64, uint64]() cdone := make(chan bool) for i := 0; i < numWorkers; i++ { go parallelTypedComputer(m, numIters, numWorkers, cdone) } // Wait for the goroutines to finish. for i := 0; i < numWorkers; i++ { <-cdone } // Verify map contents. for i := 0; i < numWorkers; i++ { v, ok := m.Load(uint64(i)) if !ok { t.Fatalf("value not found for %d", i) } if v != numWorkers*numIters { t.Fatalf("values do not match for %d: %v", i, v) } } } func parallelTypedRangeStorer(m *MapOf[int, int], numEntries int, stopFlag *int64, cdone chan bool) { for { for i := 0; i < numEntries; i++ { m.Store(i, i) } if atomic.LoadInt64(stopFlag) != 0 { break } } cdone <- true } func parallelTypedRangeDeleter(m *MapOf[int, int], numEntries int, stopFlag *int64, cdone chan bool) { for { for i := 0; i < numEntries; i++ { m.Delete(i) } if atomic.LoadInt64(stopFlag) != 0 { break } } cdone <- true } func TestMapOfParallelRange(t *testing.T) { const numEntries = 10_000 m := NewMapOfPresized[int, int](numEntries) for i := 0; i < numEntries; i++ { m.Store(i, i) } // Start goroutines that would be storing and deleting items in parallel. cdone := make(chan bool) stopFlag := int64(0) go parallelTypedRangeStorer(m, numEntries, &stopFlag, cdone) go parallelTypedRangeDeleter(m, numEntries, &stopFlag, cdone) // Iterate the map and verify that no duplicate keys were met. met := make(map[int]int) m.Range(func(key int, value int) bool { if key != value { t.Fatalf("got unexpected value for key %d: %d", key, value) return false } met[key] += 1 return true }) if len(met) == 0 { t.Fatal("no entries were met when iterating") } for k, c := range met { if c != 1 { t.Fatalf("met key %d multiple times: %d", k, c) } } // Make sure that both goroutines finish. atomic.StoreInt64(&stopFlag, 1) <-cdone <-cdone } func parallelTypedShrinker(t *testing.T, m *MapOf[uint64, *point], numIters, numEntries int, stopFlag *int64, cdone chan bool) { for i := 0; i < numIters; i++ { for j := 0; j < numEntries; j++ { if p, loaded := m.LoadOrStore(uint64(j), &point{int32(j), int32(j)}); loaded { t.Errorf("value was present for %d: %v", j, p) } } for j := 0; j < numEntries; j++ { m.Delete(uint64(j)) } } atomic.StoreInt64(stopFlag, 1) cdone <- true } func parallelTypedUpdater(t *testing.T, m *MapOf[uint64, *point], idx int, stopFlag *int64, cdone chan bool) { for atomic.LoadInt64(stopFlag) != 1 { sleepUs := int(Fastrand() % 10) if p, loaded := m.LoadOrStore(uint64(idx), &point{int32(idx), int32(idx)}); loaded { t.Errorf("value was present for %d: %v", idx, p) } time.Sleep(time.Duration(sleepUs) * time.Microsecond) if _, ok := m.Load(uint64(idx)); !ok { t.Errorf("value was not found for %d", idx) } m.Delete(uint64(idx)) } cdone <- true } func TestMapOfDoesNotLoseEntriesOnResize(t *testing.T) { const numIters = 10_000 const numEntries = 128 m := NewMapOf[uint64, *point]() cdone := make(chan bool) stopFlag := int64(0) go parallelTypedShrinker(t, m, numIters, numEntries, &stopFlag, cdone) go parallelTypedUpdater(t, m, numEntries, &stopFlag, cdone) // Wait for the goroutines to finish. <-cdone <-cdone // Verify map contents. if s := m.Size(); s != 0 { t.Fatalf("map is not empty: %d", s) } } func TestMapOfStats(t *testing.T) { m := NewMapOf[int, int]() stats := m.Stats() if stats.RootBuckets != DefaultMinMapTableLen { t.Fatalf("unexpected number of root buckets: %d", stats.RootBuckets) } if stats.TotalBuckets != stats.RootBuckets { t.Fatalf("unexpected number of total buckets: %d", stats.TotalBuckets) } if stats.EmptyBuckets != stats.RootBuckets { t.Fatalf("unexpected number of empty buckets: %d", stats.EmptyBuckets) } if stats.Capacity != EntriesPerMapOfBucket*DefaultMinMapTableLen { t.Fatalf("unexpected capacity: %d", stats.Capacity) } if stats.Size != 0 { t.Fatalf("unexpected size: %d", stats.Size) } if stats.Counter != 0 { t.Fatalf("unexpected counter: %d", stats.Counter) } if stats.CounterLen != 8 { t.Fatalf("unexpected counter length: %d", stats.CounterLen) } for i := 0; i < 200; i++ { m.Store(i, i) } stats = m.Stats() if stats.RootBuckets != 2*DefaultMinMapTableLen { t.Fatalf("unexpected number of root buckets: %d", stats.RootBuckets) } if stats.TotalBuckets < stats.RootBuckets { t.Fatalf("unexpected number of total buckets: %d", stats.TotalBuckets) } if stats.EmptyBuckets >= stats.RootBuckets { t.Fatalf("unexpected number of empty buckets: %d", stats.EmptyBuckets) } if stats.Capacity < 2*EntriesPerMapOfBucket*DefaultMinMapTableLen { t.Fatalf("unexpected capacity: %d", stats.Capacity) } if stats.Size != 200 { t.Fatalf("unexpected size: %d", stats.Size) } if stats.Counter != 200 { t.Fatalf("unexpected counter: %d", stats.Counter) } if stats.CounterLen != 8 { t.Fatalf("unexpected counter length: %d", stats.CounterLen) } } func TestToPlainMapOf_NilPointer(t *testing.T) { pm := ToPlainMapOf[int, int](nil) if len(pm) != 0 { t.Fatalf("got unexpected size of nil map copy: %d", len(pm)) } } func TestToPlainMapOf(t *testing.T) { const numEntries = 1000 m := NewMapOf[int, int]() for i := 0; i < numEntries; i++ { m.Store(i, i) } pm := ToPlainMapOf[int, int](m) if len(pm) != numEntries { t.Fatalf("got unexpected size of nil map copy: %d", len(pm)) } for i := 0; i < numEntries; i++ { if v := pm[i]; v != i { t.Fatalf("unexpected value for key %d: %d", i, v) } } } func BenchmarkMapOf_NoWarmUp(b *testing.B) { for _, bc := range benchmarkCases { if bc.readPercentage == 100 { // This benchmark doesn't make sense without a warm-up. continue } b.Run(bc.name, func(b *testing.B) { m := NewMapOf[string, int]() benchmarkMapOfStringKeys(b, func(k string) (int, bool) { return m.Load(k) }, func(k string, v int) { m.Store(k, v) }, func(k string) { m.Delete(k) }, bc.readPercentage) }) } } func BenchmarkMapOf_WarmUp(b *testing.B) { for _, bc := range benchmarkCases { b.Run(bc.name, func(b *testing.B) { m := NewMapOfPresized[string, int](benchmarkNumEntries) for i := 0; i < benchmarkNumEntries; i++ { m.Store(benchmarkKeyPrefix+strconv.Itoa(i), i) } b.ResetTimer() benchmarkMapOfStringKeys(b, func(k string) (int, bool) { return m.Load(k) }, func(k string, v int) { m.Store(k, v) }, func(k string) { m.Delete(k) }, bc.readPercentage) }) } } func benchmarkMapOfStringKeys( b *testing.B, loadFn func(k string) (int, bool), storeFn func(k string, v int), deleteFn func(k string), readPercentage int, ) { runParallel(b, func(pb *testing.PB) { // convert percent to permille to support 99% case storeThreshold := 10 * readPercentage deleteThreshold := 10*readPercentage + ((1000 - 10*readPercentage) / 2) for pb.Next() { op := int(Fastrand() % 1000) i := int(Fastrand() % benchmarkNumEntries) if op >= deleteThreshold { deleteFn(benchmarkKeys[i]) } else if op >= storeThreshold { storeFn(benchmarkKeys[i], i) } else { loadFn(benchmarkKeys[i]) } } }) } func BenchmarkMapOfInt_NoWarmUp(b *testing.B) { for _, bc := range benchmarkCases { if bc.readPercentage == 100 { // This benchmark doesn't make sense without a warm-up. continue } b.Run(bc.name, func(b *testing.B) { m := NewMapOf[int, int]() benchmarkMapOfIntKeys(b, func(k int) (int, bool) { return m.Load(k) }, func(k int, v int) { m.Store(k, v) }, func(k int) { m.Delete(k) }, bc.readPercentage) }) } } func BenchmarkMapOfInt_WarmUp(b *testing.B) { for _, bc := range benchmarkCases { b.Run(bc.name, func(b *testing.B) { m := NewMapOfPresized[int, int](benchmarkNumEntries) for i := 0; i < benchmarkNumEntries; i++ { m.Store(i, i) } b.ResetTimer() benchmarkMapOfIntKeys(b, func(k int) (int, bool) { return m.Load(k) }, func(k int, v int) { m.Store(k, v) }, func(k int) { m.Delete(k) }, bc.readPercentage) }) } } func BenchmarkMapOfInt_Murmur3Finalizer_WarmUp(b *testing.B) { for _, bc := range benchmarkCases { b.Run(bc.name, func(b *testing.B) { m := NewMapOfWithHasher[int, int](murmur3Finalizer, WithPresize(benchmarkNumEntries)) for i := 0; i < benchmarkNumEntries; i++ { m.Store(i, i) } b.ResetTimer() benchmarkMapOfIntKeys(b, func(k int) (int, bool) { return m.Load(k) }, func(k int, v int) { m.Store(k, v) }, func(k int) { m.Delete(k) }, bc.readPercentage) }) } } func BenchmarkIntMapStandard_NoWarmUp(b *testing.B) { for _, bc := range benchmarkCases { if bc.readPercentage == 100 { // This benchmark doesn't make sense without a warm-up. continue } b.Run(bc.name, func(b *testing.B) { var m sync.Map benchmarkMapOfIntKeys(b, func(k int) (value int, ok bool) { v, ok := m.Load(k) if ok { return v.(int), ok } else { return 0, false } }, func(k int, v int) { m.Store(k, v) }, func(k int) { m.Delete(k) }, bc.readPercentage) }) } } // This is a nice scenario for sync.Map since a lot of updates // will hit the readOnly part of the map. func BenchmarkIntMapStandard_WarmUp(b *testing.B) { for _, bc := range benchmarkCases { b.Run(bc.name, func(b *testing.B) { var m sync.Map for i := 0; i < benchmarkNumEntries; i++ { m.Store(i, i) } b.ResetTimer() benchmarkMapOfIntKeys(b, func(k int) (value int, ok bool) { v, ok := m.Load(k) if ok { return v.(int), ok } else { return 0, false } }, func(k int, v int) { m.Store(k, v) }, func(k int) { m.Delete(k) }, bc.readPercentage) }) } } func benchmarkMapOfIntKeys( b *testing.B, loadFn func(k int) (int, bool), storeFn func(k int, v int), deleteFn func(k int), readPercentage int, ) { runParallel(b, func(pb *testing.PB) { // convert percent to permille to support 99% case storeThreshold := 10 * readPercentage deleteThreshold := 10*readPercentage + ((1000 - 10*readPercentage) / 2) for pb.Next() { op := int(Fastrand() % 1000) i := int(Fastrand() % benchmarkNumEntries) if op >= deleteThreshold { deleteFn(i) } else if op >= storeThreshold { storeFn(i, i) } else { loadFn(i) } } }) } func BenchmarkMapOfRange(b *testing.B) { m := NewMapOfPresized[string, int](benchmarkNumEntries) for i := 0; i < benchmarkNumEntries; i++ { m.Store(benchmarkKeys[i], i) } b.ResetTimer() runParallel(b, func(pb *testing.PB) { foo := 0 for pb.Next() { m.Range(func(key string, value int) bool { foo++ return true }) _ = foo } }) } xsync-3.5.0/mpmcqueue.go000066400000000000000000000062101474520662300152230ustar00rootroot00000000000000package xsync import ( "runtime" "sync/atomic" "unsafe" ) // A MPMCQueue is a bounded multi-producer multi-consumer concurrent // queue. // // MPMCQueue instances must be created with NewMPMCQueue function. // A MPMCQueue must not be copied after first use. // // Based on the data structure from the following C++ library: // https://github.com/rigtorp/MPMCQueue type MPMCQueue struct { cap uint64 head uint64 //lint:ignore U1000 prevents false sharing hpad [cacheLineSize - 8]byte tail uint64 //lint:ignore U1000 prevents false sharing tpad [cacheLineSize - 8]byte slots []slotPadded } type slotPadded struct { slot //lint:ignore U1000 prevents false sharing pad [cacheLineSize - unsafe.Sizeof(slot{})]byte } type slot struct { turn uint64 item interface{} } // NewMPMCQueue creates a new MPMCQueue instance with the given // capacity. func NewMPMCQueue(capacity int) *MPMCQueue { if capacity < 1 { panic("capacity must be positive number") } return &MPMCQueue{ cap: uint64(capacity), slots: make([]slotPadded, capacity), } } // Enqueue inserts the given item into the queue. // Blocks, if the queue is full. // // Deprecated: use TryEnqueue in combination with runtime.Gosched(). func (q *MPMCQueue) Enqueue(item interface{}) { head := atomic.AddUint64(&q.head, 1) - 1 slot := &q.slots[q.idx(head)] turn := q.turn(head) * 2 for atomic.LoadUint64(&slot.turn) != turn { runtime.Gosched() } slot.item = item atomic.StoreUint64(&slot.turn, turn+1) } // Dequeue retrieves and removes the item from the head of the queue. // Blocks, if the queue is empty. // // Deprecated: use TryDequeue in combination with runtime.Gosched(). func (q *MPMCQueue) Dequeue() interface{} { tail := atomic.AddUint64(&q.tail, 1) - 1 slot := &q.slots[q.idx(tail)] turn := q.turn(tail)*2 + 1 for atomic.LoadUint64(&slot.turn) != turn { runtime.Gosched() } item := slot.item slot.item = nil atomic.StoreUint64(&slot.turn, turn+1) return item } // TryEnqueue inserts the given item into the queue. Does not block // and returns immediately. The result indicates that the queue isn't // full and the item was inserted. func (q *MPMCQueue) TryEnqueue(item interface{}) bool { head := atomic.LoadUint64(&q.head) slot := &q.slots[q.idx(head)] turn := q.turn(head) * 2 if atomic.LoadUint64(&slot.turn) == turn { if atomic.CompareAndSwapUint64(&q.head, head, head+1) { slot.item = item atomic.StoreUint64(&slot.turn, turn+1) return true } } return false } // TryDequeue retrieves and removes the item from the head of the // queue. Does not block and returns immediately. The ok result // indicates that the queue isn't empty and an item was retrieved. func (q *MPMCQueue) TryDequeue() (item interface{}, ok bool) { tail := atomic.LoadUint64(&q.tail) slot := &q.slots[q.idx(tail)] turn := q.turn(tail)*2 + 1 if atomic.LoadUint64(&slot.turn) == turn { if atomic.CompareAndSwapUint64(&q.tail, tail, tail+1) { item = slot.item ok = true slot.item = nil atomic.StoreUint64(&slot.turn, turn+1) return } } return } func (q *MPMCQueue) idx(i uint64) uint64 { return i % q.cap } func (q *MPMCQueue) turn(i uint64) uint64 { return i / q.cap } xsync-3.5.0/mpmcqueue_test.go000066400000000000000000000154631474520662300162740ustar00rootroot00000000000000// Copyright notice. The following tests are partially based on // the following file from the Go Programming Language core repo: // https://github.com/golang/go/blob/831f9376d8d730b16fb33dfd775618dffe13ce7a/src/runtime/chan_test.go package xsync_test import ( "runtime" "sync" "sync/atomic" "testing" "time" . "github.com/puzpuzpuz/xsync/v3" ) func TestMPMCQueue_InvalidSize(t *testing.T) { defer func() { recover() }() NewMPMCQueue(0) t.Fatal("no panic detected") } func TestMPMCQueueEnqueueDequeue(t *testing.T) { q := NewMPMCQueue(10) for i := 0; i < 10; i++ { q.Enqueue(i) } for i := 0; i < 10; i++ { if got := q.Dequeue(); got != i { t.Fatalf("got %v, want %d", got, i) } } } func TestMPMCQueueEnqueueBlocksOnFull(t *testing.T) { q := NewMPMCQueue(1) q.Enqueue("foo") cdone := make(chan bool) flag := int32(0) go func() { q.Enqueue("bar") if atomic.LoadInt32(&flag) == 0 { t.Error("enqueue on full queue didn't wait for dequeue") } cdone <- true }() time.Sleep(50 * time.Millisecond) atomic.StoreInt32(&flag, 1) if got := q.Dequeue(); got != "foo" { t.Fatalf("got %v, want foo", got) } <-cdone } func TestMPMCQueueDequeueBlocksOnEmpty(t *testing.T) { q := NewMPMCQueue(2) cdone := make(chan bool) flag := int32(0) go func() { q.Dequeue() if atomic.LoadInt32(&flag) == 0 { t.Error("dequeue on empty queue didn't wait for enqueue") } cdone <- true }() time.Sleep(50 * time.Millisecond) atomic.StoreInt32(&flag, 1) q.Enqueue("foobar") <-cdone } func TestMPMCQueueTryEnqueueDequeue(t *testing.T) { q := NewMPMCQueue(10) for i := 0; i < 10; i++ { if !q.TryEnqueue(i) { t.Fatalf("failed to enqueue for %d", i) } } for i := 0; i < 10; i++ { if got, ok := q.TryDequeue(); !ok || got != i { t.Fatalf("got %v, want %d, for status %v", got, i, ok) } } } func TestMPMCQueueTryEnqueueOnFull(t *testing.T) { q := NewMPMCQueue(1) if !q.TryEnqueue("foo") { t.Error("failed to enqueue initial item") } if q.TryEnqueue("bar") { t.Error("got success for enqueue on full queue") } } func TestMPMCQueueTryDequeueOnEmpty(t *testing.T) { q := NewMPMCQueue(2) if _, ok := q.TryDequeue(); ok { t.Error("got success for enqueue on empty queue") } } func hammerMPMCQueueBlockingCalls(t *testing.T, gomaxprocs, numOps, numThreads int) { runtime.GOMAXPROCS(gomaxprocs) q := NewMPMCQueue(numThreads) startwg := sync.WaitGroup{} startwg.Add(1) csum := make(chan int, numThreads) // Start producers. for i := 0; i < numThreads; i++ { go func(n int) { startwg.Wait() for j := n; j < numOps; j += numThreads { q.Enqueue(j) } }(i) } // Start consumers. for i := 0; i < numThreads; i++ { go func(n int) { startwg.Wait() sum := 0 for j := n; j < numOps; j += numThreads { item := q.Dequeue() sum += item.(int) } csum <- sum }(i) } startwg.Done() // Wait for all the sums from producers. sum := 0 for i := 0; i < numThreads; i++ { s := <-csum sum += s } // Assert the total sum. expectedSum := numOps * (numOps - 1) / 2 if sum != expectedSum { t.Fatalf("sums don't match for %d num ops, %d num threads: got %d, want %d", numOps, numThreads, sum, expectedSum) } } func TestMPMCQueueBlockingCalls(t *testing.T) { defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(-1)) n := 100 if testing.Short() { n = 10 } hammerMPMCQueueBlockingCalls(t, 1, 100*n, n) hammerMPMCQueueBlockingCalls(t, 1, 1000*n, 10*n) hammerMPMCQueueBlockingCalls(t, 4, 100*n, n) hammerMPMCQueueBlockingCalls(t, 4, 1000*n, 10*n) hammerMPMCQueueBlockingCalls(t, 8, 100*n, n) hammerMPMCQueueBlockingCalls(t, 8, 1000*n, 10*n) } func hammerMPMCQueueNonBlockingCalls(t *testing.T, gomaxprocs, numOps, numThreads int) { runtime.GOMAXPROCS(gomaxprocs) q := NewMPMCQueue(numThreads) startwg := sync.WaitGroup{} startwg.Add(1) csum := make(chan int, numThreads) // Start producers. for i := 0; i < numThreads; i++ { go func(n int) { startwg.Wait() for j := n; j < numOps; j += numThreads { for !q.TryEnqueue(j) { // busy spin until success } } }(i) } // Start consumers. for i := 0; i < numThreads; i++ { go func(n int) { startwg.Wait() sum := 0 for j := n; j < numOps; j += numThreads { var ( item interface{} ok bool ) for { // busy spin until success if item, ok = q.TryDequeue(); ok { sum += item.(int) break } } } csum <- sum }(i) } startwg.Done() // Wait for all the sums from producers. sum := 0 for i := 0; i < numThreads; i++ { s := <-csum sum += s } // Assert the total sum. expectedSum := numOps * (numOps - 1) / 2 if sum != expectedSum { t.Fatalf("sums don't match for %d num ops, %d num threads: got %d, want %d", numOps, numThreads, sum, expectedSum) } } func TestMPMCQueueNonBlockingCalls(t *testing.T) { defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(-1)) n := 10 if testing.Short() { n = 1 } hammerMPMCQueueNonBlockingCalls(t, 1, n, n) hammerMPMCQueueNonBlockingCalls(t, 2, 10*n, 2*n) hammerMPMCQueueNonBlockingCalls(t, 4, 100*n, 4*n) } func benchmarkMPMCQueue(b *testing.B, queueSize, localWork int) { callsPerSched := queueSize procs := runtime.GOMAXPROCS(-1) / 2 if procs == 0 { procs = 1 } N := int32(b.N / callsPerSched) c := make(chan bool, 2*procs) q := NewMPMCQueue(queueSize) for p := 0; p < procs; p++ { go func() { foo := 0 for atomic.AddInt32(&N, -1) >= 0 { for g := 0; g < callsPerSched; g++ { for i := 0; i < localWork; i++ { foo *= 2 foo /= 2 } q.Enqueue(1) } } q.Enqueue(0) c <- foo == 42 }() go func() { foo := 0 for { v := q.Dequeue().(int) if v == 0 { break } for i := 0; i < localWork; i++ { foo *= 2 foo /= 2 } } c <- foo == 42 }() } for p := 0; p < procs; p++ { <-c <-c } } func BenchmarkMPMCQueue(b *testing.B) { benchmarkMPMCQueue(b, 1000, 0) } func BenchmarkMPMCQueueWork100(b *testing.B) { benchmarkMPMCQueue(b, 1000, 100) } func benchmarkMPMCChan(b *testing.B, chanSize, localWork int) { callsPerSched := chanSize procs := runtime.GOMAXPROCS(-1) / 2 if procs == 0 { procs = 1 } N := int32(b.N / callsPerSched) c := make(chan bool, 2*procs) myc := make(chan int, chanSize) for p := 0; p < procs; p++ { go func() { foo := 0 for atomic.AddInt32(&N, -1) >= 0 { for g := 0; g < callsPerSched; g++ { for i := 0; i < localWork; i++ { foo *= 2 foo /= 2 } myc <- 1 } } myc <- 0 c <- foo == 42 }() go func() { foo := 0 for { v := <-myc if v == 0 { break } for i := 0; i < localWork; i++ { foo *= 2 foo /= 2 } } c <- foo == 42 }() } for p := 0; p < procs; p++ { <-c <-c } } func BenchmarkMPMCChan(b *testing.B) { benchmarkMPMCChan(b, 1000, 0) } func BenchmarkMPMCChanWork100(b *testing.B) { benchmarkMPMCChan(b, 1000, 100) } xsync-3.5.0/mpmcqueueof.go000066400000000000000000000070051474520662300155530ustar00rootroot00000000000000//go:build go1.19 // +build go1.19 package xsync import ( "runtime" "sync/atomic" "unsafe" ) // A MPMCQueueOf is a bounded multi-producer multi-consumer concurrent // queue. It's a generic version of MPMCQueue. // // MPMCQueueOf instances must be created with NewMPMCQueueOf function. // A MPMCQueueOf must not be copied after first use. // // Based on the data structure from the following C++ library: // https://github.com/rigtorp/MPMCQueue type MPMCQueueOf[I any] struct { cap uint64 head uint64 //lint:ignore U1000 prevents false sharing hpad [cacheLineSize - 8]byte tail uint64 //lint:ignore U1000 prevents false sharing tpad [cacheLineSize - 8]byte slots []slotOfPadded[I] } type slotOfPadded[I any] struct { slotOf[I] // Unfortunately, proper padding like the below one: // // pad [cacheLineSize - (unsafe.Sizeof(slotOf[I]{}) % cacheLineSize)]byte // // won't compile, so here we add a best-effort padding for items up to // 56 bytes size. //lint:ignore U1000 prevents false sharing pad [cacheLineSize - unsafe.Sizeof(atomic.Uint64{})]byte } type slotOf[I any] struct { // atomic.Uint64 is used here to get proper 8 byte alignment on // 32-bit archs. turn atomic.Uint64 item I } // NewMPMCQueueOf creates a new MPMCQueueOf instance with the given // capacity. func NewMPMCQueueOf[I any](capacity int) *MPMCQueueOf[I] { if capacity < 1 { panic("capacity must be positive number") } return &MPMCQueueOf[I]{ cap: uint64(capacity), slots: make([]slotOfPadded[I], capacity), } } // Enqueue inserts the given item into the queue. // Blocks, if the queue is full. // // Deprecated: use TryEnqueue in combination with runtime.Gosched(). func (q *MPMCQueueOf[I]) Enqueue(item I) { head := atomic.AddUint64(&q.head, 1) - 1 slot := &q.slots[q.idx(head)] turn := q.turn(head) * 2 for slot.turn.Load() != turn { runtime.Gosched() } slot.item = item slot.turn.Store(turn + 1) } // Dequeue retrieves and removes the item from the head of the queue. // Blocks, if the queue is empty. // // Deprecated: use TryDequeue in combination with runtime.Gosched(). func (q *MPMCQueueOf[I]) Dequeue() I { var zeroI I tail := atomic.AddUint64(&q.tail, 1) - 1 slot := &q.slots[q.idx(tail)] turn := q.turn(tail)*2 + 1 for slot.turn.Load() != turn { runtime.Gosched() } item := slot.item slot.item = zeroI slot.turn.Store(turn + 1) return item } // TryEnqueue inserts the given item into the queue. Does not block // and returns immediately. The result indicates that the queue isn't // full and the item was inserted. func (q *MPMCQueueOf[I]) TryEnqueue(item I) bool { head := atomic.LoadUint64(&q.head) slot := &q.slots[q.idx(head)] turn := q.turn(head) * 2 if slot.turn.Load() == turn { if atomic.CompareAndSwapUint64(&q.head, head, head+1) { slot.item = item slot.turn.Store(turn + 1) return true } } return false } // TryDequeue retrieves and removes the item from the head of the // queue. Does not block and returns immediately. The ok result // indicates that the queue isn't empty and an item was retrieved. func (q *MPMCQueueOf[I]) TryDequeue() (item I, ok bool) { tail := atomic.LoadUint64(&q.tail) slot := &q.slots[q.idx(tail)] turn := q.turn(tail)*2 + 1 if slot.turn.Load() == turn { if atomic.CompareAndSwapUint64(&q.tail, tail, tail+1) { var zeroI I item = slot.item ok = true slot.item = zeroI slot.turn.Store(turn + 1) return } } return } func (q *MPMCQueueOf[I]) idx(i uint64) uint64 { return i % q.cap } func (q *MPMCQueueOf[I]) turn(i uint64) uint64 { return i / q.cap } xsync-3.5.0/mpmcqueueof_test.go000066400000000000000000000160421474520662300166130ustar00rootroot00000000000000//go:build go1.19 // +build go1.19 // Copyright notice. The following tests are partially based on // the following file from the Go Programming Language core repo: // https://github.com/golang/go/blob/831f9376d8d730b16fb33dfd775618dffe13ce7a/src/runtime/chan_test.go package xsync_test import ( "runtime" "strconv" "sync" "sync/atomic" "testing" "time" . "github.com/puzpuzpuz/xsync/v3" ) func TestMPMCQueueOf_InvalidSize(t *testing.T) { defer func() { recover() }() NewMPMCQueueOf[int](0) t.Fatal("no panic detected") } func TestMPMCQueueOfEnqueueDequeueInt(t *testing.T) { q := NewMPMCQueueOf[int](10) for i := 0; i < 10; i++ { q.Enqueue(i) } for i := 0; i < 10; i++ { if got := q.Dequeue(); got != i { t.Fatalf("got %v, want %d", got, i) } } } func TestMPMCQueueOfEnqueueDequeueString(t *testing.T) { q := NewMPMCQueueOf[string](10) for i := 0; i < 10; i++ { q.Enqueue(strconv.Itoa(i)) } for i := 0; i < 10; i++ { if got := q.Dequeue(); got != strconv.Itoa(i) { t.Fatalf("got %v, want %d", got, i) } } } func TestMPMCQueueOfEnqueueDequeueStruct(t *testing.T) { type foo struct { bar int baz int } q := NewMPMCQueueOf[foo](10) for i := 0; i < 10; i++ { q.Enqueue(foo{i, i}) } for i := 0; i < 10; i++ { if got := q.Dequeue(); got.bar != i || got.baz != i { t.Fatalf("got %v, want %d", got, i) } } } func TestMPMCQueueOfEnqueueDequeueStructRef(t *testing.T) { type foo struct { bar int baz int } q := NewMPMCQueueOf[*foo](11) for i := 0; i < 10; i++ { q.Enqueue(&foo{i, i}) } q.Enqueue(nil) for i := 0; i < 10; i++ { if got := q.Dequeue(); got.bar != i || got.baz != i { t.Fatalf("got %v, want %d", got, i) } } if last := q.Dequeue(); last != nil { t.Fatalf("got %v, want nil", last) } } func TestMPMCQueueOfEnqueueBlocksOnFull(t *testing.T) { q := NewMPMCQueueOf[string](1) q.Enqueue("foo") cdone := make(chan bool) flag := int32(0) go func() { q.Enqueue("bar") if atomic.LoadInt32(&flag) == 0 { t.Error("enqueue on full queue didn't wait for dequeue") } cdone <- true }() time.Sleep(50 * time.Millisecond) atomic.StoreInt32(&flag, 1) if got := q.Dequeue(); got != "foo" { t.Fatalf("got %v, want foo", got) } <-cdone } func TestMPMCQueueOfDequeueBlocksOnEmpty(t *testing.T) { q := NewMPMCQueueOf[string](2) cdone := make(chan bool) flag := int32(0) go func() { q.Dequeue() if atomic.LoadInt32(&flag) == 0 { t.Error("dequeue on empty queue didn't wait for enqueue") } cdone <- true }() time.Sleep(50 * time.Millisecond) atomic.StoreInt32(&flag, 1) q.Enqueue("foobar") <-cdone } func TestMPMCQueueOfTryEnqueueDequeue(t *testing.T) { q := NewMPMCQueueOf[int](10) for i := 0; i < 10; i++ { if !q.TryEnqueue(i) { t.Fatalf("failed to enqueue for %d", i) } } for i := 0; i < 10; i++ { if got, ok := q.TryDequeue(); !ok || got != i { t.Fatalf("got %v, want %d, for status %v", got, i, ok) } } } func TestMPMCQueueOfTryEnqueueOnFull(t *testing.T) { q := NewMPMCQueueOf[string](1) if !q.TryEnqueue("foo") { t.Error("failed to enqueue initial item") } if q.TryEnqueue("bar") { t.Error("got success for enqueue on full queue") } } func TestMPMCQueueOfTryDequeueOnEmpty(t *testing.T) { q := NewMPMCQueueOf[int](2) if _, ok := q.TryDequeue(); ok { t.Error("got success for enqueue on empty queue") } } func hammerMPMCQueueOfBlockingCalls(t *testing.T, gomaxprocs, numOps, numThreads int) { runtime.GOMAXPROCS(gomaxprocs) q := NewMPMCQueueOf[int](numThreads) startwg := sync.WaitGroup{} startwg.Add(1) csum := make(chan int, numThreads) // Start producers. for i := 0; i < numThreads; i++ { go func(n int) { startwg.Wait() for j := n; j < numOps; j += numThreads { q.Enqueue(j) } }(i) } // Start consumers. for i := 0; i < numThreads; i++ { go func(n int) { startwg.Wait() sum := 0 for j := n; j < numOps; j += numThreads { item := q.Dequeue() sum += item } csum <- sum }(i) } startwg.Done() // Wait for all the sums from producers. sum := 0 for i := 0; i < numThreads; i++ { s := <-csum sum += s } // Assert the total sum. expectedSum := numOps * (numOps - 1) / 2 if sum != expectedSum { t.Fatalf("sums don't match for %d num ops, %d num threads: got %d, want %d", numOps, numThreads, sum, expectedSum) } } func TestMPMCQueueOfBlockingCalls(t *testing.T) { defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(-1)) n := 100 if testing.Short() { n = 10 } hammerMPMCQueueOfBlockingCalls(t, 1, 100*n, n) hammerMPMCQueueOfBlockingCalls(t, 1, 1000*n, 10*n) hammerMPMCQueueOfBlockingCalls(t, 4, 100*n, n) hammerMPMCQueueOfBlockingCalls(t, 4, 1000*n, 10*n) hammerMPMCQueueOfBlockingCalls(t, 8, 100*n, n) hammerMPMCQueueOfBlockingCalls(t, 8, 1000*n, 10*n) } func hammerMPMCQueueOfNonBlockingCalls(t *testing.T, gomaxprocs, numOps, numThreads int) { runtime.GOMAXPROCS(gomaxprocs) q := NewMPMCQueueOf[int](numThreads) startwg := sync.WaitGroup{} startwg.Add(1) csum := make(chan int, numThreads) // Start producers. for i := 0; i < numThreads; i++ { go func(n int) { startwg.Wait() for j := n; j < numOps; j += numThreads { for !q.TryEnqueue(j) { // busy spin until success } } }(i) } // Start consumers. for i := 0; i < numThreads; i++ { go func(n int) { startwg.Wait() sum := 0 for j := n; j < numOps; j += numThreads { var ( item int ok bool ) for { // busy spin until success if item, ok = q.TryDequeue(); ok { sum += item break } } } csum <- sum }(i) } startwg.Done() // Wait for all the sums from producers. sum := 0 for i := 0; i < numThreads; i++ { s := <-csum sum += s } // Assert the total sum. expectedSum := numOps * (numOps - 1) / 2 if sum != expectedSum { t.Fatalf("sums don't match for %d num ops, %d num threads: got %d, want %d", numOps, numThreads, sum, expectedSum) } } func TestMPMCQueueOfNonBlockingCalls(t *testing.T) { defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(-1)) n := 10 if testing.Short() { n = 1 } hammerMPMCQueueOfNonBlockingCalls(t, 1, n, n) hammerMPMCQueueOfNonBlockingCalls(t, 2, 10*n, 2*n) hammerMPMCQueueOfNonBlockingCalls(t, 4, 100*n, 4*n) } func benchmarkMPMCQueueOf(b *testing.B, queueSize, localWork int) { callsPerSched := queueSize procs := runtime.GOMAXPROCS(-1) / 2 if procs == 0 { procs = 1 } N := int32(b.N / callsPerSched) c := make(chan bool, 2*procs) q := NewMPMCQueueOf[int](queueSize) for p := 0; p < procs; p++ { go func() { foo := 0 for atomic.AddInt32(&N, -1) >= 0 { for g := 0; g < callsPerSched; g++ { for i := 0; i < localWork; i++ { foo *= 2 foo /= 2 } q.Enqueue(1) } } q.Enqueue(0) c <- foo == 42 }() go func() { foo := 0 for { v := q.Dequeue() if v == 0 { break } for i := 0; i < localWork; i++ { foo *= 2 foo /= 2 } } c <- foo == 42 }() } for p := 0; p < procs; p++ { <-c <-c } } func BenchmarkMPMCQueueOf(b *testing.B) { benchmarkMPMCQueueOf(b, 1000, 0) } func BenchmarkMPMCQueueOfWork100(b *testing.B) { benchmarkMPMCQueueOf(b, 1000, 100) } xsync-3.5.0/rbmutex.go000066400000000000000000000117401474520662300147140ustar00rootroot00000000000000package xsync import ( "runtime" "sync" "sync/atomic" "time" ) // slow-down guard const nslowdown = 7 // pool for reader tokens var rtokenPool sync.Pool // RToken is a reader lock token. type RToken struct { slot uint32 //lint:ignore U1000 prevents false sharing pad [cacheLineSize - 4]byte } // A RBMutex is a reader biased reader/writer mutual exclusion lock. // The lock can be held by an many readers or a single writer. // The zero value for a RBMutex is an unlocked mutex. // // A RBMutex must not be copied after first use. // // RBMutex is based on a modified version of BRAVO // (Biased Locking for Reader-Writer Locks) algorithm: // https://arxiv.org/pdf/1810.01553.pdf // // RBMutex is a specialized mutex for scenarios, such as caches, // where the vast majority of locks are acquired by readers and write // lock acquire attempts are infrequent. In such scenarios, RBMutex // performs better than sync.RWMutex on large multicore machines. // // RBMutex extends sync.RWMutex internally and uses it as the "reader // bias disabled" fallback, so the same semantics apply. The only // noticeable difference is in reader tokens returned from the // RLock/RUnlock methods. type RBMutex struct { rslots []rslot rmask uint32 rbias int32 inhibitUntil time.Time rw sync.RWMutex } type rslot struct { mu int32 //lint:ignore U1000 prevents false sharing pad [cacheLineSize - 4]byte } // NewRBMutex creates a new RBMutex instance. func NewRBMutex() *RBMutex { nslots := nextPowOf2(parallelism()) mu := RBMutex{ rslots: make([]rslot, nslots), rmask: nslots - 1, rbias: 1, } return &mu } // TryRLock tries to lock m for reading without blocking. // When TryRLock succeeds, it returns true and a reader token. // In case of a failure, a false is returned. func (mu *RBMutex) TryRLock() (bool, *RToken) { if t := mu.fastRlock(); t != nil { return true, t } // Optimistic slow path. if mu.rw.TryRLock() { if atomic.LoadInt32(&mu.rbias) == 0 && time.Now().After(mu.inhibitUntil) { atomic.StoreInt32(&mu.rbias, 1) } return true, nil } return false, nil } // RLock locks m for reading and returns a reader token. The // token must be used in the later RUnlock call. // // Should not be used for recursive read locking; a blocked Lock // call excludes new readers from acquiring the lock. func (mu *RBMutex) RLock() *RToken { if t := mu.fastRlock(); t != nil { return t } // Slow path. mu.rw.RLock() if atomic.LoadInt32(&mu.rbias) == 0 && time.Now().After(mu.inhibitUntil) { atomic.StoreInt32(&mu.rbias, 1) } return nil } func (mu *RBMutex) fastRlock() *RToken { if atomic.LoadInt32(&mu.rbias) == 1 { t, ok := rtokenPool.Get().(*RToken) if !ok { t = new(RToken) t.slot = runtime_fastrand() } // Try all available slots to distribute reader threads to slots. for i := 0; i < len(mu.rslots); i++ { slot := t.slot + uint32(i) rslot := &mu.rslots[slot&mu.rmask] rslotmu := atomic.LoadInt32(&rslot.mu) if atomic.CompareAndSwapInt32(&rslot.mu, rslotmu, rslotmu+1) { if atomic.LoadInt32(&mu.rbias) == 1 { // Hot path succeeded. t.slot = slot return t } // The mutex is no longer reader biased. Roll back. atomic.AddInt32(&rslot.mu, -1) rtokenPool.Put(t) return nil } // Contention detected. Give a try with the next slot. } } return nil } // RUnlock undoes a single RLock call. A reader token obtained from // the RLock call must be provided. RUnlock does not affect other // simultaneous readers. A panic is raised if m is not locked for // reading on entry to RUnlock. func (mu *RBMutex) RUnlock(t *RToken) { if t == nil { mu.rw.RUnlock() return } if atomic.AddInt32(&mu.rslots[t.slot&mu.rmask].mu, -1) < 0 { panic("invalid reader state detected") } rtokenPool.Put(t) } // TryLock tries to lock m for writing without blocking. func (mu *RBMutex) TryLock() bool { if mu.rw.TryLock() { if atomic.LoadInt32(&mu.rbias) == 1 { atomic.StoreInt32(&mu.rbias, 0) for i := 0; i < len(mu.rslots); i++ { if atomic.LoadInt32(&mu.rslots[i].mu) > 0 { // There is a reader. Roll back. atomic.StoreInt32(&mu.rbias, 1) mu.rw.Unlock() return false } } } return true } return false } // Lock locks m for writing. If the lock is already locked for // reading or writing, Lock blocks until the lock is available. func (mu *RBMutex) Lock() { mu.rw.Lock() if atomic.LoadInt32(&mu.rbias) == 1 { atomic.StoreInt32(&mu.rbias, 0) start := time.Now() for i := 0; i < len(mu.rslots); i++ { for atomic.LoadInt32(&mu.rslots[i].mu) > 0 { runtime.Gosched() } } mu.inhibitUntil = time.Now().Add(time.Since(start) * nslowdown) } } // Unlock unlocks m for writing. A panic is raised if m is not locked // for writing on entry to Unlock. // // As with RWMutex, a locked RBMutex is not associated with a // particular goroutine. One goroutine may RLock (Lock) a RBMutex and // then arrange for another goroutine to RUnlock (Unlock) it. func (mu *RBMutex) Unlock() { mu.rw.Unlock() } xsync-3.5.0/rbmutex_test.go000066400000000000000000000204421474520662300157520ustar00rootroot00000000000000// Copyright notice. Initial version of the following tests was based on // the following file from the Go Programming Language core repo: // https://github.com/golang/go/blob/831f9376d8d730b16fb33dfd775618dffe13ce7a/src/sync/rwmutex_test.go package xsync_test import ( "fmt" "runtime" "sync" "sync/atomic" "testing" . "github.com/puzpuzpuz/xsync/v3" ) func TestRBMutexSerialReader(t *testing.T) { const numCalls = 10 mu := NewRBMutex() for i := 0; i < 3; i++ { var rtokens [numCalls]*RToken for j := 0; j < numCalls; j++ { rtokens[j] = mu.RLock() } for j := 0; j < numCalls; j++ { mu.RUnlock(rtokens[j]) } } } func TestRBMutexSerialOptimisticReader(t *testing.T) { const numCalls = 10 mu := NewRBMutex() for i := 0; i < 3; i++ { var rtokens [numCalls]*RToken for j := 0; j < numCalls; j++ { ok, rt := mu.TryRLock() if !ok { t.Fatalf("TryRLock failed for %d", j) } if rt == nil { t.Fatalf("nil reader token for %d", j) } rtokens[j] = rt } for j := 0; j < numCalls; j++ { mu.RUnlock(rtokens[j]) } } } func TestRBMutexSerialOptimisticWriter(t *testing.T) { mu := NewRBMutex() for i := 0; i < 3; i++ { if !mu.TryLock() { t.Fatal("TryLock failed") } mu.Unlock() } } func parallelReader(mu *RBMutex, clocked, cunlock, cdone chan bool) { t := mu.RLock() clocked <- true <-cunlock mu.RUnlock(t) cdone <- true } func doTestParallelReaders(numReaders, gomaxprocs int) { runtime.GOMAXPROCS(gomaxprocs) mu := NewRBMutex() clocked := make(chan bool) cunlock := make(chan bool) cdone := make(chan bool) for i := 0; i < numReaders; i++ { go parallelReader(mu, clocked, cunlock, cdone) } // Wait for all parallel RLock()s to succeed. for i := 0; i < numReaders; i++ { <-clocked } for i := 0; i < numReaders; i++ { cunlock <- true } // Wait for the goroutines to finish. for i := 0; i < numReaders; i++ { <-cdone } } func TestRBMutexParallelReaders(t *testing.T) { defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(0)) doTestParallelReaders(1, 4) doTestParallelReaders(3, 4) doTestParallelReaders(4, 2) } func reader(mu *RBMutex, numIterations int, activity *int32, cdone chan bool) { for i := 0; i < numIterations; i++ { t := mu.RLock() n := atomic.AddInt32(activity, 1) if n < 1 || n >= 10000 { mu.RUnlock(t) panic(fmt.Sprintf("rlock(%d)\n", n)) } for i := 0; i < 100; i++ { } atomic.AddInt32(activity, -1) mu.RUnlock(t) } cdone <- true } func writer(mu *RBMutex, numIterations int, activity *int32, cdone chan bool) { for i := 0; i < numIterations; i++ { mu.Lock() n := atomic.AddInt32(activity, 10000) if n != 10000 { mu.Unlock() panic(fmt.Sprintf("wlock(%d)\n", n)) } for i := 0; i < 100; i++ { } atomic.AddInt32(activity, -10000) mu.Unlock() } cdone <- true } func hammerRBMutex(gomaxprocs, numReaders, numIterations int) { runtime.GOMAXPROCS(gomaxprocs) // Number of active readers + 10000 * number of active writers. var activity int32 mu := NewRBMutex() cdone := make(chan bool) go writer(mu, numIterations, &activity, cdone) var i int for i = 0; i < numReaders/2; i++ { go reader(mu, numIterations, &activity, cdone) } go writer(mu, numIterations, &activity, cdone) for ; i < numReaders; i++ { go reader(mu, numIterations, &activity, cdone) } // Wait for the 2 writers and all readers to finish. for i := 0; i < 2+numReaders; i++ { <-cdone } } func TestRBMutex(t *testing.T) { const n = 1000 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(0)) hammerRBMutex(1, 1, n) hammerRBMutex(1, 3, n) hammerRBMutex(1, 10, n) hammerRBMutex(4, 1, n) hammerRBMutex(4, 3, n) hammerRBMutex(4, 10, n) hammerRBMutex(10, 1, n) hammerRBMutex(10, 3, n) hammerRBMutex(10, 10, n) hammerRBMutex(10, 5, n) } func optimisticReader(mu *RBMutex, numIterations int, activity *int32, cdone chan bool) { for i := 0; i < numIterations; i++ { if ok, t := mu.TryRLock(); ok { n := atomic.AddInt32(activity, 1) if n < 1 || n >= 10000 { mu.RUnlock(t) panic(fmt.Sprintf("rlock(%d)\n", n)) } for i := 0; i < 100; i++ { } atomic.AddInt32(activity, -1) mu.RUnlock(t) } } cdone <- true } func optimisticWriter(mu *RBMutex, numIterations int, activity *int32, cdone chan bool) { for i := 0; i < numIterations; i++ { if mu.TryLock() { n := atomic.AddInt32(activity, 10000) if n != 10000 { mu.Unlock() panic(fmt.Sprintf("wlock(%d)\n", n)) } for i := 0; i < 100; i++ { } atomic.AddInt32(activity, -10000) mu.Unlock() } } cdone <- true } func hammerOptimisticRBMutex(gomaxprocs, numReaders, numIterations int) { runtime.GOMAXPROCS(gomaxprocs) // Number of active readers + 10000 * number of active writers. var activity int32 mu := NewRBMutex() cdone := make(chan bool) go optimisticWriter(mu, numIterations, &activity, cdone) var i int for i = 0; i < numReaders/2; i++ { go optimisticReader(mu, numIterations, &activity, cdone) } go optimisticWriter(mu, numIterations, &activity, cdone) for ; i < numReaders; i++ { go optimisticReader(mu, numIterations, &activity, cdone) } // Wait for the 2 writers and all readers to finish. for i := 0; i < 2+numReaders; i++ { <-cdone } } func TestRBMutex_Optimistic(t *testing.T) { const n = 1000 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(0)) hammerOptimisticRBMutex(1, 1, n) hammerOptimisticRBMutex(1, 3, n) hammerOptimisticRBMutex(1, 10, n) hammerOptimisticRBMutex(4, 1, n) hammerOptimisticRBMutex(4, 3, n) hammerOptimisticRBMutex(4, 10, n) hammerOptimisticRBMutex(10, 1, n) hammerOptimisticRBMutex(10, 3, n) hammerOptimisticRBMutex(10, 10, n) hammerOptimisticRBMutex(10, 5, n) } func hammerMixedRBMutex(gomaxprocs, numReaders, numIterations int) { runtime.GOMAXPROCS(gomaxprocs) // Number of active readers + 10000 * number of active writers. var activity int32 mu := NewRBMutex() cdone := make(chan bool) go writer(mu, numIterations, &activity, cdone) var i int for i = 0; i < numReaders/2; i++ { go reader(mu, numIterations, &activity, cdone) } go optimisticWriter(mu, numIterations, &activity, cdone) for ; i < numReaders; i++ { go optimisticReader(mu, numIterations, &activity, cdone) } // Wait for the 2 writers and all readers to finish. for i := 0; i < 2+numReaders; i++ { <-cdone } } func TestRBMutex_Mixed(t *testing.T) { const n = 1000 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(0)) hammerMixedRBMutex(1, 1, n) hammerMixedRBMutex(1, 3, n) hammerMixedRBMutex(1, 10, n) hammerMixedRBMutex(4, 1, n) hammerMixedRBMutex(4, 3, n) hammerMixedRBMutex(4, 10, n) hammerMixedRBMutex(10, 1, n) hammerMixedRBMutex(10, 3, n) hammerMixedRBMutex(10, 10, n) hammerMixedRBMutex(10, 5, n) } func benchmarkRBMutex(b *testing.B, parallelism, localWork, writeRatio int) { mu := NewRBMutex() b.SetParallelism(parallelism) runParallel(b, func(pb *testing.PB) { foo := 0 for pb.Next() { foo++ if writeRatio > 0 && foo%writeRatio == 0 { mu.Lock() for i := 0; i != localWork; i += 1 { foo *= 2 foo /= 2 } mu.Unlock() } else { tk := mu.RLock() for i := 0; i != localWork; i += 1 { foo *= 2 foo /= 2 } mu.RUnlock(tk) } } _ = foo }) } func BenchmarkRBMutexWorkReadOnly_HighParallelism(b *testing.B) { benchmarkRBMutex(b, 1024, 100, -1) } func BenchmarkRBMutexWorkReadOnly(b *testing.B) { benchmarkRBMutex(b, -1, 100, -1) } func BenchmarkRBMutexWorkWrite100000(b *testing.B) { benchmarkRBMutex(b, -1, 100, 100000) } func BenchmarkRBMutexWorkWrite1000(b *testing.B) { benchmarkRBMutex(b, -1, 100, 1000) } func benchmarkRWMutex(b *testing.B, parallelism, localWork, writeRatio int) { var mu sync.RWMutex b.SetParallelism(parallelism) runParallel(b, func(pb *testing.PB) { foo := 0 for pb.Next() { foo++ if writeRatio > 0 && foo%writeRatio == 0 { mu.Lock() for i := 0; i != localWork; i += 1 { foo *= 2 foo /= 2 } mu.Unlock() } else { mu.RLock() for i := 0; i != localWork; i += 1 { foo *= 2 foo /= 2 } mu.RUnlock() } } _ = foo }) } func BenchmarkRWMutexWorkReadOnly_HighParallelism(b *testing.B) { benchmarkRWMutex(b, 1024, 100, -1) } func BenchmarkRWMutexWorkReadOnly(b *testing.B) { benchmarkRWMutex(b, -1, 100, -1) } func BenchmarkRWMutexWorkWrite100000(b *testing.B) { benchmarkRWMutex(b, -1, 100, 100000) } func BenchmarkRWMutexWorkWrite1000(b *testing.B) { benchmarkRWMutex(b, -1, 100, 1000) } xsync-3.5.0/spscqueue.go000066400000000000000000000047061474520662300152470ustar00rootroot00000000000000package xsync import ( "sync/atomic" ) // A SPSCQueue is a bounded single-producer single-consumer concurrent // queue. This means that not more than a single goroutine must be // publishing items to the queue while not more than a single goroutine // must be consuming those items. // // SPSCQueue instances must be created with NewSPSCQueue function. // A SPSCQueue must not be copied after first use. // // Based on the data structure from the following article: // https://rigtorp.se/ringbuffer/ type SPSCQueue struct { cap uint64 pidx uint64 //lint:ignore U1000 prevents false sharing pad0 [cacheLineSize - 8]byte pcachedIdx uint64 //lint:ignore U1000 prevents false sharing pad1 [cacheLineSize - 8]byte cidx uint64 //lint:ignore U1000 prevents false sharing pad2 [cacheLineSize - 8]byte ccachedIdx uint64 //lint:ignore U1000 prevents false sharing pad3 [cacheLineSize - 8]byte items []interface{} } // NewSPSCQueue creates a new SPSCQueue instance with the given // capacity. func NewSPSCQueue(capacity int) *SPSCQueue { if capacity < 1 { panic("capacity must be positive number") } return &SPSCQueue{ cap: uint64(capacity + 1), items: make([]interface{}, capacity+1), } } // TryEnqueue inserts the given item into the queue. Does not block // and returns immediately. The result indicates that the queue isn't // full and the item was inserted. func (q *SPSCQueue) TryEnqueue(item interface{}) bool { // relaxed memory order would be enough here idx := atomic.LoadUint64(&q.pidx) nextIdx := idx + 1 if nextIdx == q.cap { nextIdx = 0 } cachedIdx := q.ccachedIdx if nextIdx == cachedIdx { cachedIdx = atomic.LoadUint64(&q.cidx) q.ccachedIdx = cachedIdx if nextIdx == cachedIdx { return false } } q.items[idx] = item atomic.StoreUint64(&q.pidx, nextIdx) return true } // TryDequeue retrieves and removes the item from the head of the // queue. Does not block and returns immediately. The ok result // indicates that the queue isn't empty and an item was retrieved. func (q *SPSCQueue) TryDequeue() (item interface{}, ok bool) { // relaxed memory order would be enough here idx := atomic.LoadUint64(&q.cidx) cachedIdx := q.pcachedIdx if idx == cachedIdx { cachedIdx = atomic.LoadUint64(&q.pidx) q.pcachedIdx = cachedIdx if idx == cachedIdx { return } } item = q.items[idx] q.items[idx] = nil ok = true nextIdx := idx + 1 if nextIdx == q.cap { nextIdx = 0 } atomic.StoreUint64(&q.cidx, nextIdx) return } xsync-3.5.0/spscqueue_test.go000066400000000000000000000043021474520662300162760ustar00rootroot00000000000000// Copyright notice. The following tests are partially based on // the following file from the Go Programming Language core repo: // https://github.com/golang/go/blob/831f9376d8d730b16fb33dfd775618dffe13ce7a/src/runtime/chan_test.go package xsync_test import ( "sync" "testing" . "github.com/puzpuzpuz/xsync/v3" ) func TestSPSCQueue_InvalidSize(t *testing.T) { defer func() { recover() }() NewSPSCQueue(0) t.Fatal("no panic detected") } func TestSPSCQueueTryEnqueueDequeue(t *testing.T) { q := NewSPSCQueue(10) for i := 0; i < 10; i++ { if !q.TryEnqueue(i) { t.Fatal("TryEnqueue failed") } } for i := 0; i < 10; i++ { if got, ok := q.TryDequeue(); !ok || got != i { t.Fatalf("%v: got %v, want %d", ok, got, i) } } } func TestSPSCQueueTryEnqueueOnFull(t *testing.T) { q := NewSPSCQueue(1) if !q.TryEnqueue("foo") { t.Error("failed to enqueue initial item") } if q.TryEnqueue("bar") { t.Error("got success for enqueue on full queue") } } func TestSPSCQueueTryDequeueOnEmpty(t *testing.T) { q := NewSPSCQueue(2) if _, ok := q.TryDequeue(); ok { t.Error("got success for enqueue on empty queue") } } func hammerSPSCQueueNonBlockingCalls(t *testing.T, cap, numOps int) { q := NewSPSCQueue(cap) startwg := sync.WaitGroup{} startwg.Add(1) csum := make(chan int, 2) // Start producer. go func() { startwg.Wait() for j := 0; j < numOps; j++ { for !q.TryEnqueue(j) { // busy spin until success } } }() // Start consumer. go func() { startwg.Wait() sum := 0 for j := 0; j < numOps; j++ { var ( item interface{} ok bool ) for { // busy spin until success if item, ok = q.TryDequeue(); ok { sum += item.(int) break } } } csum <- sum }() startwg.Done() // Wait for all the sum from the producer. sum := <-csum // Assert the total sum. expectedSum := numOps * (numOps - 1) / 2 if sum != expectedSum { t.Fatalf("sums don't match for %d num ops: got %d, want %d", numOps, sum, expectedSum) } } func TestSPSCQueueNonBlockingCalls(t *testing.T) { n := 10 if testing.Short() { n = 1 } hammerSPSCQueueNonBlockingCalls(t, 1, n) hammerSPSCQueueNonBlockingCalls(t, 2, 2*n) hammerSPSCQueueNonBlockingCalls(t, 4, 4*n) } xsync-3.5.0/spscqueueof.go000066400000000000000000000050271474520662300155710ustar00rootroot00000000000000//go:build go1.19 // +build go1.19 package xsync import ( "sync/atomic" ) // A SPSCQueueOf is a bounded single-producer single-consumer concurrent // queue. This means that not more than a single goroutine must be // publishing items to the queue while not more than a single goroutine // must be consuming those items. // // SPSCQueueOf instances must be created with NewSPSCQueueOf function. // A SPSCQueueOf must not be copied after first use. // // Based on the data structure from the following article: // https://rigtorp.se/ringbuffer/ type SPSCQueueOf[I any] struct { cap uint64 pidx uint64 //lint:ignore U1000 prevents false sharing pad0 [cacheLineSize - 8]byte pcachedIdx uint64 //lint:ignore U1000 prevents false sharing pad1 [cacheLineSize - 8]byte cidx uint64 //lint:ignore U1000 prevents false sharing pad2 [cacheLineSize - 8]byte ccachedIdx uint64 //lint:ignore U1000 prevents false sharing pad3 [cacheLineSize - 8]byte items []I } // NewSPSCQueueOf creates a new SPSCQueueOf instance with the given // capacity. func NewSPSCQueueOf[I any](capacity int) *SPSCQueueOf[I] { if capacity < 1 { panic("capacity must be positive number") } return &SPSCQueueOf[I]{ cap: uint64(capacity + 1), items: make([]I, capacity+1), } } // TryEnqueue inserts the given item into the queue. Does not block // and returns immediately. The result indicates that the queue isn't // full and the item was inserted. func (q *SPSCQueueOf[I]) TryEnqueue(item I) bool { // relaxed memory order would be enough here idx := atomic.LoadUint64(&q.pidx) next_idx := idx + 1 if next_idx == q.cap { next_idx = 0 } cached_idx := q.ccachedIdx if next_idx == cached_idx { cached_idx = atomic.LoadUint64(&q.cidx) q.ccachedIdx = cached_idx if next_idx == cached_idx { return false } } q.items[idx] = item atomic.StoreUint64(&q.pidx, next_idx) return true } // TryDequeue retrieves and removes the item from the head of the // queue. Does not block and returns immediately. The ok result // indicates that the queue isn't empty and an item was retrieved. func (q *SPSCQueueOf[I]) TryDequeue() (item I, ok bool) { // relaxed memory order would be enough here idx := atomic.LoadUint64(&q.cidx) cached_idx := q.pcachedIdx if idx == cached_idx { cached_idx = atomic.LoadUint64(&q.pidx) q.pcachedIdx = cached_idx if idx == cached_idx { return } } var zeroI I item = q.items[idx] q.items[idx] = zeroI ok = true next_idx := idx + 1 if next_idx == q.cap { next_idx = 0 } atomic.StoreUint64(&q.cidx, next_idx) return } xsync-3.5.0/spscqueueof_test.go000066400000000000000000000130271474520662300166270ustar00rootroot00000000000000//go:build go1.19 // +build go1.19 // Copyright notice. The following tests are partially based on // the following file from the Go Programming Language core repo: // https://github.com/golang/go/blob/831f9376d8d730b16fb33dfd775618dffe13ce7a/src/runtime/chan_test.go package xsync_test import ( "runtime" "strconv" "sync" "sync/atomic" "testing" . "github.com/puzpuzpuz/xsync/v3" ) func TestSPSCQueueOf_InvalidSize(t *testing.T) { defer func() { recover() }() NewSPSCQueueOf[int](0) t.Fatal("no panic detected") } func TestSPSCQueueOfTryEnqueueDequeueInt(t *testing.T) { q := NewSPSCQueueOf[int](10) for i := 0; i < 10; i++ { if !q.TryEnqueue(i) { t.Fatal("TryEnqueue failed") } } for i := 0; i < 10; i++ { if got, ok := q.TryDequeue(); !ok || got != i { t.Fatalf("%v: got %v, want %d", ok, got, i) } } } func TestSPSCQueueOfTryEnqueueDequeueString(t *testing.T) { q := NewSPSCQueueOf[string](10) for i := 0; i < 10; i++ { if !q.TryEnqueue(strconv.Itoa(i)) { t.Fatal("TryEnqueue failed") } } for i := 0; i < 10; i++ { if got, ok := q.TryDequeue(); !ok || got != strconv.Itoa(i) { t.Fatalf("%v: got %v, want %d", ok, got, i) } } } func TestSPSCQueueOfTryEnqueueDequeueStruct(t *testing.T) { type foo struct { bar int baz int } q := NewSPSCQueueOf[foo](10) for i := 0; i < 10; i++ { if !q.TryEnqueue(foo{i, i}) { t.Fatal("TryEnqueue failed") } } for i := 0; i < 10; i++ { if got, ok := q.TryDequeue(); !ok || got.bar != i || got.baz != i { t.Fatalf("%v: got %v, want %d", ok, got, i) } } } func TestSPSCQueueOfTryEnqueueDequeueStructRef(t *testing.T) { type foo struct { bar int baz int } q := NewSPSCQueueOf[*foo](11) for i := 0; i < 10; i++ { if !q.TryEnqueue(&foo{i, i}) { t.Fatal("TryEnqueue failed") } } if !q.TryEnqueue(nil) { t.Fatal("TryEnqueue with nil failed") } for i := 0; i < 10; i++ { if got, ok := q.TryDequeue(); !ok || got.bar != i || got.baz != i { t.Fatalf("%v: got %v, want %d", ok, got, i) } } if last, ok := q.TryDequeue(); !ok || last != nil { t.Fatalf("%v: got %v, want nil", ok, last) } } func TestSPSCQueueOfTryEnqueueDequeue(t *testing.T) { q := NewSPSCQueueOf[int](10) for i := 0; i < 10; i++ { if !q.TryEnqueue(i) { t.Fatalf("failed to enqueue for %d", i) } } for i := 0; i < 10; i++ { if got, ok := q.TryDequeue(); !ok || got != i { t.Fatalf("got %v, want %d, for status %v", got, i, ok) } } } func TestSPSCQueueOfTryEnqueueOnFull(t *testing.T) { q := NewSPSCQueueOf[string](1) if !q.TryEnqueue("foo") { t.Error("failed to enqueue initial item") } if q.TryEnqueue("bar") { t.Error("got success for enqueue on full queue") } } func TestSPSCQueueOfTryDequeueOnEmpty(t *testing.T) { q := NewSPSCQueueOf[int](2) if _, ok := q.TryDequeue(); ok { t.Error("got success for enqueue on empty queue") } } func hammerSPSCQueueOfNonBlockingCalls(t *testing.T, cap, numOps int) { q := NewSPSCQueueOf[int](cap) startwg := sync.WaitGroup{} startwg.Add(1) csum := make(chan int, 2) // Start producer. go func() { startwg.Wait() for j := 0; j < numOps; j++ { for !q.TryEnqueue(j) { // busy spin until success } } }() // Start consumer. go func() { startwg.Wait() sum := 0 for j := 0; j < numOps; j++ { var ( item int ok bool ) for { // busy spin until success if item, ok = q.TryDequeue(); ok { sum += item break } } } csum <- sum }() startwg.Done() // Wait for all the sum from the producer. sum := <-csum // Assert the total sum. expectedSum := numOps * (numOps - 1) / 2 if sum != expectedSum { t.Fatalf("sums don't match for %d num ops: got %d, want %d", numOps, sum, expectedSum) } } func TestSPSCQueueOfNonBlockingCalls(t *testing.T) { n := 10 if testing.Short() { n = 1 } hammerSPSCQueueOfNonBlockingCalls(t, 1, n) hammerSPSCQueueOfNonBlockingCalls(t, 2, 2*n) hammerSPSCQueueOfNonBlockingCalls(t, 4, 4*n) } func benchmarkSPSCQueueOfProdCons(b *testing.B, queueSize, localWork int) { callsPerSched := queueSize N := int32(b.N / callsPerSched) c := make(chan bool, 2) q := NewSPSCQueueOf[int](queueSize) go func() { foo := 0 for atomic.AddInt32(&N, -1) >= 0 { for g := 0; g < callsPerSched; g++ { for i := 0; i < localWork; i++ { foo *= 2 foo /= 2 } if !q.TryEnqueue(1) { runtime.Gosched() } } } q.TryEnqueue(0) c <- foo == 42 }() go func() { foo := 0 for { v, ok := q.TryDequeue() if ok { if v == 0 { break } for i := 0; i < localWork; i++ { foo *= 2 foo /= 2 } } else { runtime.Gosched() } } c <- foo == 42 }() <-c <-c } func BenchmarkSPSCQueueOfProdCons(b *testing.B) { benchmarkSPSCQueueOfProdCons(b, 1000, 0) } func BenchmarkSPSCQueueOfProdConsWork100(b *testing.B) { benchmarkSPSCQueueOfProdCons(b, 1000, 100) } func benchmarkSPSCChan(b *testing.B, chanSize, localWork int) { callsPerSched := chanSize N := int32(b.N / callsPerSched) c := make(chan bool, 2) myc := make(chan int, chanSize) go func() { foo := 0 for atomic.AddInt32(&N, -1) >= 0 { for g := 0; g < callsPerSched; g++ { for i := 0; i < localWork; i++ { foo *= 2 foo /= 2 } myc <- 1 } } myc <- 0 c <- foo == 42 }() go func() { foo := 0 for { v := <-myc if v == 0 { break } for i := 0; i < localWork; i++ { foo *= 2 foo /= 2 } } c <- foo == 42 }() <-c <-c } func BenchmarkSPSCChan(b *testing.B) { benchmarkSPSCChan(b, 1000, 0) } func BenchmarkSPSCChanWork100(b *testing.B) { benchmarkSPSCChan(b, 1000, 100) } xsync-3.5.0/util.go000066400000000000000000000026561474520662300142110ustar00rootroot00000000000000package xsync import ( "math/bits" "runtime" _ "unsafe" ) // test-only assert()-like flag var assertionsEnabled = false const ( // cacheLineSize is used in paddings to prevent false sharing; // 64B are used instead of 128B as a compromise between // memory footprint and performance; 128B usage may give ~30% // improvement on NUMA machines. cacheLineSize = 64 ) // nextPowOf2 computes the next highest power of 2 of 32-bit v. // Source: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 func nextPowOf2(v uint32) uint32 { if v == 0 { return 1 } v-- v |= v >> 1 v |= v >> 2 v |= v >> 4 v |= v >> 8 v |= v >> 16 v++ return v } func parallelism() uint32 { maxProcs := uint32(runtime.GOMAXPROCS(0)) numCores := uint32(runtime.NumCPU()) if maxProcs < numCores { return maxProcs } return numCores } //go:noescape //go:linkname runtime_fastrand runtime.fastrand func runtime_fastrand() uint32 func broadcast(b uint8) uint64 { return 0x101010101010101 * uint64(b) } func firstMarkedByteIndex(w uint64) int { return bits.TrailingZeros64(w) >> 3 } // SWAR byte search: may produce false positives, e.g. for 0x0100, // so make sure to double-check bytes found by this function. func markZeroBytes(w uint64) uint64 { return ((w - 0x0101010101010101) & (^w) & 0x8080808080808080) } func setByte(w uint64, b uint8, idx int) uint64 { shift := idx << 3 return (w &^ (0xff << shift)) | (uint64(b) << shift) } xsync-3.5.0/util_hash.go000066400000000000000000000040451474520662300152060ustar00rootroot00000000000000package xsync import ( "reflect" "unsafe" ) // makeSeed creates a random seed. func makeSeed() uint64 { var s1 uint32 for { s1 = runtime_fastrand() // We use seed 0 to indicate an uninitialized seed/hash, // so keep trying until we get a non-zero seed. if s1 != 0 { break } } s2 := runtime_fastrand() return uint64(s1)<<32 | uint64(s2) } // hashString calculates a hash of s with the given seed. func hashString(s string, seed uint64) uint64 { if s == "" { return seed } strh := (*reflect.StringHeader)(unsafe.Pointer(&s)) return uint64(runtime_memhash(unsafe.Pointer(strh.Data), uintptr(seed), uintptr(strh.Len))) } //go:noescape //go:linkname runtime_memhash runtime.memhash func runtime_memhash(p unsafe.Pointer, h, s uintptr) uintptr // defaultHasher creates a fast hash function for the given comparable type. // The only limitation is that the type should not contain interfaces inside // based on runtime.typehash. func defaultHasher[T comparable]() func(T, uint64) uint64 { var zero T if reflect.TypeOf(&zero).Elem().Kind() == reflect.Interface { return func(value T, seed uint64) uint64 { iValue := any(value) i := (*iface)(unsafe.Pointer(&iValue)) return runtime_typehash64(i.typ, i.word, seed) } } else { var iZero any = zero i := (*iface)(unsafe.Pointer(&iZero)) return func(value T, seed uint64) uint64 { return runtime_typehash64(i.typ, unsafe.Pointer(&value), seed) } } } // how interface is represented in memory type iface struct { typ uintptr word unsafe.Pointer } // same as runtime_typehash, but always returns a uint64 // see: maphash.rthash function for details func runtime_typehash64(t uintptr, p unsafe.Pointer, seed uint64) uint64 { if unsafe.Sizeof(uintptr(0)) == 8 { return uint64(runtime_typehash(t, p, uintptr(seed))) } lo := runtime_typehash(t, p, uintptr(seed)) hi := runtime_typehash(t, p, uintptr(seed>>32)) return uint64(hi)<<32 | uint64(lo) } //go:noescape //go:linkname runtime_typehash runtime.typehash func runtime_typehash(t uintptr, p unsafe.Pointer, h uintptr) uintptr xsync-3.5.0/util_hash_test.go000066400000000000000000000113631474520662300162460ustar00rootroot00000000000000package xsync_test //lint:file-ignore U1000 unused fields are necessary to access the hasher //lint:file-ignore SA4000 hash code comparisons use identical expressions import ( "fmt" "hash/maphash" "testing" "unsafe" . "github.com/puzpuzpuz/xsync/v3" ) func TestMakeHashFunc(t *testing.T) { type User struct { Name string City string } seed := MakeSeed() hashString := DefaultHasher[string]() hashUser := DefaultHasher[User]() hashUserMap := makeMapHasher[User]() // Not that much to test TBH. // check that hash is not always the same for i := 0; ; i++ { if hashString("foo", seed) != hashString("bar", seed) { break } if i >= 100 { t.Error("hashString is always the same") break } seed = MakeSeed() // try with a new seed } if hashString("foo", seed) != hashString("foo", seed) { t.Error("hashString is not deterministic") } if hashUser(User{Name: "Ivan", City: "Sofia"}, seed) != hashUser(User{Name: "Ivan", City: "Sofia"}, seed) { t.Error("hashUser is not deterministic") } // just for fun, compare with native hash function if hashUser(User{Name: "Ivan", City: "Sofia"}, seed) != hashUserMap(User{Name: "Ivan", City: "Sofia"}, seed) { t.Error("hashUser and hashUserNative return different values") } } func BenchmarkMapHashString(b *testing.B) { fn := func(seed maphash.Seed, s string) uint64 { var h maphash.Hash h.SetSeed(seed) h.WriteString(s) return h.Sum64() } seed := maphash.MakeSeed() for i := 0; i < b.N; i++ { _ = fn(seed, benchmarkKeyPrefix) } // about 13ns/op on x86-64 } func BenchmarkHashString(b *testing.B) { seed := MakeSeed() for i := 0; i < b.N; i++ { _ = HashString(benchmarkKeyPrefix, seed) } // about 4ns/op on x86-64 } func makeMapHasher[T comparable]() func(T, uint64) uint64 { hasher := makeMapHasherInternal(make(map[T]struct{})) is64Bit := unsafe.Sizeof(uintptr(0)) == 8 if is64Bit { return func(value T, seed uint64) uint64 { seed64 := *(*uint64)(unsafe.Pointer(&seed)) return uint64(hasher(runtime_noescape(unsafe.Pointer(&value)), uintptr(seed64))) } } else { return func(value T, seed uint64) uint64 { seed64 := *(*uint64)(unsafe.Pointer(&seed)) lo := hasher(runtime_noescape(unsafe.Pointer(&value)), uintptr(seed64)) hi := hasher(runtime_noescape(unsafe.Pointer(&value)), uintptr(seed64>>32)) return uint64(hi)<<32 | uint64(lo) } } } //go:noescape //go:linkname runtime_noescape runtime.noescape func runtime_noescape(p unsafe.Pointer) unsafe.Pointer type nativeHasher func(unsafe.Pointer, uintptr) uintptr func makeMapHasherInternal(mapValue any) nativeHasher { // go/src/runtime/type.go type tflag uint8 type nameOff int32 type typeOff int32 // go/src/runtime/type.go type _type struct { size uintptr ptrdata uintptr hash uint32 tflag tflag align uint8 fieldAlign uint8 kind uint8 equal func(unsafe.Pointer, unsafe.Pointer) bool gcdata *byte str nameOff ptrToThis typeOff } // go/src/runtime/type.go type maptype struct { typ _type key *_type elem *_type bucket *_type // function for hashing keys (ptr to key, seed) -> hash hasher nativeHasher keysize uint8 elemsize uint8 bucketsize uint16 flags uint32 } type mapiface struct { typ *maptype val uintptr } i := (*mapiface)(unsafe.Pointer(&mapValue)) return i.typ.hasher } func BenchmarkMakeHashFunc(b *testing.B) { type Point struct { X, Y, Z int } type User struct { ID int FirstName string LastName string IsActive bool City string } type PadInside struct { A int B byte C int } type PadTrailing struct { A int B byte } doBenchmarkMakeHashFunc(b, int64(116)) doBenchmarkMakeHashFunc(b, int32(116)) doBenchmarkMakeHashFunc(b, 3.14) doBenchmarkMakeHashFunc(b, "test key test key test key test key test key test key test key test key test key ") doBenchmarkMakeHashFunc(b, Point{1, 2, 3}) doBenchmarkMakeHashFunc(b, User{ID: 1, FirstName: "Ivan", LastName: "Ivanov", IsActive: true, City: "Sofia"}) doBenchmarkMakeHashFunc(b, PadInside{}) doBenchmarkMakeHashFunc(b, PadTrailing{}) doBenchmarkMakeHashFunc(b, [1024]byte{}) doBenchmarkMakeHashFunc(b, [128]Point{}) doBenchmarkMakeHashFunc(b, [128]User{}) doBenchmarkMakeHashFunc(b, [128]PadInside{}) doBenchmarkMakeHashFunc(b, [128]PadTrailing{}) } func doBenchmarkMakeHashFunc[T comparable](b *testing.B, val T) { hash := DefaultHasher[T]() hashNativeMap := makeMapHasher[T]() seed := MakeSeed() b.Run(fmt.Sprintf("%T normal", val), func(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { _ = hash(val, seed) } }) b.Run(fmt.Sprintf("%T map native", val), func(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { _ = hashNativeMap(val, seed) } }) } xsync-3.5.0/util_test.go000066400000000000000000000113251474520662300152410ustar00rootroot00000000000000package xsync_test import ( "math/rand" "strconv" "testing" . "github.com/puzpuzpuz/xsync/v3" ) func TestNextPowOf2(t *testing.T) { if NextPowOf2(0) != 1 { t.Error("nextPowOf2 failed") } if NextPowOf2(1) != 1 { t.Error("nextPowOf2 failed") } if NextPowOf2(2) != 2 { t.Error("nextPowOf2 failed") } if NextPowOf2(3) != 4 { t.Error("nextPowOf2 failed") } } // This test is here to catch potential problems // with fastrand-related changes. func TestFastrand(t *testing.T) { count := 100 set := make(map[uint32]struct{}, count) for i := 0; i < count; i++ { num := Fastrand() set[num] = struct{}{} } if len(set) != count { t.Error("duplicated rand num") } } func TestBroadcast(t *testing.T) { testCases := []struct { input uint8 expected uint64 }{ { input: 0, expected: 0, }, { input: 1, expected: 0x0101010101010101, }, { input: 2, expected: 0x0202020202020202, }, { input: 42, expected: 0x2a2a2a2a2a2a2a2a, }, { input: 127, expected: 0x7f7f7f7f7f7f7f7f, }, { input: 255, expected: 0xffffffffffffffff, }, } for _, tc := range testCases { t.Run(strconv.Itoa(int(tc.input)), func(t *testing.T) { if Broadcast(tc.input) != tc.expected { t.Errorf("unexpected result: %x", Broadcast(tc.input)) } }) } } func TestFirstMarkedByteIndex(t *testing.T) { testCases := []struct { input uint64 expected int }{ { input: 0, expected: 8, }, { input: 0x8080808080808080, expected: 0, }, { input: 0x0000000000000080, expected: 0, }, { input: 0x0000000000008000, expected: 1, }, { input: 0x0000000000800000, expected: 2, }, { input: 0x0000000080000000, expected: 3, }, { input: 0x0000008000000000, expected: 4, }, { input: 0x0000800000000000, expected: 5, }, { input: 0x0080000000000000, expected: 6, }, { input: 0x8000000000000000, expected: 7, }, } for _, tc := range testCases { t.Run(strconv.Itoa(int(tc.input)), func(t *testing.T) { if FirstMarkedByteIndex(tc.input) != tc.expected { t.Errorf("unexpected result: %x", FirstMarkedByteIndex(tc.input)) } }) } } func TestMarkZeroBytes(t *testing.T) { testCases := []struct { input uint64 expected uint64 }{ { input: 0xffffffffffffffff, expected: 0, }, { input: 0, expected: 0x8080808080808080, }, { input: 1, expected: 0x8080808080808000, }, { input: 1 << 9, expected: 0x8080808080800080, }, { input: 1 << 17, expected: 0x8080808080008080, }, { input: 1 << 25, expected: 0x8080808000808080, }, { input: 1 << 33, expected: 0x8080800080808080, }, { input: 1 << 41, expected: 0x8080008080808080, }, { input: 1 << 49, expected: 0x8000808080808080, }, { input: 1 << 57, expected: 0x0080808080808080, }, // false positive { input: 0x0100, expected: 0x8080808080808080, }, } for _, tc := range testCases { t.Run(strconv.Itoa(int(tc.input)), func(t *testing.T) { if MarkZeroBytes(tc.input) != tc.expected { t.Errorf("unexpected result: %x", MarkZeroBytes(tc.input)) } }) } } func TestSetByte(t *testing.T) { testCases := []struct { word uint64 b uint8 idx int expected uint64 }{ { word: 0xffffffffffffffff, b: 0, idx: 0, expected: 0xffffffffffffff00, }, { word: 0xffffffffffffffff, b: 1, idx: 1, expected: 0xffffffffffff01ff, }, { word: 0xffffffffffffffff, b: 2, idx: 2, expected: 0xffffffffff02ffff, }, { word: 0xffffffffffffffff, b: 3, idx: 3, expected: 0xffffffff03ffffff, }, { word: 0xffffffffffffffff, b: 4, idx: 4, expected: 0xffffff04ffffffff, }, { word: 0xffffffffffffffff, b: 5, idx: 5, expected: 0xffff05ffffffffff, }, { word: 0xffffffffffffffff, b: 6, idx: 6, expected: 0xff06ffffffffffff, }, { word: 0xffffffffffffffff, b: 7, idx: 7, expected: 0x07ffffffffffffff, }, { word: 0, b: 0xff, idx: 7, expected: 0xff00000000000000, }, } for _, tc := range testCases { t.Run(strconv.Itoa(int(tc.word)), func(t *testing.T) { if SetByte(tc.word, tc.b, tc.idx) != tc.expected { t.Errorf("unexpected result: %x", SetByte(tc.word, tc.b, tc.idx)) } }) } } func BenchmarkFastrand(b *testing.B) { for i := 0; i < b.N; i++ { _ = Fastrand() } // about 1.4 ns/op on x86-64 } func BenchmarkRand(b *testing.B) { for i := 0; i < b.N; i++ { _ = rand.Uint32() } // about 12 ns/op on x86-64 }