Skip to content

Commit 403794c

Browse files
voodoosliam923
authored andcommitted
Refactor index caching. Get rid of the stamping.
1 parent d1cb27e commit 403794c

File tree

4 files changed

+79
-74
lines changed

4 files changed

+79
-74
lines changed

src/analysis/index_occurrences.ml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ let should_ignore_lid (lid : Longident.t Location.loc) =
4444
*)
4545
Location.is_none lid.loc
4646

47-
let iterator ~current_buffer_path ~index ~stamp ~reduce_for_uid =
48-
let add uid loc = Stamped_hashtable.add index ~stamp (uid, loc) () in
47+
let iterator ~current_buffer_path ~index ~reduce_for_uid =
48+
let add uid loc = index := Shape.Uid.Map.add_to_list uid loc !index in
4949
let f ~namespace env path (lid : Longident.t Location.loc) =
5050
log ~title:"index_buffer" "Path: %a" Logger.fmt (Fun.flip Path.print path);
5151
let lid = { lid with loc = set_fname ~file:current_buffer_path lid.loc } in
@@ -89,7 +89,7 @@ let iterator ~current_buffer_path ~index ~stamp ~reduce_for_uid =
8989
in
9090
Ast_iterators.iterator_on_usages ~include_hidden:true ~f
9191

92-
let items ~index ~stamp (config : Mconfig.t) items =
92+
let items index (config : Mconfig.t) items =
9393
let module Shape_reduce = Shape_reduce.Make (struct
9494
let fuel () = Misc_stdlib.Maybe_bounded.of_int 10
9595

@@ -123,7 +123,11 @@ let items ~index ~stamp (config : Mconfig.t) items =
123123
Filename.concat config.query.directory config.query.filename
124124
in
125125
let reduce_for_uid = Shape_reduce.reduce_for_uid in
126-
let iterator = iterator ~current_buffer_path ~index ~stamp ~reduce_for_uid in
127-
match items with
128-
| `Impl items -> List.iter ~f:(iterator.structure_item iterator) items
129-
| `Intf items -> List.iter ~f:(iterator.signature_item iterator) items
126+
let index = ref index in
127+
let iterator = iterator ~current_buffer_path ~index ~reduce_for_uid in
128+
let () =
129+
match items with
130+
| `Impl items -> List.iter ~f:(iterator.structure_item iterator) items
131+
| `Intf items -> List.iter ~f:(iterator.signature_item iterator) items
132+
in
133+
!index

src/analysis/occurrences.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,12 @@ end = struct
186186
end
187187

188188
let get_buffer_locs result uid =
189-
Stamped_hashtable.fold
190-
(fun (uid', loc) () acc ->
191-
if Shape.Uid.equal uid uid' then
192-
Lid_set.add (Index_format.Lid.of_lid loc) acc
193-
else acc)
189+
Shape.Uid.Map.fold
190+
(fun uid' lids acc ->
191+
if Shape.Uid.equal uid uid' then
192+
List.fold_left lids ~init:acc ~f:(fun acc lid ->
193+
Lid_set.add (Index_format.Lid.of_lid lid) acc)
194+
else acc)
194195
(Mtyper.get_index result) Lid_set.empty
195196

196197
let get_external_locs ~(config : Mconfig.t) ~current_buffer_path uid :

src/kernel/mtyper.ml

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@ open Local_store
33

44
let { Logger.log } = Logger.for_section "Mtyper"
55

6-
let index_changelog = Local_store.s_table Stamped_hashtable.create_changelog ()
7-
8-
type index_tbl =
9-
(Shape.Uid.t * Longident.t Location.loc, unit) Stamped_hashtable.t
6+
type index = Longident.t Location.loc list Shape.Uid.Map.t
107

118
(* Forward ref to be filled by analysis.Occurrences *)
129
let index_items :
13-
(index:index_tbl ->
14-
stamp:int ->
10+
(index ->
1511
Mconfig.t ->
1612
[ `Impl of Typedtree.structure_item list
1713
| `Intf of Typedtree.signature_item list ] ->
18-
unit)
14+
index)
1915
ref =
20-
ref (fun ~index:_ ~stamp:_ _config _item -> ())
16+
ref (fun acc _config _item -> acc)
2117
let set_index_items f = index_items := f
2218

2319
type ('p, 't) item =
@@ -30,7 +26,8 @@ type ('p, 't) item =
3026
part_rev_sg : Types.signature_item list;
3127
part_errors : exn list;
3228
part_checks : Typecore.delayed_check list;
33-
part_warnings : Warnings.state
29+
part_warnings : Warnings.state;
30+
part_index : index
3431
}
3532

3633
type typedtree =
@@ -53,8 +50,7 @@ type 'a cache_result =
5350
snapshot : Types.snapshot;
5451
ident_stamp : int;
5552
uid_stamp : int;
56-
value : 'a;
57-
index : (Shape.Uid.t * Longident.t Location.loc, unit) Stamped_hashtable.t
53+
value : 'a
5854
}
5955

