package core_kernel
Industrial strength alternative to OCaml's standard library
Install
Dune Dependency
Authors
Maintainers
Sources
core_kernel-v0.15.0.tar.gz
sha256=34a0288f16027c6b90e4ad16cb5cc677d7063d310faf918748ce70f1745116c0
doc/src/core_kernel.tuple_pool/tuple_pool.ml.html
Source file tuple_pool.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
open! Core open! Import open Tuple_pool_intf module Tuple_type = Tuple_type let failwiths = Error.failwiths let phys_equal = Caml.( == ) let arch_sixtyfour = Sys.word_size_in_bits = 64 module Int = struct let num_bits = Int.num_bits let max_value = Caml.max_int let to_string = string_of_int end let sprintf = Printf.sprintf let concat l = Base.String.concat ~sep:"" l module type S = S module Pool = struct let grow_capacity ~capacity ~old_capacity = match capacity with | None -> if old_capacity = 0 then 1 else old_capacity * 2 | Some capacity -> if capacity <= old_capacity then failwiths ~here:[%here] "Pool.grow got too small capacity" (`capacity capacity, `old_capacity old_capacity) [%sexp_of: [ `capacity of int ] * [ `old_capacity of int ]]; capacity ;; module Slots = Tuple_type.Slots let max_slot = 14 (* The pool is represented as a single [Uniform_array.t], where index zero has the metadata about the pool and the remaining indices are the tuples layed out one after the other. Each tuple takes [1 + slots_per_tuple] indices in the pool, where the first index holds a header and the remaining indices hold the tuple's slots: {v | header | s0 | s1 | ... | s<N-1> | v} A [Pointer.t] to a tuple contains the integer index where its header is, as well as (a mask of) the tuple's unique id. The free tuples are singly linked via the headers. When a tuple is in use, its header is marked to indicate so, and also to include the tuple's unique id. This allows us to check in constant time whether a pointer is valid, by comparing the id in the pointer with the id in the header. When a tuple is not in use, its header is part of the free list, and its tuple slots have dummy values of the appropriate types, from the [dummy] tuple supplied to [create]. We must have dummy values of the correct type to prevent a segfault in code that (mistakenly) uses a pointer to a free tuple. For [Pool.Unsafe], a slot in a free object is guaranteed to be an int; it must not be pointer to prevent a space leak. However, the int in the slot may not represent a valid value of the type. *) module Slot = struct type ('slots, 'a) t = int [@@deriving sexp_of] let equal (t1 : (_, _) t) t2 = t1 = t2 let t0 = 1 let t1 = 2 let t2 = 3 let t3 = 4 let t4 = 5 let t5 = 6 let t6 = 7 let t7 = 8 let t8 = 9 let t9 = 10 let t10 = 11 let t11 = 12 let t12 = 13 let t13 = 14 let%test _ = t13 = max_slot end (* We only have [Int.num_bits] bits available for pool pointers. The bits of a pool pointer encode two things: - the tuple's array index in the pool - the tuple's identifier (not necessarily unique) We choose [array_index_num_bits] as large as needed for the maximum pool capacity that we want to support, and use the remaining [masked_tuple_id_num_bits] bits for the identifier. 64-bit and 32-bit architectures typically have very different address-space sizes, so we choose [array_index_num_bits] differently. *) let array_index_num_bits = if arch_sixtyfour then ( assert (Int.num_bits = 63); 30) else ( assert (Int.num_bits = 31 || Int.num_bits = 32); 22) ;; let masked_tuple_id_num_bits = Int.num_bits - array_index_num_bits let%test _ = array_index_num_bits > 0 let%test _ = masked_tuple_id_num_bits > 0 let%test _ = array_index_num_bits + masked_tuple_id_num_bits <= Int.num_bits let max_array_length = 1 lsl array_index_num_bits module Tuple_id : sig type t = private int [@@deriving sexp_of] include Invariant.S with type t := t val to_string : t -> string val equal : t -> t -> bool val init : t val next : t -> t val of_int : int -> t val to_int : t -> int val examples : t list end = struct type t = int [@@deriving sexp_of] (* We guarantee that tuple ids are nonnegative so that they can be encoded in headers. *) let invariant t = assert (t >= 0) let to_string = Int.to_string let equal (t1 : t) t2 = t1 = t2 let init = 0 let next t = if arch_sixtyfour then t + 1 else if t = Int.max_value then 0 else t + 1 let to_int t = t let of_int i = if i < 0 then failwiths ~here:[%here] "Tuple_id.of_int got negative int" i [%sexp_of: int]; i ;; let examples = [ 0; 1; 0x1FFF_FFFF; Int.max_value ] end let tuple_id_mask = (1 lsl masked_tuple_id_num_bits) - 1 module Pointer : sig (* [Pointer.t] is an encoding as an [int] of the following sum type: {[ | Null | Normal of { header_index : int; masked_tuple_id : int } ]} The encoding is chosen to optimize the most common operation, namely tuple-slot access, the [slot_index] function. The encoding is designed so that [slot_index] produces a negative number for [Null], which will cause the subsequent array bounds check to fail. *) type 'slots t = private int [@@deriving sexp_of, typerep] include Invariant.S1 with type 'a t := 'a t val phys_compare : 'a t -> 'a t -> int val phys_equal : 'a t -> 'a t -> bool (* The null pointer. [null] is a function due to issues with the value restriction. *) val null : unit -> _ t val is_null : _ t -> bool (* Normal pointers. *) val create : header_index:int -> Tuple_id.t -> _ t val header_index : _ t -> int val masked_tuple_id : _ t -> int val slot_index : _ t -> (_, _) Slot.t -> int val first_slot_index : _ t -> int module Id : sig type t [@@deriving bin_io, sexp] val to_int63 : t -> Int63.t val of_int63 : Int63.t -> t end val to_id : _ t -> Id.t val of_id_exn : Id.t -> _ t end = struct (* A pointer is either [null] or the (positive) index in the pool of the next-free field preceeding the tuple's slots. *) type 'slots t = int [@@deriving typerep] let sexp_of_t _ t = Sexp.Atom (sprintf "<Pool.Pointer.t: 0x%08x>" t) let phys_equal (t1 : _ t) t2 = phys_equal t1 t2 let phys_compare = compare let null () = -max_slot - 1 let is_null t = phys_equal t (null ()) (* [null] must be such that [null + slot] is an invalid array index for all slots. Otherwise get/set on the null pointer may lead to a segfault. *) let%test _ = null () + max_slot < 0 let create ~header_index (tuple_id : Tuple_id.t) = header_index lor ((Tuple_id.to_int tuple_id land tuple_id_mask) lsl array_index_num_bits) ;; let header_index_mask = (1 lsl array_index_num_bits) - 1 let masked_tuple_id t = t lsr array_index_num_bits let header_index t = t land header_index_mask let invariant _ t = if not (is_null t) then assert (header_index t > 0) let%test_unit _ = invariant ignore (null ()) let%test_unit _ = List.iter Tuple_id.examples ~f:(fun tuple_id -> invariant ignore (create ~header_index:1 tuple_id)) ;; let slot_index t slot = header_index t + slot let first_slot_index t = slot_index t Slot.t0 module Id = struct include Int63 let to_int63 t = t let of_int63 i = i end let to_id t = Id.of_int t let of_id_exn id = try let t = Id.to_int_exn id in if is_null t then t else ( let should_equal = create ~header_index:(header_index t) (Tuple_id.of_int (masked_tuple_id t)) in if phys_equal t should_equal then t else failwiths ~here:[%here] "should equal" should_equal [%sexp_of: _ t]) with | exn -> failwiths ~here:[%here] "Pointer.of_id_exn got strange id" (id, exn) [%sexp_of: Id.t * exn] ;; end module Header : sig (* A [Header.t] is an encoding as an [int] of the following type: {[ | Null | Free of { next_free_header_index : int } | Used of { tuple_id : int } ]} If a tuple is free, its header is set to either [Null] or [Free] with [next_free_header_index] indicating the header of the next tuple on the free list. If a tuple is in use, it header is set to [Used]. *) type t = private int [@@deriving sexp_of] val null : t val is_null : t -> bool val free : next_free_header_index:int -> t val is_free : t -> bool val next_free_header_index : t -> int (* only valid if [is_free t] *) val used : Tuple_id.t -> t val is_used : t -> bool val tuple_id : t -> Tuple_id.t (* only valid if [is_used t] *) end = struct type t = int let null = 0 let is_null t = t = 0 (* We know that header indices are [> 0], because index [0] holds the metadata. *) let free ~next_free_header_index = next_free_header_index let is_free t = t > 0 let next_free_header_index t = t let used (tuple_id : Tuple_id.t) = -1 - (tuple_id :> int) let is_used t = t < 0 let tuple_id t = Tuple_id.of_int (-(t + 1)) let%test_unit _ = List.iter Tuple_id.examples ~f:(fun id -> let t = used id in assert (is_used t); assert (Tuple_id.equal (tuple_id t) id)) ;; let sexp_of_t t = if is_null t then Sexp.Atom "null" else if is_free t then Sexp.(List [ Atom "Free"; Atom (Int.to_string (next_free_header_index t)) ]) else Sexp.(List [ Atom "Used"; Atom (Tuple_id.to_string (tuple_id t)) ]) ;; end let metadata_index = 0 let start_of_tuples_index = 1 let max_capacity ~slots_per_tuple = (max_array_length - start_of_tuples_index) / (1 + slots_per_tuple) ;; let%test_unit _ = for slots_per_tuple = 1 to max_slot do assert ( start_of_tuples_index + ((1 + slots_per_tuple) * max_capacity ~slots_per_tuple) <= max_array_length) done ;; module Metadata = struct type 'slots t = { (* [slots_per_tuple] is number of slots in a tuple as seen by the user; i.e. not counting the next-free pointer. *) slots_per_tuple : int ; capacity : int ; mutable length : int ; mutable next_id : Tuple_id.t ; mutable first_free : Header.t (* [dummy] is [None] in an unsafe pool. In a safe pool, [dummy] is [Some a], with [Uniform_array.length a = slots_per_tuple]. [dummy] is actually a tuple value with the correct type (corresponding to ['slots]), but we make the type of [dummy] be [Obj.t Uniform_array.t] because we can't write that type here. Also, the purpose of [dummy] is to initialize a pool element, making [dummy] an [Obj.t Uniform_array.t] lets us initialize a pool element using [Uniform_array.blit] from [dummy] to the pool, which is an [Obj.t Uniform_array.t]. *) ; dummy : (Obj.t Uniform_array.t[@sexp.opaque]) option } [@@deriving fields, sexp_of] let array_indices_per_tuple t = 1 + t.slots_per_tuple let array_length t = start_of_tuples_index + (t.capacity * array_indices_per_tuple t) let header_index_to_tuple_num t ~header_index = (header_index - start_of_tuples_index) / array_indices_per_tuple t ;; let tuple_num_to_header_index t tuple_num = start_of_tuples_index + (tuple_num * array_indices_per_tuple t) ;; let tuple_num_to_first_slot_index t tuple_num = tuple_num_to_header_index t tuple_num + 1 ;; let is_full t = t.length = t.capacity end open Metadata (* We use type [Obj.t] because the array holds a mix of integers as well as OCaml values of arbitrary type. *) type 'slots t = Obj.t Uniform_array.t let metadata (type slots) (t : slots t) = Uniform_array.unsafe_get t metadata_index |> (Obj.obj : _ -> slots Metadata.t) ;; let length t = (metadata t).length let sexp_of_t sexp_of_ty t = Metadata.sexp_of_t sexp_of_ty (metadata t) (* Because [unsafe_header] and [unsafe_set_header] do not do a bounds check, one must be sure that one has a valid [header_index] before calling them. *) let unsafe_header t ~header_index = Uniform_array.unsafe_get t header_index |> (Obj.obj : _ -> Header.t) ;; let unsafe_set_header t ~header_index (header : Header.t) = Uniform_array.unsafe_set_int_assuming_currently_int t header_index (header :> int) ;; let header_index_is_in_bounds t ~header_index = header_index >= start_of_tuples_index && header_index < Uniform_array.length t ;; let unsafe_pointer_is_live t pointer = let header_index = Pointer.header_index pointer in let header = unsafe_header t ~header_index in Header.is_used header && Tuple_id.to_int (Header.tuple_id header) land tuple_id_mask = Pointer.masked_tuple_id pointer ;; let pointer_is_valid t pointer = header_index_is_in_bounds t ~header_index:(Pointer.header_index pointer) (* At this point, we know the pointer isn't [null] and is in bounds, so we know it is the index of a header, since we maintain the invariant that all pointers other than [null] are. *) && unsafe_pointer_is_live t pointer ;; let id_of_pointer _t pointer = Pointer.to_id pointer let is_valid_header_index t ~header_index = let metadata = metadata t in header_index_is_in_bounds t ~header_index && 0 = (header_index - start_of_tuples_index) mod Metadata.array_indices_per_tuple metadata ;; let pointer_of_id_exn t id = try let pointer = Pointer.of_id_exn id in if not (Pointer.is_null pointer) then ( let header_index = Pointer.header_index pointer in if not (is_valid_header_index t ~header_index) then failwiths ~here:[%here] "invalid header index" header_index [%sexp_of: int]; if not (unsafe_pointer_is_live t pointer) then failwith "pointer not live"); pointer with | exn -> failwiths ~here:[%here] "Pool.pointer_of_id_exn got invalid id" (id, t, exn) [%sexp_of: Pointer.Id.t * _ t * exn] ;; let invariant _invariant_a t : unit = try let metadata = metadata t in let check f field = f (Field.get field metadata) in Metadata.Fields.iter ~slots_per_tuple:(check (fun slots_per_tuple -> assert (slots_per_tuple > 0))) ~capacity: (check (fun capacity -> assert (capacity >= 0); assert (Uniform_array.length t = Metadata.array_length metadata))) ~length: (check (fun length -> assert (length >= 0); assert (length <= metadata.capacity))) ~next_id:(check Tuple_id.invariant) ~first_free: (check (fun first_free -> let free = Array.create ~len:metadata.capacity false in let r = ref first_free in while not (Header.is_null !r) do let header = !r in assert (Header.is_free header); let header_index = Header.next_free_header_index header in assert (is_valid_header_index t ~header_index); let tuple_num = header_index_to_tuple_num metadata ~header_index in if free.(tuple_num) then failwiths ~here:[%here] "cycle in free list" tuple_num [%sexp_of: int]; free.(tuple_num) <- true; r := unsafe_header t ~header_index done)) ~dummy: (check (function | Some dummy -> assert (Uniform_array.length dummy = metadata.slots_per_tuple) | None -> for tuple_num = 0 to metadata.capacity - 1 do let header_index = tuple_num_to_header_index metadata tuple_num in let header = unsafe_header t ~header_index in if Header.is_free header then ( let first_slot = tuple_num_to_first_slot_index metadata tuple_num in for slot = 0 to metadata.slots_per_tuple - 1 do assert (Obj.is_int (Uniform_array.get t (first_slot + slot))) done) done)) with | exn -> failwiths ~here:[%here] "Pool.invariant failed" (exn, t) [%sexp_of: exn * _ t] ;; let capacity t = (metadata t).capacity let is_full t = Metadata.is_full (metadata t) let unsafe_add_to_free_list t metadata ~header_index = unsafe_set_header t ~header_index metadata.first_free; metadata.first_free <- Header.free ~next_free_header_index:header_index ;; let set_metadata (type slots) (t : slots t) metadata = Uniform_array.set t metadata_index (Obj.repr (metadata : slots Metadata.t)) ;; let create_array (type slots) (metadata : slots Metadata.t) : slots t = let t = Uniform_array.create_obj_array ~len:(Metadata.array_length metadata) in set_metadata t metadata; t ;; (* Initialize tuples numbered from [lo] (inclusive) up to [hi] (exclusive). For each tuple, this puts dummy values in the tuple's slots and adds the tuple to the free list. *) let unsafe_init_range t metadata ~lo ~hi = (match metadata.dummy with | None -> () | Some dummy -> for tuple_num = lo to hi - 1 do Uniform_array.blit ~src:dummy ~src_pos:0 ~dst:t ~dst_pos:(tuple_num_to_first_slot_index metadata tuple_num) ~len:metadata.slots_per_tuple done); for tuple_num = hi - 1 downto lo do unsafe_add_to_free_list t metadata ~header_index:(tuple_num_to_header_index metadata tuple_num) done ;; let create_with_dummy slots ~capacity ~dummy = if capacity < 0 then failwiths ~here:[%here] "Pool.create got invalid capacity" capacity [%sexp_of: int]; let slots_per_tuple = Slots.slots_per_tuple slots in let max_capacity = max_capacity ~slots_per_tuple in if capacity > max_capacity then failwiths ~here:[%here] "Pool.create got too large capacity" (capacity, `max max_capacity) [%sexp_of: int * [ `max of int ]]; let metadata = { Metadata.slots_per_tuple ; capacity ; length = 0 ; next_id = Tuple_id.init ; first_free = Header.null ; dummy } in let t = create_array metadata in unsafe_init_range t metadata ~lo:0 ~hi:capacity; t ;; let create (type tuple) (slots : (tuple, _) Slots.t) ~capacity ~dummy = let dummy = if Slots.slots_per_tuple slots = 1 then Uniform_array.singleton (Obj.repr (dummy : tuple)) else (Obj.magic (dummy : tuple) : Obj.t Uniform_array.t) in create_with_dummy slots ~capacity ~dummy:(Some dummy) ;; (* Purge a pool and make it unusable. *) let destroy t = let metadata = metadata t in (* We clear out all the pool's entries, which causes all pointers to be invalid. This also prevents the destroyed pool from unnecessarily keeping heap blocks alive. This is similar to [free]ing all the entries with the difference that we make the free list empty as well. *) (match metadata.dummy with | None -> for i = start_of_tuples_index to Uniform_array.length t - 1 do Uniform_array.unsafe_set t i (Obj.repr 0) done | Some dummy -> for tuple_num = 0 to metadata.capacity - 1 do let header_index = tuple_num_to_header_index metadata tuple_num in unsafe_set_header t ~header_index Header.null; Uniform_array.blit ~src:dummy ~src_pos:0 ~dst:t ~dst_pos:(header_index + 1) ~len:metadata.slots_per_tuple done); let metadata = { Metadata.slots_per_tuple = metadata.slots_per_tuple ; capacity = 0 ; length = 0 ; next_id = metadata.next_id ; first_free = Header.null ; dummy = metadata.dummy } in set_metadata t metadata ;; let[@cold] grow ?capacity t = let { Metadata.slots_per_tuple ; capacity = old_capacity ; length ; next_id ; first_free = _ ; dummy } = metadata t in let capacity = min (max_capacity ~slots_per_tuple) (grow_capacity ~capacity ~old_capacity) in if capacity = old_capacity then failwiths ~here:[%here] "Pool.grow cannot grow pool; capacity already at maximum" capacity [%sexp_of: int]; let metadata = { Metadata.slots_per_tuple ; capacity ; length ; next_id ; first_free = Header.null ; dummy } in let t' = create_array metadata in Uniform_array.blit ~src:t ~src_pos:start_of_tuples_index ~dst:t' ~dst_pos:start_of_tuples_index ~len:(old_capacity * Metadata.array_indices_per_tuple metadata); destroy t; unsafe_init_range t' metadata ~lo:old_capacity ~hi:capacity; for tuple_num = old_capacity - 1 downto 0 do let header_index = tuple_num_to_header_index metadata tuple_num in let header = unsafe_header t' ~header_index in if not (Header.is_used header) then unsafe_add_to_free_list t' metadata ~header_index done; t' ;; let[@cold] raise_malloc_full t = failwiths ~here:[%here] "Pool.malloc of full pool" t [%sexp_of: _ t] ;; let malloc (type slots) (t : slots t) : slots Pointer.t = let metadata = metadata t in let first_free = metadata.first_free in if Header.is_null first_free then raise_malloc_full t; let header_index = Header.next_free_header_index first_free in metadata.first_free <- unsafe_header t ~header_index; metadata.length <- metadata.length + 1; let tuple_id = metadata.next_id in unsafe_set_header t ~header_index (Header.used tuple_id); metadata.next_id <- Tuple_id.next tuple_id; Pointer.create ~header_index tuple_id ;; let unsafe_free (type slots) (t : slots t) (pointer : slots Pointer.t) = let metadata = metadata t in metadata.length <- metadata.length - 1; unsafe_add_to_free_list t metadata ~header_index:(Pointer.header_index pointer); match metadata.dummy with | None -> let pos = Pointer.first_slot_index pointer in for i = 0 to metadata.slots_per_tuple - 1 do Uniform_array.unsafe_clear_if_pointer t (pos + i) done | Some dummy -> Uniform_array.unsafe_blit ~src:dummy ~src_pos:0 ~len:metadata.slots_per_tuple ~dst:t ~dst_pos:(Pointer.first_slot_index pointer) ;; let free (type slots) (t : slots t) (pointer : slots Pointer.t) = (* Check [pointer_is_valid] to: - avoid freeing a null pointer - avoid freeing a free pointer (this would lead to a pool inconsistency) - be able to use unsafe functions after. *) if not (pointer_is_valid t pointer) then failwiths ~here:[%here] "Pool.free of invalid pointer" (pointer, t) [%sexp_of: _ Pointer.t * _ t]; unsafe_free t pointer ;; let new1 t a0 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); pointer ;; let new2 t a0 a1 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); pointer ;; let new3 t a0 a1 a2 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); Uniform_array.unsafe_set t (offset + 3) (Obj.repr a2); pointer ;; let new4 t a0 a1 a2 a3 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); Uniform_array.unsafe_set t (offset + 3) (Obj.repr a2); Uniform_array.unsafe_set t (offset + 4) (Obj.repr a3); pointer ;; let new5 t a0 a1 a2 a3 a4 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); Uniform_array.unsafe_set t (offset + 3) (Obj.repr a2); Uniform_array.unsafe_set t (offset + 4) (Obj.repr a3); Uniform_array.unsafe_set t (offset + 5) (Obj.repr a4); pointer ;; let new6 t a0 a1 a2 a3 a4 a5 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); Uniform_array.unsafe_set t (offset + 3) (Obj.repr a2); Uniform_array.unsafe_set t (offset + 4) (Obj.repr a3); Uniform_array.unsafe_set t (offset + 5) (Obj.repr a4); Uniform_array.unsafe_set t (offset + 6) (Obj.repr a5); pointer ;; let new7 t a0 a1 a2 a3 a4 a5 a6 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); Uniform_array.unsafe_set t (offset + 3) (Obj.repr a2); Uniform_array.unsafe_set t (offset + 4) (Obj.repr a3); Uniform_array.unsafe_set t (offset + 5) (Obj.repr a4); Uniform_array.unsafe_set t (offset + 6) (Obj.repr a5); Uniform_array.unsafe_set t (offset + 7) (Obj.repr a6); pointer ;; let new8 t a0 a1 a2 a3 a4 a5 a6 a7 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); Uniform_array.unsafe_set t (offset + 3) (Obj.repr a2); Uniform_array.unsafe_set t (offset + 4) (Obj.repr a3); Uniform_array.unsafe_set t (offset + 5) (Obj.repr a4); Uniform_array.unsafe_set t (offset + 6) (Obj.repr a5); Uniform_array.unsafe_set t (offset + 7) (Obj.repr a6); Uniform_array.unsafe_set t (offset + 8) (Obj.repr a7); pointer ;; let new9 t a0 a1 a2 a3 a4 a5 a6 a7 a8 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); Uniform_array.unsafe_set t (offset + 3) (Obj.repr a2); Uniform_array.unsafe_set t (offset + 4) (Obj.repr a3); Uniform_array.unsafe_set t (offset + 5) (Obj.repr a4); Uniform_array.unsafe_set t (offset + 6) (Obj.repr a5); Uniform_array.unsafe_set t (offset + 7) (Obj.repr a6); Uniform_array.unsafe_set t (offset + 8) (Obj.repr a7); Uniform_array.unsafe_set t (offset + 9) (Obj.repr a8); pointer ;; let new10 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); Uniform_array.unsafe_set t (offset + 3) (Obj.repr a2); Uniform_array.unsafe_set t (offset + 4) (Obj.repr a3); Uniform_array.unsafe_set t (offset + 5) (Obj.repr a4); Uniform_array.unsafe_set t (offset + 6) (Obj.repr a5); Uniform_array.unsafe_set t (offset + 7) (Obj.repr a6); Uniform_array.unsafe_set t (offset + 8) (Obj.repr a7); Uniform_array.unsafe_set t (offset + 9) (Obj.repr a8); Uniform_array.unsafe_set t (offset + 10) (Obj.repr a9); pointer ;; let new11 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); Uniform_array.unsafe_set t (offset + 3) (Obj.repr a2); Uniform_array.unsafe_set t (offset + 4) (Obj.repr a3); Uniform_array.unsafe_set t (offset + 5) (Obj.repr a4); Uniform_array.unsafe_set t (offset + 6) (Obj.repr a5); Uniform_array.unsafe_set t (offset + 7) (Obj.repr a6); Uniform_array.unsafe_set t (offset + 8) (Obj.repr a7); Uniform_array.unsafe_set t (offset + 9) (Obj.repr a8); Uniform_array.unsafe_set t (offset + 10) (Obj.repr a9); Uniform_array.unsafe_set t (offset + 11) (Obj.repr a10); pointer ;; let new12 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); Uniform_array.unsafe_set t (offset + 3) (Obj.repr a2); Uniform_array.unsafe_set t (offset + 4) (Obj.repr a3); Uniform_array.unsafe_set t (offset + 5) (Obj.repr a4); Uniform_array.unsafe_set t (offset + 6) (Obj.repr a5); Uniform_array.unsafe_set t (offset + 7) (Obj.repr a6); Uniform_array.unsafe_set t (offset + 8) (Obj.repr a7); Uniform_array.unsafe_set t (offset + 9) (Obj.repr a8); Uniform_array.unsafe_set t (offset + 10) (Obj.repr a9); Uniform_array.unsafe_set t (offset + 11) (Obj.repr a10); Uniform_array.unsafe_set t (offset + 12) (Obj.repr a11); pointer ;; let new13 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); Uniform_array.unsafe_set t (offset + 3) (Obj.repr a2); Uniform_array.unsafe_set t (offset + 4) (Obj.repr a3); Uniform_array.unsafe_set t (offset + 5) (Obj.repr a4); Uniform_array.unsafe_set t (offset + 6) (Obj.repr a5); Uniform_array.unsafe_set t (offset + 7) (Obj.repr a6); Uniform_array.unsafe_set t (offset + 8) (Obj.repr a7); Uniform_array.unsafe_set t (offset + 9) (Obj.repr a8); Uniform_array.unsafe_set t (offset + 10) (Obj.repr a9); Uniform_array.unsafe_set t (offset + 11) (Obj.repr a10); Uniform_array.unsafe_set t (offset + 12) (Obj.repr a11); Uniform_array.unsafe_set t (offset + 13) (Obj.repr a12); pointer ;; let new14 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 = let pointer = malloc t in let offset = Pointer.header_index pointer in Uniform_array.unsafe_set t (offset + 1) (Obj.repr a0); Uniform_array.unsafe_set t (offset + 2) (Obj.repr a1); Uniform_array.unsafe_set t (offset + 3) (Obj.repr a2); Uniform_array.unsafe_set t (offset + 4) (Obj.repr a3); Uniform_array.unsafe_set t (offset + 5) (Obj.repr a4); Uniform_array.unsafe_set t (offset + 6) (Obj.repr a5); Uniform_array.unsafe_set t (offset + 7) (Obj.repr a6); Uniform_array.unsafe_set t (offset + 8) (Obj.repr a7); Uniform_array.unsafe_set t (offset + 9) (Obj.repr a8); Uniform_array.unsafe_set t (offset + 10) (Obj.repr a9); Uniform_array.unsafe_set t (offset + 11) (Obj.repr a10); Uniform_array.unsafe_set t (offset + 12) (Obj.repr a11); Uniform_array.unsafe_set t (offset + 13) (Obj.repr a12); Uniform_array.unsafe_set t (offset + 14) (Obj.repr a13); pointer ;; let get t p slot = Obj.obj (Uniform_array.get t (Pointer.slot_index p slot)) let unsafe_get t p slot = Obj.obj (Uniform_array.unsafe_get t (Pointer.slot_index p slot)) ;; let set t p slot x = Uniform_array.set t (Pointer.slot_index p slot) (Obj.repr x) let unsafe_set t p slot x = Uniform_array.unsafe_set t (Pointer.slot_index p slot) (Obj.repr x) ;; let get_tuple (type tuple) (t : (tuple, _) Slots.t t) pointer = let metadata = metadata t in let len = metadata.slots_per_tuple in if len = 1 then get t pointer Slot.t0 else (Obj.magic (Uniform_array.sub t ~pos:(Pointer.first_slot_index pointer) ~len : Obj.t Uniform_array.t) : tuple) ;; end include Pool module Unsafe = struct include Pool let create slots ~capacity = create_with_dummy slots ~capacity ~dummy:None end module Debug (Pool : S) = struct open Pool let check_invariant = ref true let show_messages = ref true let debug name ts arg sexp_of_arg sexp_of_result f = let prefix = "Pool." in if !check_invariant then List.iter ts ~f:(invariant ignore); if !show_messages then Debug.eprints (concat [ prefix; name ]) arg sexp_of_arg; let result_or_exn = Result.try_with f in if !show_messages then Debug.eprints (concat [ prefix; name; " result" ]) result_or_exn [%sexp_of: (result, exn) Result.t]; Result.ok_exn result_or_exn ;; module Slots = Slots module Slot = Slot module Pointer = struct open Pointer type nonrec 'slots t = 'slots t [@@deriving sexp_of, typerep] let phys_compare t1 t2 = debug "Pointer.phys_compare" [] (t1, t2) [%sexp_of: _ t * _ t] [%sexp_of: int] (fun () -> phys_compare t1 t2) ;; let phys_equal t1 t2 = debug "Pointer.phys_equal" [] (t1, t2) [%sexp_of: _ t * _ t] [%sexp_of: bool] (fun () -> phys_equal t1 t2) ;; let is_null t = debug "Pointer.is_null" [] t [%sexp_of: _ t] [%sexp_of: bool] (fun () -> is_null t) ;; let null = null module Id = struct open Id type nonrec t = t [@@deriving bin_io, sexp] let of_int63 i = debug "Pointer.Id.of_int63" [] i [%sexp_of: Int63.t] [%sexp_of: t] (fun () -> of_int63 i) ;; let to_int63 t = debug "Pointer.Id.to_int63" [] t [%sexp_of: t] [%sexp_of: Int63.t] (fun () -> to_int63 t) ;; end end type nonrec 'slots t = 'slots t [@@deriving sexp_of] let invariant = invariant let length = length let id_of_pointer t pointer = debug "id_of_pointer" [ t ] pointer [%sexp_of: _ Pointer.t] [%sexp_of: Pointer.Id.t] (fun () -> id_of_pointer t pointer) ;; let pointer_of_id_exn t id = debug "pointer_of_id_exn" [ t ] id [%sexp_of: Pointer.Id.t] [%sexp_of: _ Pointer.t] (fun () -> pointer_of_id_exn t id) ;; let pointer_is_valid t pointer = debug "pointer_is_valid" [ t ] pointer [%sexp_of: _ Pointer.t] [%sexp_of: bool] (fun () -> pointer_is_valid t pointer) ;; let create slots ~capacity ~dummy = debug "create" [] capacity [%sexp_of: int] [%sexp_of: _ t] (fun () -> create slots ~capacity ~dummy) ;; let max_capacity ~slots_per_tuple = debug "max_capacity" [] slots_per_tuple [%sexp_of: int] [%sexp_of: int] (fun () -> max_capacity ~slots_per_tuple) ;; let capacity t = debug "capacity" [ t ] t [%sexp_of: _ t] [%sexp_of: int] (fun () -> capacity t) ;; let grow ?capacity t = debug "grow" [ t ] (`capacity capacity) [%sexp_of: [ `capacity of int option ]] [%sexp_of: _ t] (fun () -> grow ?capacity t) ;; let is_full t = debug "is_full" [ t ] t [%sexp_of: _ t] [%sexp_of: bool] (fun () -> is_full t) ;; let unsafe_free t p = debug "unsafe_free" [ t ] p [%sexp_of: _ Pointer.t] [%sexp_of: unit] (fun () -> unsafe_free t p) ;; let free t p = debug "free" [ t ] p [%sexp_of: _ Pointer.t] [%sexp_of: unit] (fun () -> free t p) ;; let debug_new t f = debug "new" [ t ] () [%sexp_of: unit] [%sexp_of: _ Pointer.t] f let new1 t a0 = debug_new t (fun () -> new1 t a0) let new2 t a0 a1 = debug_new t (fun () -> new2 t a0 a1) let new3 t a0 a1 a2 = debug_new t (fun () -> new3 t a0 a1 a2) let new4 t a0 a1 a2 a3 = debug_new t (fun () -> new4 t a0 a1 a2 a3) let new5 t a0 a1 a2 a3 a4 = debug_new t (fun () -> new5 t a0 a1 a2 a3 a4) let new6 t a0 a1 a2 a3 a4 a5 = debug_new t (fun () -> new6 t a0 a1 a2 a3 a4 a5) let new7 t a0 a1 a2 a3 a4 a5 a6 = debug_new t (fun () -> new7 t a0 a1 a2 a3 a4 a5 a6) let new8 t a0 a1 a2 a3 a4 a5 a6 a7 = debug_new t (fun () -> new8 t a0 a1 a2 a3 a4 a5 a6 a7) ;; let new9 t a0 a1 a2 a3 a4 a5 a6 a7 a8 = debug_new t (fun () -> new9 t a0 a1 a2 a3 a4 a5 a6 a7 a8) ;; let new10 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 = debug_new t (fun () -> new10 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9) ;; let new11 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 = debug_new t (fun () -> new11 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10) ;; let new12 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 = debug_new t (fun () -> new12 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11) ;; let new13 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 = debug_new t (fun () -> new13 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12) ;; let new14 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 = debug_new t (fun () -> new14 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13) ;; let get_tuple t pointer = debug "get_tuple" [ t ] pointer [%sexp_of: _ Pointer.t] [%sexp_of: _] (fun () -> get_tuple t pointer) ;; let debug_get name f t pointer = debug name [ t ] pointer [%sexp_of: _ Pointer.t] [%sexp_of: _] (fun () -> f t pointer) ;; let get t pointer slot = debug_get "get" get t pointer slot let unsafe_get t pointer slot = debug_get "unsafe_get" unsafe_get t pointer slot let debug_set name f t pointer slot a = debug name [ t ] pointer [%sexp_of: _ Pointer.t] [%sexp_of: unit] (fun () -> f t pointer slot a) ;; let set t pointer slot a = debug_set "set" set t pointer slot a let unsafe_set t pointer slot a = debug_set "unsafe_set" unsafe_set t pointer slot a end module Error_check (Pool : S) = struct open Pool module Slots = Slots module Slot = Slot module Pointer = struct type 'slots t = { mutable is_valid : bool ; pointer : 'slots Pointer.t } [@@deriving sexp_of, typerep] let create pointer = { is_valid = true; pointer } let null () = { is_valid = false; pointer = Pointer.null () } let phys_compare t1 t2 = Pointer.phys_compare t1.pointer t2.pointer let phys_equal t1 t2 = Pointer.phys_equal t1.pointer t2.pointer let is_null t = Pointer.is_null t.pointer let follow t = if not t.is_valid then failwiths ~here:[%here] "attempt to use invalid pointer" t [%sexp_of: _ t]; t.pointer ;; let invalidate t = t.is_valid <- false module Id = Pointer.Id end type 'slots t = 'slots Pool.t [@@deriving sexp_of] let invariant = invariant let length = length let pointer_is_valid t { Pointer.is_valid; pointer } = is_valid && pointer_is_valid t pointer ;; (* We don't do [Pointer.follow pointer], because that would disallow [id_of_pointer t (Pointer.null ())]. *) let id_of_pointer t pointer = id_of_pointer t pointer.Pointer.pointer let pointer_of_id_exn t id = let pointer = pointer_of_id_exn t id in let is_valid = Pool.pointer_is_valid t pointer in { Pointer.is_valid; pointer } ;; let create = create let capacity = capacity let max_capacity = max_capacity let grow = grow let is_full = is_full let get_tuple t p = get_tuple t (Pointer.follow p) let get t p = get t (Pointer.follow p) let unsafe_get t p = unsafe_get t (Pointer.follow p) let set t p slot v = set t (Pointer.follow p) slot v let unsafe_set t p slot v = unsafe_set t (Pointer.follow p) slot v let unsafe_free t p = unsafe_free t (Pointer.follow p); Pointer.invalidate p ;; let free t p = free t (Pointer.follow p); Pointer.invalidate p ;; let new1 t a0 = Pointer.create (Pool.new1 t a0) let new2 t a0 a1 = Pointer.create (Pool.new2 t a0 a1) let new3 t a0 a1 a2 = Pointer.create (Pool.new3 t a0 a1 a2) let new4 t a0 a1 a2 a3 = Pointer.create (Pool.new4 t a0 a1 a2 a3) let new5 t a0 a1 a2 a3 a4 = Pointer.create (Pool.new5 t a0 a1 a2 a3 a4) let new6 t a0 a1 a2 a3 a4 a5 = Pointer.create (Pool.new6 t a0 a1 a2 a3 a4 a5) let new7 t a0 a1 a2 a3 a4 a5 a6 = Pointer.create (Pool.new7 t a0 a1 a2 a3 a4 a5 a6) let new8 t a0 a1 a2 a3 a4 a5 a6 a7 = Pointer.create (Pool.new8 t a0 a1 a2 a3 a4 a5 a6 a7) ;; let new9 t a0 a1 a2 a3 a4 a5 a6 a7 a8 = Pointer.create (Pool.new9 t a0 a1 a2 a3 a4 a5 a6 a7 a8) ;; let new10 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 = Pointer.create (Pool.new10 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9) ;; let new11 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 = Pointer.create (Pool.new11 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10) ;; let new12 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 = Pointer.create (Pool.new12 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11) ;; let new13 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 = Pointer.create (Pool.new13 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12) ;; let new14 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 = Pointer.create (Pool.new14 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13) ;; end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>