Source file picos_aux_htbl.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
let[@inline never] impossible () = failwith "impossible"
let ceil_pow_2_minus_1 n =
let n = Nativeint.of_int n in
let n = Nativeint.logor n (Nativeint.shift_right_logical n 1) in
let n = Nativeint.logor n (Nativeint.shift_right_logical n 2) in
let n = Nativeint.logor n (Nativeint.shift_right_logical n 4) in
let n = Nativeint.logor n (Nativeint.shift_right_logical n 8) in
let n = Nativeint.logor n (Nativeint.shift_right_logical n 16) in
Nativeint.to_int
(if Sys.int_size > 32 then
Nativeint.logor n (Nativeint.shift_right_logical n 32)
else n)
module Atomic = Multicore_magic.Transparent_atomic
module Atomic_array = Multicore_magic.Atomic_array
type 'k hashed_type = (module Stdlib.Hashtbl.HashedType with type t = 'k)
type ('k, 'v, _) tdt =
| Nil : ('k, 'v, [> `Nil ]) tdt
| Cons : {
key : 'k;
value : 'v;
rest : ('k, 'v, [ `Nil | `Cons ]) tdt;
}
-> ('k, 'v, [> `Cons ]) tdt
| Resize : {
spine : ('k, 'v, [ `Nil | `Cons ]) tdt;
}
-> ('k, 'v, [> `Resize ]) tdt
(** During resizing and snapshotting target buckets will be initialized
with a physically unique [Resize] value and the source buckets will
then be gradually updated to [Resize] values and the target buckets
updated with data from the source buckets. *)
type ('k, 'v) bucket =
| B : ('k, 'v, [< `Nil | `Cons | `Resize ]) tdt -> ('k, 'v) bucket
[@@unboxed]
type ('k, 'v) pending =
| Nothing
| Resize of {
buckets : ('k, 'v) bucket Atomic_array.t;
non_linearizable_size : int Atomic.t array;
}
type ('k, 'v) state = {
hash : 'k -> int;
buckets : ('k, 'v) bucket Atomic_array.t;
equal : 'k -> 'k -> bool;
non_linearizable_size : int Atomic.t array;
pending : ('k, 'v) pending;
min_buckets : int;
max_buckets : int;
}
(** This record is [7 + 1] words and should be aligned on such a boundary on the
second generation heap. It is probably not worth it to pad it further. *)
type ('k, 'v) t = ('k, 'v) state Atomic.t
let lo_buckets = 1 lsl 3
and hi_buckets =
let mask = ceil_pow_2_minus_1 Sys.max_array_length in
mask lxor (mask lsr 1)
let min_buckets_default = 1 lsl 4
and max_buckets_default = Int.min hi_buckets (1 lsl 30 )
let create (type k) ?hashed_type ?min_buckets ?max_buckets () =
let min_buckets =
match min_buckets with
| None -> min_buckets_default
| Some n ->
let n = Int.max lo_buckets n |> Int.min hi_buckets in
ceil_pow_2_minus_1 (n - 1) + 1
in
let max_buckets =
match max_buckets with
| None -> Int.max min_buckets max_buckets_default
| Some n ->
let n = Int.max min_buckets n |> Int.min hi_buckets in
ceil_pow_2_minus_1 (n - 1) + 1
in
let equal, hash =
match hashed_type with
| None ->
(( = ), Stdlib.Hashtbl.seeded_hash (Int64.to_int (Random.bits64 ())))
| Some ((module Hashed_type) : k hashed_type) ->
(Hashed_type.equal, Hashed_type.hash)
in
{
hash;
buckets = Atomic_array.make min_buckets (B Nil);
equal;
non_linearizable_size =
Array.init
(ceil_pow_2_minus_1
(Multicore_magic.instantaneous_domain_index () lor 1)
)
(fun _ -> Atomic.make_contended 0);
pending = Nothing;
min_buckets;
max_buckets;
}
|> Atomic.make_contended
let hashed_type_of (type k) (t : (k, _) t) : k hashed_type =
let r = Atomic.get t in
(module struct
type t = k
let hash = r.hash
and equal = r.equal
end)
let min_buckets_of t = (Atomic.get t).min_buckets
let max_buckets_of t = (Atomic.get t).max_buckets
let rec take_at backoff bs i =
match Atomic_array.unsafe_fenceless_get bs i with
| B ((Nil | Cons _) as spine) ->
if
Atomic_array.unsafe_compare_and_set bs i (B spine)
(B (Resize { spine }))
then spine
else take_at (Backoff.once backoff) bs i
| B (Resize spine_r) -> spine_r.spine
let rec copy_all r target i t step =
let i = (i + step) land (Atomic_array.length target - 1) in
let spine = take_at Backoff.default r.buckets i in
let (B before) = Atomic_array.unsafe_fenceless_get target i in
Atomic.get t == r
&& begin
begin
match before with
| Resize _ ->
Atomic_array.unsafe_compare_and_set target i (B before) (B spine)
|> ignore
| Nil | Cons _ -> ()
end;
i = 0 || copy_all r target i t step
end
let rec split_all r target i t step =
let i = (i + step) land (Atomic_array.length r.buckets - 1) in
let spine = take_at Backoff.default r.buckets i in
let high = Atomic_array.length r.buckets in
let[@tail_mod_cons] rec filter t msk chk = function
| Nil -> Nil
| Cons r ->
if t r.key land msk = chk then
Cons { r with rest = filter t msk chk r.rest }
else filter t msk chk r.rest
in
let after_lo = filter r.hash high 0 spine in
let after_hi = filter r.hash high high spine in
let (B before_lo) = Atomic_array.unsafe_fenceless_get target i in
let (B before_hi) = Atomic_array.unsafe_fenceless_get target (i + high) in
Atomic.get t == r
&& begin
begin
match before_lo with
| Resize _ ->
Atomic_array.unsafe_compare_and_set target i (B before_lo)
(B after_lo)
|> ignore
| Nil | Cons _ -> ()
end;
begin
match before_hi with
| Resize _ ->
Atomic_array.unsafe_compare_and_set target (i + high) (B before_hi)
(B after_hi)
|> ignore
| Nil | Cons _ -> ()
end;
i = 0 || split_all r target i t step
end
let rec merge_all r target i t step =
let i = (i + step) land (Atomic_array.length target - 1) in
let spine_lo = take_at Backoff.default r.buckets i in
let spine_hi =
take_at Backoff.default r.buckets (i + Atomic_array.length target)
in
let[@tail_mod_cons] rec merge rest = function
| Nil -> rest
| Cons r -> Cons { r with rest = merge rest r.rest }
in
let ((Nil | Cons _) as after) = merge spine_lo spine_hi in
let (B before) = Atomic_array.unsafe_fenceless_get target i in
Atomic.get t == r
&& begin
begin
match before with
| Resize _ ->
Atomic_array.unsafe_compare_and_set target i (B before) (B after)
|> ignore
| Nil | Cons _ -> ()
end;
i = 0 || merge_all r target i t step
end
let[@inline never] rec finish t r =
match r.pending with
| Nothing -> r
| Resize { buckets; non_linearizable_size } ->
let high_source = Atomic_array.length r.buckets in
let high_target = Atomic_array.length buckets in
let step = Int64.to_int (Random.bits64 ()) lor 1 in
if
if high_source < high_target then begin
split_all r buckets 0 t step
end
else if high_target < high_source then begin
merge_all r buckets 0 t step
end
else begin
copy_all r buckets 0 t step
end
then
let new_r =
{ r with buckets; non_linearizable_size; pending = Nothing }
in
if Atomic.compare_and_set t r new_r then new_r
else finish t (Atomic.get t)
else finish t (Atomic.get t)
(** This must be called with [r.pending == Nothing]. *)
let[@inline never] try_resize t r new_capacity ~clear =
let resize_avoid_aba =
if clear then B Nil else B (Resize { spine = Sys.opaque_identity Nil })
in
let buckets = Atomic_array.make new_capacity resize_avoid_aba in
let non_linearizable_size =
if clear then
Array.init (Array.length r.non_linearizable_size) @@ fun _ ->
Atomic.make_contended 0
else r.non_linearizable_size
in
let new_r = { r with pending = Resize { buckets; non_linearizable_size } } in
Atomic.compare_and_set t r new_r
&& begin
finish t new_r |> ignore;
true
end
let rec adjust_estimated_size t r mask delta result =
let i = Multicore_magic.instantaneous_domain_index () in
let n = Array.length r.non_linearizable_size in
if i < n then begin
Atomic.fetch_and_add (Array.unsafe_get r.non_linearizable_size i) delta
|> ignore;
if
r.pending == Nothing
&& Int64.to_int (Random.bits64 ()) land mask = 0
&& Atomic.get t == r
then begin
let estimated_size r =
let cs = r.non_linearizable_size in
let n = Array.length cs - 1 in
let rec estimated_size cs n sum =
let n = n - 1 in
if 0 <= n then
estimated_size cs n (sum + Atomic.get (Array.unsafe_get cs n))
else sum
in
estimated_size cs n (Atomic.get (Array.unsafe_get cs n))
in
let estimated_size = estimated_size r in
let capacity = Atomic_array.length r.buckets in
if capacity < estimated_size && capacity < r.max_buckets then
try_resize t r (capacity + capacity) ~clear:false |> ignore
else if
r.min_buckets < capacity
&& estimated_size + estimated_size + estimated_size < capacity
then try_resize t r (capacity lsr 1) ~clear:false |> ignore
end;
result
end
else
let new_cs =
Array.init (n + n + 1) @@ fun i ->
if i < n then Array.unsafe_get r.non_linearizable_size i
else Atomic.make_contended 0
in
let new_r = { r with non_linearizable_size = new_cs } in
let r = if Atomic.compare_and_set t r new_r then new_r else Atomic.get t in
adjust_estimated_size t r mask delta result
(** [get] only returns with a state where [pending = Nothing]. *)
let[@inline] get t =
let r = Atomic.get t in
if r.pending == Nothing then r else finish t r
let rec exists t key = function
| Nil -> false
| Cons r ->
let result = t r.key key in
if result then result else exists t key r.rest
let mem t key =
let r = Atomic.get t in
let h = r.hash key in
let mask = Atomic_array.length r.buckets - 1 in
let i = h land mask in
match Atomic_array.unsafe_fenceless_get r.buckets i with
| B Nil -> false
| B (Cons cons_r) ->
let result = r.equal cons_r.key key in
if result then result else exists r.equal key cons_r.rest
| B (Resize resize_r) ->
exists r.equal key resize_r.spine
let rec assoc t key = function
| Nil -> raise_notrace Not_found
| Cons r -> if t r.key key then r.value else assoc t key r.rest
let find_exn t key =
let r = Atomic.get t in
let h = r.hash key in
let mask = Atomic_array.length r.buckets - 1 in
let i = h land mask in
match Atomic_array.unsafe_fenceless_get r.buckets i with
| B Nil -> raise_notrace Not_found
| B (Cons cons_r) ->
if r.equal cons_r.key key then cons_r.value
else assoc r.equal key cons_r.rest
| B (Resize resize_r) ->
assoc r.equal key resize_r.spine
let rec try_add t key value backoff =
let r = Atomic.get t in
let h = r.hash key in
let mask = Atomic_array.length r.buckets - 1 in
let i = h land mask in
match Atomic_array.unsafe_fenceless_get r.buckets i with
| B Nil ->
let after = Cons { key; value; rest = Nil } in
if Atomic_array.unsafe_compare_and_set r.buckets i (B Nil) (B after) then
adjust_estimated_size t r mask 1 true
else try_add t key value (Backoff.once backoff)
| B (Cons _ as before) ->
if exists r.equal key before then false
else
let after = Cons { key; value; rest = before } in
if Atomic_array.unsafe_compare_and_set r.buckets i (B before) (B after)
then adjust_estimated_size t r mask 1 true
else try_add t key value (Backoff.once backoff)
| B (Resize _) ->
let _ = finish t (Atomic.get t) in
try_add t key value Backoff.default
type ('v, _, _) op =
| Compare : ('v, 'v, bool) op
| Exists : ('v, _, bool) op
| Return : ('v, _, 'v) op
let rec try_reassoc :
type v c r. (_, v) t -> _ -> c -> v -> (v, c, r) op -> _ -> r =
fun t key present future op backoff ->
let r = Atomic.get t in
let h = r.hash key in
let mask = Atomic_array.length r.buckets - 1 in
let i = h land mask in
let not_found (type v c r) (op : (v, c, r) op) : r =
match op with
| Compare -> false
| Exists -> false
| Return -> raise_notrace Not_found
in
match Atomic_array.unsafe_fenceless_get r.buckets i with
| B Nil -> not_found op
| B (Cons cons_r as before) -> begin
if r.equal cons_r.key key then
if
match op with
| Exists | Return -> true
| Compare -> cons_r.value == present
then
let after = Cons { key; value = future; rest = cons_r.rest } in
if
Atomic_array.unsafe_compare_and_set r.buckets i (B before) (B after)
then
match op with
| Compare -> true
| Exists -> true
| Return -> cons_r.value
else try_reassoc t key present future op (Backoff.once backoff)
else not_found op
else
let[@tail_mod_cons] rec reassoc :
type v c r.
_ -> _ -> c -> v -> (v, c, r) op -> (_, v, 't) tdt -> (_, v, 't) tdt
=
fun t key present future op -> function
| Nil -> raise_notrace Not_found
| Cons r ->
if t key r.key then
match op with
| Exists | Return -> Cons { r with value = future }
| Compare ->
if r.value == present then Cons { r with value = future }
else raise_notrace Not_found
else Cons { r with rest = reassoc t key present future op r.rest }
in
match reassoc r.equal key present future op cons_r.rest with
| rest ->
let after = Cons { cons_r with rest } in
if
Atomic_array.unsafe_compare_and_set r.buckets i (B before)
(B after)
then
match op with
| Compare -> true
| Exists -> true
| Return -> assoc r.equal key cons_r.rest
else try_reassoc t key present future op (Backoff.once backoff)
| exception Not_found -> not_found op
end
| B (Resize _) ->
let _ = finish t (Atomic.get t) in
try_reassoc t key present future op Backoff.default
let rec try_dissoc : type v c r. (_, v) t -> _ -> c -> (v, c, r) op -> _ -> r =
fun t key present op backoff ->
let r = Atomic.get t in
let h = r.hash key in
let mask = Atomic_array.length r.buckets - 1 in
let i = h land mask in
let not_found (type v c r) (op : (v, c, r) op) : r =
match op with
| Compare -> false
| Exists -> false
| Return -> raise_notrace Not_found
in
match Atomic_array.unsafe_fenceless_get r.buckets i with
| B Nil -> not_found op
| B (Cons cons_r as before) -> begin
if r.equal cons_r.key key then
if
match op with
| Exists | Return -> true
| Compare -> cons_r.value == present
then
if
Atomic_array.unsafe_compare_and_set r.buckets i (B before)
(B cons_r.rest)
then
let res : r =
match op with
| Compare -> true
| Exists -> true
| Return -> cons_r.value
in
adjust_estimated_size t r mask (-1) res
else try_dissoc t key present op (Backoff.once backoff)
else not_found op
else
let[@tail_mod_cons] rec dissoc :
type v c r.
_ -> _ -> c -> (v, c, r) op -> (_, v, 't) tdt -> (_, v, 't) tdt =
fun t key present op -> function
| Nil -> raise_notrace Not_found
| Cons r ->
if t key r.key then
match op with
| Exists | Return -> r.rest
| Compare ->
if r.value == present then r.rest
else raise_notrace Not_found
else Cons { r with rest = dissoc t key present op r.rest }
in
match dissoc r.equal key present op cons_r.rest with
| (Nil | Cons _) as rest ->
if
Atomic_array.unsafe_compare_and_set r.buckets i (B before)
(B (Cons { cons_r with rest }))
then
let res : r =
match op with
| Compare -> true
| Exists -> true
| Return -> assoc r.equal key cons_r.rest
in
adjust_estimated_size t r mask (-1) res
else try_dissoc t key present op (Backoff.once backoff)
| exception Not_found -> not_found op
end
| B (Resize _) ->
let _ = finish t (Atomic.get t) in
try_dissoc t key present op Backoff.default
let rec snapshot t ~clear backoff =
let r = get t in
if try_resize t r (Atomic_array.length r.buckets) ~clear then begin
let snapshot = r.buckets in
let rec loop i kvs () =
match kvs with
| Nil ->
if i = Atomic_array.length snapshot then Seq.Nil
else
loop (i + 1)
(match Atomic_array.unsafe_fenceless_get snapshot i with
| B (Resize spine_r) -> spine_r.spine
| B (Nil | Cons _) ->
assert false)
()
| Cons r -> Seq.Cons ((r.key, r.value), loop i r.rest)
in
loop 0 Nil
end
else snapshot t ~clear (Backoff.once backoff)
let to_seq t = snapshot t ~clear:false Backoff.default
let remove_all t = snapshot t ~clear:true Backoff.default
let find_random_exn t =
let try_find_random_non_empty_bucket t =
let buckets = (Atomic.get t).buckets in
let seed = Int64.to_int (Random.bits64 ()) in
let rec try_find_random_non_empty_bucket buckets seed i =
match Atomic_array.unsafe_fenceless_get buckets i with
| B Nil | B (Resize { spine = Nil }) ->
let mask = Atomic_array.length buckets - 1 in
let i = (i + 1) land mask in
if i <> seed land mask then
try_find_random_non_empty_bucket buckets seed i
else Nil
| B (Cons cons_r) | B (Resize { spine = Cons cons_r }) -> Cons cons_r
in
try_find_random_non_empty_bucket buckets seed
(seed land (Atomic_array.length buckets - 1))
in
match try_find_random_non_empty_bucket t with
| (Cons cons_r as spine : (_, _, [< `Nil | `Cons ]) tdt) ->
if cons_r.rest == Nil then cons_r.key
else
let rec length spine n =
match spine with Nil -> n | Cons r -> length r.rest (n + 1)
in
let n = length cons_r.rest 1 in
let rec nth spine i =
match spine with
| Nil -> impossible ()
| Cons r -> if i <= 0 then r.key else nth r.rest (i - 1)
in
nth spine (Random.int n)
| Nil ->
let bindings = to_seq t |> Array.of_seq in
let n = Array.length bindings in
if n <> 0 then fst (Array.unsafe_get bindings (Random.int n))
else raise_notrace Not_found
let[@inline] try_add t key value = try_add t key value Backoff.default
let[@inline] try_set t key future =
try_reassoc t key future future Exists Backoff.default
let[@inline] try_compare_and_set t key present future =
try_reassoc t key present future Compare Backoff.default
let[@inline] set_exn t key value =
try_reassoc t key key value Return Backoff.default
let[@inline] try_remove t key = try_dissoc t key key Exists Backoff.default
let[@inline] try_compare_and_remove t key present =
try_dissoc t key present Compare Backoff.default
let[@inline] remove_exn t key = try_dissoc t key key Return Backoff.default