123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223(*
Copyright (c) 2020, Anton Bachin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*)(* https://discuss.ocaml.org/t/a-new-list-map-that-is-both-stack-safe-and-fast/865 *)(* Non-tail-recursive prefix for the new fast map implementation. This is not
really necessary, as it is possible to use the tail-recursive suffix
function, and get almost the same performance. However, there *is* a slight
improvement, and this makes it fully competitive with Containers and
Base :) *)letrecplain_unrolled_prefix_5map_suffixcountfl=matchlwith|[]->[]|[x1]->lety1=fx1in[y1]|[x1;x2]->lety2=fx2inlety1=fx1in[y1;y2]|[x1;x2;x3]->lety3=fx3inlety2=fx2inlety1=fx1in[y1;y2;y3]|[x1;x2;x3;x4]->lety4=fx4inlety3=fx3inlety2=fx2inlety1=fx1in[y1;y2;y3;y4]|x1::x2::x3::x4::x5::tail->lettail=ifcount<=0thenmap_suffixftailelseplain_unrolled_prefix_5map_suffix(count-1)ftailinlety5=fx5inlety4=fx4inlety3=fx3inlety2=fx2inlety1=fx1iny1::y2::y3::y4::y5::tail(* Fast tail-recursive map for the list suffix. *)letchunked_tail_recursive_map_12fl=letrecsplitchunksl=matchlwith|_::_::_::_::_::_::_::_::_::_::_::_::tail->(split[@ocaml.tailcall])(l::chunks)tail|_->l::chunksinletmap_head_chunkchunk=match[@ocaml.warning"-8"]chunkwith|[]->[]|[x1]->lety1=fx1in[y1]|[x1;x2]->lety2=fx2inlety1=fx1in[y1;y2]|[x1;x2;x3]->lety3=fx3inlety2=fx2inlety1=fx1in[y1;y2;y3]|[x1;x2;x3;x4]->lety4=fx4inlety3=fx3inlety2=fx2inlety1=fx1in[y1;y2;y3;y4]|[x1;x2;x3;x4;x5]->lety5=fx5inlety4=fx4inlety3=fx3inlety2=fx2inlety1=fx1in[y1;y2;y3;y4;y5]|[x1;x2;x3;x4;x5;x6]->lety6=fx6inlety5=fx5inlety4=fx4inlety3=fx3inlety2=fx2inlety1=fx1in[y1;y2;y3;y4;y5;y6]|[x1;x2;x3;x4;x5;x6;x7]->lety7=fx7inlety6=fx6inlety5=fx5inlety4=fx4inlety3=fx3inlety2=fx2inlety1=fx1in[y1;y2;y3;y4;y5;y6;y7]|[x1;x2;x3;x4;x5;x6;x7;x8]->lety8=fx8inlety7=fx7inlety6=fx6inlety5=fx5inlety4=fx4inlety3=fx3inlety2=fx2inlety1=fx1in[y1;y2;y3;y4;y5;y6;y7;y8]|[x1;x2;x3;x4;x5;x6;x7;x8;x9]->lety9=fx9inlety8=fx8inlety7=fx7inlety6=fx6inlety5=fx5inlety4=fx4inlety3=fx3inlety2=fx2inlety1=fx1in[y1;y2;y3;y4;y5;y6;y7;y8;y9]|[x1;x2;x3;x4;x5;x6;x7;x8;x9;x10]->lety10=fx10inlety9=fx9inlety8=fx8inlety7=fx7inlety6=fx6inlety5=fx5inlety4=fx4inlety3=fx3inlety2=fx2inlety1=fx1in[y1;y2;y3;y4;y5;y6;y7;y8;y9;y10]|[x1;x2;x3;x4;x5;x6;x7;x8;x9;x10;x11]->lety11=fx11inlety10=fx10inlety9=fx9inlety8=fx8inlety7=fx7inlety6=fx6inlety5=fx5inlety4=fx4inlety3=fx3inlety2=fx2inlety1=fx1in[y1;y2;y3;y4;y5;y6;y7;y8;y9;y10;y11]|[x1;x2;x3;x4;x5;x6;x7;x8;x9;x10;x11;x12]->lety12=fx12inlety11=fx11inlety10=fx10inlety9=fx9inlety8=fx8inlety7=fx7inlety6=fx6inlety5=fx5inlety4=fx4inlety3=fx3inlety2=fx2inlety1=fx1in[y1;y2;y3;y4;y5;y6;y7;y8;y9;y10;y11;y12]inletmap_tail_chunksuffixchunk=match[@ocaml.warning"-8"]chunkwith|x1::x2::x3::x4::x5::x6::x7::x8::x9::x10::x11::x12::_->lety12=fx12inlety11=fx11inlety10=fx10inlety9=fx9inlety8=fx8inlety7=fx7inlety6=fx6inlety5=fx5inlety4=fx4inlety3=fx3inlety2=fx2inlety1=fx1iny1::y2::y3::y4::y5::y6::y7::y8::y9::y10::y11::y12::suffixinletrecmap_all_tail_chunkssuffixchunks=matchchunkswith|[]->suffix|chunk::more->(map_all_tail_chunks[@ocaml.tailcall])(map_tail_chunksuffixchunk)moreinletchunks=split[]linmatchchunkswith|[]->[]|first::rest->map_all_tail_chunks(map_head_chunkfirst)rest(* Combines the 5x unrolled non-tail-recursive map for a prefix of 5000
elements, followed by the tail-recursive new fast map for the remainder. *)letfaster_mapfl=plain_unrolled_prefix_5chunked_tail_recursive_map_121000fl(* wrapper that is actually exported *)letmap_purefl=faster_mapfl