6056
let cache : typedtree_items option cache_result option ref = s_ref None
@@ -72,8 +68,7 @@ let get_cache config =
7268
| Some ({ snapshot; _ } as c) when Types.is_valid snapshot -> c
7369
| Some _ | None ->
7470
let env, snapshot, ident_stamp, uid_stamp = fresh_env config in
75-
let index = Stamped_hashtable.create !index_changelog 256 in
76-
{ env; snapshot; ident_stamp; uid_stamp; value = None; index }
71+
{ env; snapshot; ident_stamp; uid_stamp; value = None }
7772

7873
let return_and_cache status =
7974
cache := Some { status with value = Some status.value };
@@ -87,7 +82,6 @@ type result =
8782
stamp : int;
8883
initial_uid_stamp : int;
8984
typedtree : typedtree_items;
90-
index : (Shape.Uid.t * Longident.t Location.loc, unit) Stamped_hashtable.t;
9185
cache_stat : typer_cache_stats
9286
}
9387

@@ -111,11 +105,12 @@ let compatible_prefix_rev result_items tree_items =
111105
in
112106
aux [] (result_items, tree_items)
113107

114-
let[@tail_mod_cons] rec type_structure caught env sg = function
108+
let[@tail_mod_cons] rec type_structure config caught env index sg = function
115109
| parsetree_item :: rest ->
116110
let items, sg', part_env =
117111
Typemod.merlin_type_structure env sg [ parsetree_item ]
118112
in
113+
let part_index = !index_items index config (`Impl items.str_items) in
119114
let typedtree_items =
120115
(items.Typedtree.str_items, items.Typedtree.str_type)
121116
in
@@ -130,14 +125,15 @@ let[@tail_mod_cons] rec type_structure caught env sg = function
130125
part_uid = Shape.Uid.get_current_stamp ();
131126
part_errors = !caught;
132127
part_checks = !Typecore.delayed_checks;
133-
part_warnings = Warnings.backup ()
128+
part_warnings = Warnings.backup ();
129+
part_index
134130
}
135131
in
136-
item :: type_structure caught part_env part_rev_sg rest
132+
item :: type_structure config caught part_env part_index part_rev_sg rest
137133
| [] -> []
138134

139-
let[@tail_mod_cons] rec type_signature caught env sg psg_modalities psg_loc =
140-
function
135+
let[@tail_mod_cons] rec type_signature config caught env index sg psg_modalities
136+
psg_loc = function
141137
| parsetree_item :: rest ->
142138
let { Typedtree.sig_final_env = part_env;
143139
sig_items;
@@ -150,6 +146,7 @@ let[@tail_mod_cons] rec type_signature caught env sg psg_modalities psg_loc =
150146
[ parsetree_item ])
151147
in
152148
let part_rev_sg = List.rev_append sig_type sg in
149+
let part_index = !index_items index config (`Intf sig_items) in
153150
let item =
154151
{ parsetree_item;
155152
typedtree_items = (sig_items, sig_type);
@@ -160,26 +157,34 @@ let[@tail_mod_cons] rec type_signature caught env sg psg_modalities psg_loc =
160157
part_uid = Shape.Uid.get_current_stamp ();
161158
part_errors = !caught;
162159
part_checks = !Typecore.delayed_checks;
163-
part_warnings = Warnings.backup ()
160+
part_warnings = Warnings.backup ();
161+
part_index
164162
}
165163
in
166164
item
167-
:: type_signature caught part_env part_rev_sg psg_modalities psg_loc rest
165+
:: type_signature config caught part_env part_index part_rev_sg
166+
psg_modalities psg_loc rest
168167
| [] -> []
169168

