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

TRMC implementation of @ #11859

Merged
merged 5 commits into from Jan 5, 2023
Merged

TRMC implementation of @ #11859

merged 5 commits into from Jan 5, 2023

Conversation

yallop
Copy link
Member

@yallop yallop commented Jan 3, 2023

A tail-recursive-modulo-cons implementation of Stdlib.(@) (and List.append), replacing the existing stack-consuming implementation.

To avoid a slowdown over the existing version, the function is also unrolled. (It's worth noting, though, that this isn't the fastest possible implementation: an unrolled stack-consuming version would be even faster.)

Benchmark results:

Almost always faster than the current implementation; always faster on lists of length less than 16384; twice as fast on long lists
l1 length original trmc trmc/unrolled
1 6.46ns 9.74ns 5.47ns
2 9.25ns 14.58ns 7.56ns
4 16.58ns 24.49ns 13.76ns
8 28.55ns 43.67ns 22.30ns
16 61.44ns 84.18ns 55.11ns
32 131.71ns 151.22ns 102.34ns
64 250.58ns 286.24ns 197.16ns
128 511.47ns 596.78ns 382.34ns
256 1_046.92ns 1_222.15ns 804.63ns
512 2.93us 2.75us 1.79us
1024 6.36us 5.84us 3.96us
2048 14.37us 12.96us 9.38us
4096 33.32us 31.76us 23.32us
8192 79.70us 82.88us 67.22us
16384 240.20us 246.96us 226.37us
32768 776.65us 870.93us 796.78us
65536 2.84ms 3.02ms 2.76ms
131072 9.16ms 8.45ms 7.79ms
262144 23.33ms 18.02ms 16.17ms
524288 59.84ms 36.45ms 34.23ms
1048576 155.05ms 80.61ms 75.15ms

stdlib/listLabels.mli Outdated Show resolved Hide resolved
Copy link
Contributor

@dbuenzli dbuenzli left a comment

Choose a reason for hiding this comment

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

Looks good to me. If you are in the mood I left a few suggestions to improve the docs.

stdlib/stdlib.mli Outdated Show resolved Hide resolved
@gasche
Copy link
Member

gasche commented Jan 3, 2023

always faster on lists of length less than 16384

A minor point: if you observe a slowdown with TMC functions on semi-large lists, it is probably caused by the benchmarking setup. In the regime where the list length is close to the minor heap size, you can observe artifical promotion effects. See the "Promotion" section of #9760 (comment). In short: if you are "ignoring" the result of the function call in your benchmark, you should store it into a long-lived reference instead for more realistic promotion behavior (in general, the result of operations on very large inputs do stay alive until after the next minor collection).

@yallop
Copy link
Member Author

yallop commented Jan 3, 2023

@gasche:

In short: if you are "ignoring" the result of the function call in your benchmark, you should store it into a long-lived reference instead for more realistic promotion behavior

Thanks for the suggestion. I tried this and now the benchmark shows that the new version is always at least a smidgen faster than the existing implementation.

Copy link
Contributor

@nojb nojb left a comment

Choose a reason for hiding this comment

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

LGTM

stdlib/list.mli Show resolved Hide resolved
@nojb
Copy link
Contributor

nojb commented Jan 4, 2023

This looks good to go, but still needs a second official approval. Perhaps @gasche can do the honours?

@nojb
Copy link
Contributor

nojb commented Jan 5, 2023

@yallop: do you mind if I squash-merge your PR? I normally squash-merge small PRs as it is easier to cherry-pick them, revert them, etc, but since you took care to have a clean history, perhaps you have a preference for doing a plain merge...

@yallop
Copy link
Member Author

yallop commented Jan 5, 2023

Thanks for asking, @nojb! I'm happy to have it squash-merged.

@nojb nojb merged commit 4225e86 into ocaml:trunk Jan 5, 2023
@nojb
Copy link
Contributor

nojb commented Jan 5, 2023

Thanks for asking, @nojb! I'm happy to have it squash-merged.

Thanks, merged!

@yallop yallop deleted the append-trmc branch January 5, 2023 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants