Skip to content

Commit d45ddfa

Browse files
authored
feat(graindoc): Cache module comments (#2102)
1 parent 6e5b432 commit d45ddfa

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

compiler/graindoc/docblock.re

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -319,23 +319,27 @@ let lookup_type_expr = (~idx, type_exprs) => {
319319
Option.bind(type_exprs, te => List.nth_opt(te, idx));
320320
};
321321

322+
let saved_comments = Hashtbl.create(64);
323+
322324
let get_comments_from_loc = (loc: Grain_parsing.Location.t) => {
323325
open Compile;
324326

325-
let comments =
326-
switch (
327-
compile_file(
328-
~is_root_file=true,
329-
~hook=stop_after_parse,
330-
loc.loc_start.pos_fname,
331-
)
332-
) {
333-
| exception exn => []
334-
| {cstate_desc: Parsed(parsed_program)} => parsed_program.comments
335-
| _ => failwith("Invalid compilation state")
336-
};
327+
let file = loc.loc_start.pos_fname;
337328

338-
Comments.to_ordered(comments);
329+
switch (Hashtbl.find_opt(saved_comments, file)) {
330+
| Some(comments) => comments
331+
| None =>
332+
let comments =
333+
switch (compile_file(~is_root_file=true, ~hook=stop_after_parse, file)) {
334+
| exception exn => []
335+
| {cstate_desc: Parsed(parsed_program)} => parsed_program.comments
336+
| _ => failwith("Invalid compilation state")
337+
};
338+
339+
let ordered = Comments.to_ordered(comments);
340+
Hashtbl.add(saved_comments, file, ordered);
341+
ordered;
342+
};
339343
};
340344

341345
let attr_name = attr => {

0 commit comments

Comments
 (0)