170169
let type_implementation config caught parsetree =
171-
let { env; snapshot; ident_stamp; uid_stamp; value = cached_value; index; _ }
172-
=
170+
let { env; snapshot; ident_stamp; uid_stamp; value = cached_value; _ } =
173171
get_cache config
174172
in
175173
let rev_prefix, parsetree_suffix, cache_stats =
176174
match cached_value with
177175
| Some (Implementation_items items) -> compatible_prefix_rev items parsetree
178176
| Some (Interface_items _) | None -> ([], parsetree, Miss)
179177
in
180-
let env', sg', snap', stamp', uid_stamp', warn' =
178+
let env', sg', snap', stamp', uid_stamp', warn', index' =
181179
match rev_prefix with
182-
| [] -> (env, [], snapshot, ident_stamp, uid_stamp, Warnings.backup ())
180+
| [] ->
181+
( env,
182+
[],
183+
snapshot,
184+
ident_stamp,
185+
uid_stamp,
186+
Warnings.backup (),
187+
Shape.Uid.Map.empty )
183188
| x :: _ ->
184189
caught := x.part_errors;
185190
Typecore.delayed_checks := x.part_checks;
@@ -188,30 +193,21 @@ let type_implementation config caught parsetree =
188193
x.part_snapshot,
189194
x.part_stamp,
190195
x.part_uid,
191-
x.part_warnings )
196+
x.part_warnings,
197+
x.part_index )
192198
in
193199
Btype.backtrack snap';
194200
Warnings.restore warn';
195201
Env.cleanup_functor_caches ~stamp:stamp';
196-
let stamp = List.length rev_prefix - 1 in
197-
Stamped_hashtable.backtrack !index_changelog ~stamp;
198202
Env.cleanup_usage_tables ~stamp:uid_stamp';
199203
Shape.Uid.restore_stamp uid_stamp';
200-
let suffix = type_structure caught env' sg' parsetree_suffix in
201-
let () =
202-
List.iteri
203-
~f:(fun i { typedtree_items = items, _; _ } ->
204-
let stamp = stamp + i + 1 in
205-
!index_items ~index ~stamp config (`Impl items))
206-
suffix
207-
in
204+
let suffix = type_structure config caught env' index' sg' parsetree_suffix in
208205
let value = Implementation_items (List.rev_append rev_prefix suffix) in
209-
( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value; index },
206+
( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value },
210207
cache_stats )
211208

212209
let type_interface config caught (parsetree : Parsetree.signature) =
213-
let { env; snapshot; ident_stamp; uid_stamp; value = cached_value; index; _ }
214-
=
210+
let { env; snapshot; ident_stamp; uid_stamp; value = cached_value; _ } =
215211
get_cache config
216212
in
217213
let rev_prefix, parsetree_suffix, cache_stats =
@@ -230,9 +226,16 @@ let type_interface config caught (parsetree : Parsetree.signature) =
230226
| Some (Interface_items _) | Some (Implementation_items _) | None ->
231227
([], parsetree.psg_items, Miss)
232228
in
233-
let env', sg', snap', stamp', uid_stamp', warn' =
229+
let env', sg', snap', stamp', uid_stamp', warn', index' =
234230
match rev_prefix with
235-
| [] -> (env, [], snapshot, ident_stamp, uid_stamp, Warnings.backup ())
231+
| [] ->
232+
( env,
233+
[],
234+
snapshot,
235+
ident_stamp,
236+
uid_stamp,
237+
Warnings.backup (),
238+
Shape.Uid.Map.empty )
236239
| x :: _ ->
237240
caught := x.part_errors;
238241
Typecore.delayed_checks := x.part_checks;
@@ -241,25 +244,17 @@ let type_interface config caught (parsetree : Parsetree.signature) =
241244
x.part_snapshot,
242245
x.part_stamp,
243246
x.part_uid,
244-
x.part_warnings )
247+
x.part_warnings,
248+
x.part_index )
245249
in
246250
Btype.backtrack snap';
247251
Warnings.restore warn';
248252
Env.cleanup_functor_caches ~stamp:stamp';
249-
let stamp = List.length rev_prefix in
250-
Stamped_hashtable.backtrack !index_changelog ~stamp;
251253
Env.cleanup_usage_tables ~stamp:uid_stamp';
252254
Shape.Uid.restore_stamp uid_stamp';
253255
let suffix =
254-
type_signature caught env' sg' parsetree.psg_modalities parsetree.psg_loc
255-
parsetree_suffix
256-
in
257-
let () =
258-
List.iteri
259-
~f:(fun i { typedtree_items = items, _; _ } ->
260-
let stamp = stamp + i + 1 in
261-
!index_items ~index ~stamp config (`Intf items))
262-
suffix
256+
type_signature config caught env' index' sg' parsetree.psg_modalities
257+
parsetree.psg_loc parsetree_suffix
263258
in
264259
(* transl an empty signature to get the sig_modalities and sig_sloc *)
265260
let ({ sig_final_env = _;
@@ -281,7 +276,7 @@ let type_interface config caught (parsetree : Parsetree.signature) =
281276
sig_sloc
282277
}
283278
in
284-
( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value; index },
279+
( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value },
285280
cache_stats )
286281

