Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.ArrayList: initial capacity based on cache line size #22865

Merged
merged 1 commit into from
Feb 13, 2025

Conversation

andrewrk
Copy link
Member

also std.MultiArrayList

build zig with itself (ReleaseFast, no libc)

Benchmark 1 (3 runs): master/bin/zig build-exe ...
  measurement          mean ± σ            min … max           outliers         delta
  wall_time          12.7s  ±  106ms    12.6s  … 12.8s           0 ( 0%)        0%
  peak_rss            983MB ± 25.8MB     956MB … 1.01GB          0 ( 0%)        0%
  cpu_cycles         93.7G  ± 63.0M     93.6G  … 93.7G           0 ( 0%)        0%
  instructions        197G  ± 2.72M      197G  …  197G           0 ( 0%)        0%
  cache_references   5.69G  ± 12.6M     5.67G  … 5.70G           0 ( 0%)        0%
  cache_misses        413M  ± 2.31M      411M  …  415M           0 ( 0%)        0%
  branch_misses       491M  ±  306K      491M  …  492M           0 ( 0%)        0%
Benchmark 2 (3 runs): branch/bin/zig build-exe ...
  measurement          mean ± σ            min … max           outliers         delta
  wall_time          12.2s  ± 34.3ms    12.1s  … 12.2s           0 ( 0%)        ⚡-  4.1% ±  1.4%
  peak_rss            940MB ± 6.92MB     935MB …  948MB          0 ( 0%)          -  4.4% ±  4.4%
  cpu_cycles         90.3G  ± 67.1M     90.3G  … 90.4G           0 ( 0%)        ⚡-  3.6% ±  0.2%
  instructions        196G  ± 6.30M      196G  …  196G           0 ( 0%)          -  0.2% ±  0.0%
  cache_references   5.57G  ± 18.0M     5.55G  … 5.59G           0 ( 0%)        ⚡-  2.2% ±  0.6%
  cache_misses        405M  ± 3.40M      401M  …  408M           0 ( 0%)          -  2.1% ±  1.6%
  branch_misses       412M  ±  512K      411M  …  412M           0 ( 0%)        ⚡- 16.1% ±  0.2%

similar to #22861

fn growCapacity(current: usize, minimum: usize) usize {
var new = current;
while (true) {
new +|= new / 2 + init_capacity;
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you think about also preferring capacities which are a multiple of the cache line size?

Suggested change
new +|= new / 2 + init_capacity;
new = std.mem.alignForward(usize, new +| new / 2 + init_capacity, init_capacity)

Copy link
Member Author

@andrewrk andrewrk Feb 12, 2025

Choose a reason for hiding this comment

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

worth a try. that expression needs work. probably align backward would be fewer instructions and avoid potential overflow

@andrewrk andrewrk enabled auto-merge (rebase) February 12, 2025 21:14
@andrewrk andrewrk merged commit d12123a into master Feb 13, 2025
9 checks passed
@andrewrk andrewrk deleted the list-init-capacity branch February 13, 2025 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants