Skip to content

updated slice.ptr_rotate() to include a small rotation with buffer - #7101

Open
TheRadischen wants to merge 5 commits into
odin-lang:masterfrom
TheRadischen:patch-5
Open

updated slice.ptr_rotate() to include a small rotation with buffer#7101
TheRadischen wants to merge 5 commits into
odin-lang:masterfrom
TheRadischen:patch-5

Conversation

@TheRadischen

@TheRadischen TheRadischen commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Change

Added a stack rotate for small rotations,
Buffer is set to [256]byte by default, but bigger would be better
Gries-Mill automatically exits when the rest fits in 256 bytes

Motivation

  1. Small Slices are much faster to rotate with a buffer
  2. Gries-Mills has a bad performance when one side is much smaller than the other side and so copying a small enough range outside the slice and shifting everything over by that amount mitigates the worst case for Gries-Mills.
    test: https://github.com/TheRadischen/odin-utils/tree/main/rotate

Benchmarks

average, type: int, many different rotation length
size: 10 iter: 10000 slice.rotate: 3┬Ás small_rotate: 1.1┬Ás diff: 2.727272727272727
size: 100 iter: 1000 slice.rotate: 12.2┬Ás small_rotate: 5.5┬Ás diff: 2.2181818181818183
size: 1000 iter: 100 slice.rotate: 52┬Ás small_rotate: 48.9┬Ás diff: 1.0633946830265848
size: 10000 iter: 10 slice.rotate: 489.4┬Ás small_rotate: 491.6┬Ás diff: 0.9955248169243287
size: 100000 iter: 5 slice.rotate: 4.9433ms small_rotate: 4.964ms diff: 0.9958299758259468
size: 1000000 iter: 5 slice.rotate: 105.5177ms small_rotate: 106.5284ms diff: 0.9905123891844804

worse case: type: int, small left rotation (0 to 32)
size: 10 iter: 100000 slice.rotate: 900ns small_rotate: 300ns diff: 3
size: 100 iter: 10000 slice.rotate: 3.7┬Ás small_rotate: 700ns diff: 5.285714285714286
size: 1000 iter: 1000 slice.rotate: 31.7┬Ás small_rotate: 2.4┬Ás diff: 13.208333333333334
size: 10000 iter: 100 slice.rotate: 309.3┬Ás small_rotate: 30.4┬Ás diff: 10.174342105263158
size: 100000 iter: 10 slice.rotate: 3.2229ms small_rotate: 362.1┬Ás diff: 8.900579950289975
size: 1000000 iter: 5 slice.rotate: 43.7188ms small_rotate: 22.3035ms diff: 1.9601766538884031

worste case: type: u8, small left rotation (0 to 100)
size: 10 iter: 10000 slice.rotate: 3.3┬Ás small_rotate: 1┬Ás diff: 3.3
size: 100 iter: 1000 slice.rotate: 12.5┬Ás small_rotate: 1.4┬Ás diff: 8.928571428571429
size: 1000 iter: 100 slice.rotate: 50.3┬Ás small_rotate: 2┬Ás diff: 25.15
size: 10000 iter: 10 slice.rotate: 409.2┬Ás small_rotate: 8.8┬Ás diff: 46.5
size: 100000 iter: 5 slice.rotate: 3.9645ms small_rotate: 123.6┬Ás diff: 32.0752427184466
size: 1000000 iter: 5 slice.rotate: 40.0785ms small_rotate: 1.6411ms diff: 24.42172932788983

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.

1 participant