Performance bottleneck due to cache contention#1254
Draft
jacob-greenfield wants to merge 2 commits into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I have been investigating the performance of concurrent data structures as part of an ongoing research project. A key finding is that under heavy parallel workloads, data structures like
crossbeam-skiplistoften suffer from bottlenecks caused by a small number of frequently-modified shared variables due to cache contention (aka cache thrashing).With skip lists in general, any two operations on different keys in a large set are highly unlikely to modify any shared cache line. However, with crossbeam-skiplist,
HotDatais modified on every insertion and removal.hot_data.lenis updated using an atomic fetch-and-add, which is particularly expensive for highly-conteded variables on some architectures. Simply switching to a thread-local random seed and removing the atomic length variable dramatically improves performance under heavy parallel workloads.This PR demonstrates the performance improvements with a simple benchmark. The benefit scales with thread count; on an x86-64 CPU with 64 virtual cores (32 physical cores, hyperthreading enabled), this optimization increases total throughput by >3.6x at 64 threads (with throughput measured as # of set operations per second).
Implementing this optimization would unfortunately mean sacrificing the accuracy of the
len()method; while I suspect the performance boost would easily justify the loss for most users, this would be a breaking change and removes potentially important functionality. One option could be adding new, alternative versions of certain methods; e.g. create newfast_insert()andfast_remove()methods which don't modify the length variable, and leave the existinginsert()/remove()methods intact. I am not sure what the best approach is, but I believe it is ultimately worth implementing, and I am open to suggestions for alternative options.Benchmarks
Tested with rustc 1.97.0-nightly (ff9a9ea07 2026-05-13).
Baseline revision: d0cb16a
Less-contended revision: 4fefec9
Dual-socket Intel Xeon Gold 5218 (x86-64, 64 virtual cores)
lscpuoutputArchitecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 46 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 64 On-line CPU(s) list: 0-63 Vendor ID: GenuineIntel Model name: Intel(R) Xeon(R) Gold 5218 CPU @ 2.30GHz CPU family: 6 Model: 85 Thread(s) per core: 2 Core(s) per socket: 16 Socket(s): 2 Stepping: 7 CPU max MHz: 3900.0000 CPU min MHz: 1000.0000 BogoMIPS: 4600.00 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch c puid_fault epb cat_l3 cdp_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbas e tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl x saveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke avx512_vnni md_clear flush_l1d a rch_capabilities Virtualization features: Virtualization: VT-x Caches (sum of all): L1d: 1 MiB (32 instances) L1i: 1 MiB (32 instances) L2: 32 MiB (32 instances) L3: 44 MiB (2 instances) NUMA: NUMA node(s): 2 NUMA node0 CPU(s): 0-15,32-47 NUMA node1 CPU(s): 16-31,48-63 Vulnerabilities: Gather data sampling: Mitigation; Microcode Itlb multihit: KVM: Mitigation: VMX disabled L1tf: Not affected Mds: Not affected Meltdown: Not affected Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable Reg file data sampling: Not affected Retbleed: Mitigation; Enhanced IBRS Spec rstack overflow: Not affected Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI SW loop, KVM SW loop Srbds: Not affected Tsx async abort: Mitigation; TSX disabledcargo run -p crossbeam-skiplist --example parallel --release -- 1 2 4 8 16 32 48 60 64Throughput (operations/second):
142529914073940.99x261648226774221.02x474724751216961.08x587369376714611.31x6853475127051381.85x7274837227270323.12x8956789302103723.37x10018060364390803.64x10527560382918803.64xMacBook Pro (aarch64, M1 Pro, 10 cores)
cargo run -p crossbeam-skiplist --example parallel --release -- 1 2 4 8 10Throughput (operations/second):
185118918167410.98x358304737296541.06x633342572126821.28x8624892131323321.48x9427303142391391.55x