Skip to content

Commit 46ce4e6

Browse files
committed
another backport from OCaml's Weak
1 parent 8167950 commit 46ce4e6

File tree

2 files changed

+4
-23
lines changed

2 files changed

+4
-23
lines changed

CHANGES

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

2-
o fixed performance bug in weak hash tables implementation
2+
o fixed performance bugs in weak hash tables implementation,
3+
by back porting some old fixes from OCaml's `Weak` module
34
(reported by Edwin Török)
45

56
version 1.3

hashcons.ml

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,6 @@ let clear t =
4545
t.totsize <- 0;
4646
t.limit <- 3
4747

48-
let fold f t init =
49-
let rec fold_bucket i b accu =
50-
if i >= Weak.length b then accu else
51-
match Weak.get b i with
52-
| Some v -> fold_bucket (i+1) b (f v accu)
53-
| None -> fold_bucket (i+1) b accu
54-
in
55-
Array.fold_right (fold_bucket 0) t.table init
56-
5748
let iter f t =
5849
let rec iter_bucket i b =
5950
if i >= Weak.length b then () else
@@ -78,9 +69,8 @@ let rec resize t =
7869
if newlen > oldlen then begin
7970
let newt = create newlen in
8071
newt.limit <- t.limit + 100; (* prevent resizing of newt *)
81-
fold (fun d () -> add newt d) t ();
72+
iter (fun d -> add newt d) t;
8273
t.table <- newt.table;
83-
t.limit <- t.limit + 2;
8474
end
8575

8676
and add t d =
@@ -186,15 +176,6 @@ module Make(H : HashedType) : (S with type key = H.t) = struct
186176
t.totsize <- 0;
187177
t.limit <- 3
188178

189-
let fold f t init =
190-
let rec fold_bucket i b accu =
191-
if i >= Weak.length b then accu else
192-
match Weak.get b i with
193-
| Some v -> fold_bucket (i+1) b (f v accu)
194-
| None -> fold_bucket (i+1) b accu
195-
in
196-
Array.fold_right (fold_bucket 0) t.table init
197-
198179
let iter f t =
199180
let rec iter_bucket i b =
200181
if i >= Weak.length b then () else
@@ -219,9 +200,8 @@ module Make(H : HashedType) : (S with type key = H.t) = struct
219200
if newlen > oldlen then begin
220201
let newt = create newlen in
221202
newt.limit <- t.limit + 100; (* prevent resizing of newt *)
222-
fold (fun d () -> add newt d) t ();
203+
iter (fun d -> add newt d) t;
223204
t.table <- newt.table;
224-
t.limit <- t.limit + 2;
225205
end
226206

227207
and add t d =

0 commit comments

Comments
 (0)