Parcourir la source

Fixed buffered reading in file loading step

Taddeus Kroes il y a 12 ans
Parent
commit
a5d2525c5f
2 fichiers modifiés avec 23 ajouts et 18 suppressions
  1. 1 1
      main.ml
  2. 22 17
      phases/load.ml

+ 1 - 1
main.ml

@@ -16,8 +16,8 @@ let compile args =
         Print.phase;
         Desug.phase;
         Print.phase;
-        (*
         Context_analysis.phase;
+        (*
         Print.phase;
         Typecheck.phase;
         Extern_vars.phase;

+ 22 - 17
phases/load.ml

@@ -1,4 +1,3 @@
-open Printf
 open Ast
 
 let input_all ic =
@@ -8,6 +7,19 @@ let input_all ic =
     close_in ic;
     buf
 
+let input_buffered ic chunksize =
+    let rec read_all buf bufsize pos =
+        match input ic buf pos (bufsize - pos) with
+        | 0 -> (close_in ic; buf)
+        | nread when nread = bufsize - pos ->
+            let bufsize = bufsize + chunksize in
+            let pos = pos + nread in
+            read_all (buf ^ String.create chunksize) bufsize pos
+        | nread ->
+            read_all buf bufsize (pos + nread)
+    in
+    read_all (String.create chunksize) chunksize 0
+
 let phase ir =
     prerr_endline "- Load input file";
     match ir with
@@ -16,36 +28,29 @@ let phase ir =
             | Some filename -> filename
             | None -> "<stdin>"
         in
+        let bufsize = 512 in
 
         if args.cpp then
-            let _ = prerr_endline "- Run C preprocessor" in
-
             let cpp_out = match args.filename with
                 | Some filename ->
                     Unix.open_process_in ("cpp " ^ filename)
                 | None ->
-                    let content = input_all stdin in
+                    let content = input_buffered stdin bufsize in
                     let (cpp_out, cpp_in) = Unix.open_process "cpp" in
                     output_string cpp_in content;
                     close_out cpp_in;
                     cpp_out
             in
 
+            let _ = prerr_endline "- Run C preprocessor" in
+
             (* Read preprocessed code from cpp's stdout *)
-            let bufsize = 1024 in
-            let newbuf () = String.create bufsize in
-            let rec read_all buf pos =
-                let nread = input cpp_out buf pos bufsize in
-                if nread = 0
-                    then (close_in cpp_out; buf)
-                    else read_all (String.concat "" [buf; newbuf ()]) nread
-            in
-            let preprocessed = read_all (newbuf ()) 0 in
+            let preprocessed = input_buffered cpp_out bufsize in
             FileContent (display_name, preprocessed, args)
         else
-            let infile = match args.filename with
-                | Some filename -> open_in filename
-                | None -> stdin
+            let content = match args.filename with
+                | Some filename -> input_all (open_in filename)
+                | None -> input_buffered stdin bufsize
             in
-            FileContent (display_name, input_all infile, args)
+            FileContent (display_name, content, args)
     | _ -> raise (InvalidInput "load")