[m-rev.] [diff 4/4] Add caching of line information to mfilterjavac

Paul Bone paul at bone.id.au
Thu Apr 18 22:55:42 AEST 2013


Add caching of line information to mfilterjavac.

util/mfilterjavac.m:
    As above.
---
 mfilterjavac/mfilterjavac.m | 56 ++++++++++++++++++++++++++++++---------------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/mfilterjavac/mfilterjavac.m b/mfilterjavac/mfilterjavac.m
index 6aecc07..acbcd30 100644
--- a/mfilterjavac/mfilterjavac.m
+++ b/mfilterjavac/mfilterjavac.m
@@ -39,6 +39,7 @@
 :- import_module char.
 :- import_module int.
 :- import_module list.
+:- import_module map.
 :- import_module maybe.
 :- import_module require.
 :- import_module string.
@@ -46,7 +47,7 @@
 %-----------------------------------------------------------------------------%
 
 main(!IO) :-
-    filter_lines(MaybeError, !IO),
+    filter_lines(MaybeError, map.init, _, !IO),
     (
         MaybeError = ok
     ;
@@ -55,17 +56,18 @@ main(!IO) :-
         io.set_exit_status(1, !IO)
     ).
 
-:- pred filter_lines(maybe_error::out, io::di, io::uo) is det.
+:- pred filter_lines(maybe_error::out,
+    line_info_cache::in, line_info_cache::out, io::di, io::uo) is det.
 
-filter_lines(MaybeError, !IO) :-
+filter_lines(MaybeError, !Cache, !IO) :-
     io.read_line_as_string(Result, !IO),
     (
         Result = ok(Line),
-        filter_line(Line, MaybeOutLine, !IO),
+        filter_line(Line, MaybeOutLine, !Cache, !IO),
         (
             MaybeOutLine = ok(OutLine),
             io.write_string(OutLine, !IO),
-            filter_lines(MaybeError, !IO)
+            filter_lines(MaybeError, !Cache, !IO)
         ;
             MaybeOutLine = error(Error),
             MaybeError = error(Error)
@@ -79,34 +81,48 @@ filter_lines(MaybeError, !IO) :-
         MaybeError = error(ErrorStr)
     ).
 
-:- pred filter_line(string::in, maybe_error(string)::out, io::di, io::uo)
-    is det.
+:- pred filter_line(string::in, maybe_error(string)::out,
+    line_info_cache::in, line_info_cache::out, io::di, io::uo) is det.
 
-filter_line(Line, MaybeOutLine, !IO) :-
+filter_line(Line, MaybeOutLine, !Cache, !IO) :-
     (
         PartsA = split_at_separator(char.is_whitespace, Line),
         PartsA = [PartAA | OtherPartsA],
         PartsAA = split_at_char(':', PartAA),
         PartsAA = [Filename, LineStr, Empty],
-        string.to_int(LineStr, LineNo)
+        string.to_int(LineStr, LineNo),
+        Empty = ""
     ->
-        maybe_get_line_info(Filename, MaybeLineInfo, !IO),
-        (
-            MaybeLineInfo = ok(LineInfo),
-            line_info_translate(LineInfo, Filename, LineNo,
-                MerFileName, MerLineNo),
-            Rest = string.join_list(" ", OtherPartsA),
-            OutLine = string.format("%s:%d:%s %s\n",
-                [s(MerFileName), i(MerLineNo), s(Empty), s(Rest)]),
+        ( map.search(!.Cache, Filename, LineInfo) ->
+            translate_and_outpot_line(LineInfo, Filename, LineNo,
+                OtherPartsA, OutLine),
             MaybeOutLine = ok(OutLine)
         ;
-            MaybeLineInfo = error(Error),
-            MaybeOutLine = error(Error)
+            maybe_get_line_info(Filename, MaybeLineInfoErr, !IO),
+            (
+                MaybeLineInfoErr = ok(LineInfo),
+                map.det_insert(Filename, LineInfo, !Cache),
+                translate_and_outpot_line(LineInfo, Filename, LineNo,
+                    OtherPartsA, OutLine),
+                MaybeOutLine = ok(OutLine)
+            ;
+                MaybeLineInfoErr = error(Error),
+                MaybeOutLine = error(Error)
+            )
         )
     ;
         MaybeOutLine = ok(Line)
     ).
 
+:- pred translate_and_outpot_line(list(line_info)::in, string::in, int::in,
+    list(string)::in, string::out) is det.
+
+translate_and_outpot_line(LineInfo, Filename, LineNo, RestParts, OutLine) :-
+    line_info_translate(LineInfo, Filename, LineNo, MerFileName, MerLineNo),
+    Rest = string.join_list(" ", RestParts),
+    OutLine = string.format("%s:%d: %s\n",
+        [s(MerFileName), i(MerLineNo), s(Rest)]).
+
 %-----------------------------------------------------------------------------%
 
 :- type line_info
@@ -129,6 +145,8 @@ filter_line(Line, MaybeOutLine, !IO) :-
     ;       lie_beginning_without_end
     ;       lie_duplicate_beginning.
 
+:- type line_info_cache == map(string, list(line_info)).
+
 :- pred line_info_translate(list(line_info)::in, string::in, int::in,
     string::out, int::out) is det.
 
-- 
1.8.1.3




More information about the reviews mailing list