287282
let run config parsetree =
@@ -313,7 +308,6 @@ let run config parsetree =
313308
stamp;
314309
initial_uid_stamp = cached_result.uid_stamp;
315310
typedtree = cached_result.value;
316-
index = cached_result.index;
317311
cache_stat
318312
}
319313

@@ -362,7 +356,15 @@ let get_typedtree t =
362356
sig_sloc
363357
}
364358

365-
let get_index t = t.index
359+
let get_index t =
360+
let of_items items =
361+
List.last items
362+
|> Option.value_map ~default:Shape.Uid.Map.empty
363+
~f:(fun { part_index; _ } -> part_index)
364+
in
365+
match t.typedtree with
366+
| Implementation_items items -> of_items items
367+
| Interface_items { items; _ } -> of_items items
366368

367369
let get_stamp t = t.stamp
368370

src/kernel/mtyper.mli

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@ type typedtree =
1414

1515
type typer_cache_stats = Miss | Hit of { reused : int; typed : int }
1616

17-
type index_tbl =
18-
(Shape.Uid.t * Longident.t Location.loc, unit) Stamped_hashtable.t
17+
type index = Longident.t Location.loc list Shape.Uid.Map.t
1918

2019
val set_index_items :
21-
(index:index_tbl ->
22-
stamp:int ->
20+
(index ->
2321
Mconfig.t ->
2422
[ `Impl of Typedtree.structure_item list
2523
| `Intf of Typedtree.signature_item list ] ->
26-
unit) ->
24+
index) ->
2725
unit
2826

2927
val run : Mconfig.t -> Mreader.parsetree -> result
@@ -32,7 +30,7 @@ val get_env : ?pos:Msource.position -> result -> Env.t
3230

3331
val get_typedtree : result -> typedtree
3432

35-
val get_index : result -> index_tbl
33+
val get_index : result -> index
3634

3735
val get_stamp : result -> int
3836

0 commit comments

Comments
 (0)