Skip to content

Commit 87875fc

Browse files
authored
Merge branch 'main' into make-short-paths-lazier
2 parents 567b420 + c1d4ae7 commit 87875fc

File tree

7 files changed

+88
-70
lines changed

7 files changed

+88
-70
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ unreleased
33

44
+ merlin library
55
- Fix completion not working for inlined records labels (#1978, fixes #1977)
6+
- Perform buffer indexing only if the query requires it (#1990 and #1991)
67
- Stop unnecessarily forcing substitutions when initializing short-paths graph (#1988)
8+
79

810
merlin 5.6
911
==========

src/analysis/index_occurrences.ml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ let decl_of_path_or_lid env namespace path lid =
2525
end
2626
| _ -> Env_lookup.by_path path namespace env
2727

28-
let iterator ~current_buffer_path ~index ~stamp ~reduce_for_uid =
29-
let add uid loc = Stamped_hashtable.add index ~stamp (uid, loc) () in
28+
let iterator ~current_buffer_path ~index ~reduce_for_uid =
29+
let add uid loc = index := Shape.Uid.Map.add_to_list uid loc !index in
3030
let f ~namespace env path (lid : Longident.t Location.loc) =
3131
log ~title:"index_buffer" "Path: %a" Logger.fmt
3232
(Fun.flip (Format_doc.compat Path.print) path);
@@ -92,7 +92,7 @@ let iterator ~current_buffer_path ~index ~stamp ~reduce_for_uid =
9292
in
9393
Ast_iterators.iterator_on_usages ~f
9494

95-
let items ~index ~stamp (config : Mconfig.t) items =
95+
let items index (config : Mconfig.t) items =
9696
let module Shape_reduce = Shape_reduce.Make (struct
9797
let fuel = 10
9898

@@ -111,7 +111,11 @@ let items ~index ~stamp (config : Mconfig.t) items =
111111
Filename.concat config.query.directory config.query.filename
112112
in
113113
let reduce_for_uid = Shape_reduce.reduce_for_uid in
114-
let iterator = iterator ~current_buffer_path ~index ~stamp ~reduce_for_uid in
115-
match items with
116-
| `Impl items -> List.iter ~f:(iterator.structure_item iterator) items
117-
| `Intf items -> List.iter ~f:(iterator.signature_item iterator) items
114+
let index = ref index in
115+
let iterator = iterator ~current_buffer_path ~index ~reduce_for_uid in
116+
let () =
117+
match items with
118+
| `Impl items -> List.iter ~f:(iterator.structure_item iterator) items
119+
| `Intf items -> List.iter ~f:(iterator.signature_item iterator) items
120+
in
121+
!index

src/analysis/occurrences.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,11 @@ end = struct
157157
end
158158

159159
let get_buffer_locs result uid =
160-
Stamped_hashtable.fold
161-
(fun (uid', loc) () acc ->
160+
Shape.Uid.Map.fold
161+
(fun uid' lids acc ->
162162
if Shape.Uid.equal uid uid' then
163-
Lid_set.add (Index_format.Lid.of_lid loc) acc
163+
List.fold_left lids ~init:acc ~f:(fun acc lid ->
164+
Lid_set.add (Index_format.Lid.of_lid lid) acc)
164165
else acc)
165166
(Mtyper.get_index result) Lid_set.empty
166167

src/kernel/mtyper.ml

Lines changed: 65 additions & 52 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 =
@@ -29,7 +25,8 @@ type ('p, 't) item =
2925
part_env : Env.t;
3026
part_errors : exn list;
3127
part_checks : Typecore.delayed_check list;
32-
part_warnings : Warnings.state
28+
part_warnings : Warnings.state;
29+
part_index : index lazy_t
3330
}
3431

3532
type typedtree =
@@ -47,8 +44,7 @@ type 'a cache_result =
4744
snapshot : Types.snapshot;
4845
ident_stamp : int;
4946
uid_stamp : int;
50-
value : 'a;
51-
index : (Shape.Uid.t * Longident.t Location.loc, unit) Stamped_hashtable.t
47+
value : 'a
5248
}
5349

5450
let cache : typedtree_items option cache_result option ref = s_ref None
@@ -66,8 +62,7 @@ let get_cache config =
6662
| Some ({ snapshot; _ } as c) when Types.is_valid snapshot -> c
6763
| Some _ | None ->
6864
let env, snapshot, ident_stamp, uid_stamp = fresh_env config in
69-
let index = Stamped_hashtable.create !index_changelog 256 in
70-
{ env; snapshot; ident_stamp; uid_stamp; value = None; index }
65+
{ env; snapshot; ident_stamp; uid_stamp; value = None }
7166

7267
let return_and_cache status =
7368
cache := Some { status with value = Some status.value };
@@ -81,7 +76,6 @@ type result =
8176
stamp : int;
8277
initial_uid_stamp : int;
8378
typedtree : typedtree_items;
84-
index : (Shape.Uid.t * Longident.t Location.loc, unit) Stamped_hashtable.t;
8579
cache_stat : typer_cache_stats
8680
}
8781

@@ -105,11 +99,14 @@ let compatible_prefix result_items tree_items =
10599
in
106100
aux [] (result_items, tree_items)
107101

108-
let rec type_structure caught env = function
102+
let rec type_structure config caught env index = function
109103
| parsetree_item :: rest ->
110104
let items, _, part_env =
111105
Typemod.merlin_type_structure env [ parsetree_item ]
112106
in
107+
let part_index =
108+
lazy (!index_items (Lazy.force index) config (`Impl items.str_items))
109+
in
113110
let typedtree_items =
114111
(items.Typedtree.str_items, items.Typedtree.str_type)
115112
in
@@ -122,17 +119,21 @@ let rec type_structure caught env = function
122119
part_uid = Shape.Uid.get_current_stamp ();
123120
part_errors = !caught;
124121
part_checks = !Typecore.delayed_checks;
125-
part_warnings = Warnings.backup ()
122+
part_warnings = Warnings.backup ();
123+
part_index
126124
}
127125
in
128-
item :: type_structure caught part_env rest
126+
item :: type_structure config caught part_env part_index rest
129127
| [] -> []
130128

131-
let rec type_signature caught env = function
129+
let rec type_signature config caught env index = function
132130
| parsetree_item :: rest ->
133131
let { Typedtree.sig_final_env = part_env; sig_items; sig_type } =
134132
Typemod.merlin_transl_signature env [ parsetree_item ]
135133
in
134+
let part_index =
135+
lazy (!index_items (Lazy.force index) config (`Intf sig_items))
136+
in
136137
let item =
137138
{ parsetree_item;
138139
typedtree_items = (sig_items, sig_type);
@@ -142,82 +143,87 @@ let rec type_signature caught env = function
142143
part_uid = Shape.Uid.get_current_stamp ();
143144
part_errors = !caught;
144145
part_checks = !Typecore.delayed_checks;
145-
part_warnings = Warnings.backup ()
146+
part_warnings = Warnings.backup ();
147+
part_index
146148
}
147149
in
148-
item :: type_signature caught part_env rest
150+
item :: type_signature config caught part_env part_index rest
149151
| [] -> []
150152

151153
let type_implementation config caught parsetree =
152-
let { env; snapshot; ident_stamp; uid_stamp; value = prefix; index; _ } =
154+
let { env; snapshot; ident_stamp; uid_stamp; value = prefix; _ } =
153155
get_cache config
154156
in
155157
let prefix, parsetree, cache_stats =
156158
match prefix with
157159
| Some (`Implementation items) -> compatible_prefix items parsetree
158160
| Some (`Interface _) | None -> ([], parsetree, Miss)
159161
in
160-
let env', snap', stamp', uid_stamp', warn' =
162+
let env', snap', stamp', uid_stamp', warn', index' =
161163
match prefix with
162-
| [] -> (env, snapshot, ident_stamp, uid_stamp, Warnings.backup ())
164+
| [] ->
165+
( env,
166+
snapshot,
167+
ident_stamp,
168+
uid_stamp,
169+
Warnings.backup (),
170+
lazy Shape.Uid.Map.empty )
163171
| x :: _ ->
164172
caught := x.part_errors;
165173
Typecore.delayed_checks := x.part_checks;
166-
(x.part_env, x.part_snapshot, x.part_stamp, x.part_uid, x.part_warnings)
174+
( x.part_env,
175+
x.part_snapshot,
176+
x.part_stamp,
177+
x.part_uid,
178+
x.part_warnings,
179+
x.part_index )
167180
in
168181
Btype.backtrack snap';
169182
Warnings.restore warn';
170183
Env.cleanup_functor_caches ~stamp:stamp';
171-
let stamp = List.length prefix - 1 in
172-
Stamped_hashtable.backtrack !index_changelog ~stamp;
173184
Env.cleanup_usage_tables ~stamp:uid_stamp';
174185
Shape.Uid.restore_stamp uid_stamp';
175-
let suffix = type_structure caught env' parsetree in
176-
let () =
177-
List.iteri
178-
~f:(fun i { typedtree_items = items, _; _ } ->
179-
let stamp = stamp + i + 1 in
180-
!index_items ~index ~stamp config (`Impl items))
181-
suffix
182-
in
186+
let suffix = type_structure config caught env' index' parsetree in
183187
let value = `Implementation (List.rev_append prefix suffix) in
184-
( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value; index },
188+
( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value },
185189
cache_stats )
186190

187191
let type_interface config caught parsetree =
188-
let { env; snapshot; ident_stamp; uid_stamp; value = prefix; index; _ } =
192+
let { env; snapshot; ident_stamp; uid_stamp; value = prefix; _ } =
189193
get_cache config
190194
in
191195
let prefix, parsetree, cache_stats =
192196
match prefix with
193197
| Some (`Interface items) -> compatible_prefix items parsetree
194198
| Some (`Implementation _) | None -> ([], parsetree, Miss)
195199
in
196-
let env', snap', stamp', uid_stamp', warn' =
200+
let env', snap', stamp', uid_stamp', warn', index' =
197201
match prefix with
198-
| [] -> (env, snapshot, ident_stamp, uid_stamp, Warnings.backup ())
202+
| [] ->
203+
( env,
204+
snapshot,
205+
ident_stamp,
206+
uid_stamp,
207+
Warnings.backup (),
208+
lazy Shape.Uid.Map.empty )
199209
| x :: _ ->
200210
caught := x.part_errors;
201211
Typecore.delayed_checks := x.part_checks;
202-
(x.part_env, x.part_snapshot, x.part_stamp, x.part_uid, x.part_warnings)
212+
( x.part_env,
213+
x.part_snapshot,
214+
x.part_stamp,
215+
x.part_uid,
216+
x.part_warnings,
217+
x.part_index )
203218
in
204219
Btype.backtrack snap';
205220
Warnings.restore warn';
206221
Env.cleanup_functor_caches ~stamp:stamp';
207-
let stamp = List.length prefix in
208-
Stamped_hashtable.backtrack !index_changelog ~stamp;
209222
Env.cleanup_usage_tables ~stamp:uid_stamp';
210223
Shape.Uid.restore_stamp uid_stamp';
211-
let suffix = type_signature caught env' parsetree in
212-
let () =
213-
List.iteri
214-
~f:(fun i { typedtree_items = items, _; _ } ->
215-
let stamp = stamp + i + 1 in
216-
!index_items ~index ~stamp config (`Intf items))
217-
suffix
218-
in
224+
let suffix = type_signature config caught env' index' parsetree in
219225
let value = `Interface (List.rev_append prefix suffix) in
220-
( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value; index },
226+
( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value },
221227
cache_stats )
222228

223229
let run config parsetree =
@@ -246,7 +252,6 @@ let run config parsetree =
246252
stamp;
247253
initial_uid_stamp = cached_result.uid_stamp;
248254
typedtree = cached_result.value;
249-
index = cached_result.index;
250255
cache_stat
251256
}
252257

@@ -285,7 +290,15 @@ let get_typedtree t =
285290
let sig_items, sig_type = split_items l in
286291
`Interface { Typedtree.sig_items; sig_type; sig_final_env = get_env t }
287292

288-
let get_index t = t.index
293+
let get_index t =
294+
let of_items items =
295+
List.last items
296+
|> Option.value_map ~default:Shape.Uid.Map.empty
297+
~f:(fun { part_index; _ } -> Lazy.force part_index)
298+
in
299+
match t.typedtree with
300+
| `Implementation items -> of_items items
301+
| `Interface items -> of_items items
289302

290303
let get_stamp t = t.stamp
291304

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

src/ocaml/preprocess/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
(modules parser_raw)
1111
(enabled_if (<> %{profile} "release"))
1212
(mode (promote (only parser_raw.ml parser_raw.mli)))
13-
(flags :standard --inspection --table --cmly))
13+
(flags :standard --inspection --table --cmly --lalr))
1414

1515
(rule
1616
(targets parser_recover.ml)

src/sherlodoc/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
(modules type_parser)
77
(enabled_if (<> %{profile} "release"))
88
(mode promote)
9-
(flags :standard --explain))
9+
(flags :standard --explain --lalr))
1010

1111
(ocamllex type_lexer)

0 commit comments

Comments
 (0)