From 664ec4d5e5687b5d88d21551be487944325b5084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Gilbert?= Date: Fri, 15 Sep 2023 13:51:08 +0200 Subject: [PATCH] Changed NewProfile.MiniJson.t to polymorphic variants matching yojson --- lib/newProfile.ml | 37 +++++++++++++++++++------------------ lib/newProfile.mli | 12 +++++++----- toplevel/vernac.ml | 4 ++-- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/lib/newProfile.ml b/lib/newProfile.ml index 4c3b05adba55..87c11db2b06b 100644 --- a/lib/newProfile.ml +++ b/lib/newProfile.ml @@ -27,21 +27,22 @@ let f fmt = match !accu with | File ch -> Printf.fprintf ch fmt module MiniJson = struct - type t = - | String of string - | Int of string (* string not int so that we can have large ints *) - | Record of (string * t) list - | Array of t list - - let rec pr ch = function - | String s -> + type t =[ + | `Intlit of string + | `String of string + | `Assoc of (string * t) list + | `List of t list + ] + + let rec pr ch : t -> _ = function + | `String s -> let s = String.split_on_char '"' s in let s = String.concat "\\\"" s in Printf.fprintf ch "\"%s\"" s - | Int s -> Printf.fprintf ch "%s" s - | Record elts -> + | `Intlit s -> Printf.fprintf ch "%s" s + | `Assoc elts -> Printf.fprintf ch "{ %a }" prrecord elts - | Array elts -> + | `List elts -> Printf.fprintf ch "[\n%a\n]" prarray elts and prrecord ch = function @@ -56,15 +57,15 @@ module MiniJson = struct let pids = string_of_int pid - let base = [("pid", Int pids); ("tid", Int pids)] + let base = [("pid", `Intlit pids); ("tid", `Intlit pids)] let duration ~name ~ph ~ts ?args () = - let l = ("name", String name) :: ("ph", String ph) :: ("ts", Int ts) :: base in + let l = ("name", `String name) :: ("ph", `String ph) :: ("ts", `Intlit ts) :: base in let l = match args with | None -> l - | Some args -> ("args", Record args) :: l + | Some args -> ("args", `Assoc args) :: l in - Record l + `Assoc l end let gettime = Unix.gettimeofday @@ -117,11 +118,11 @@ let leave ?time name ?(args=[]) ?last () = let time = gettimeopt time in let sum, dur = leave_sums ~time name () in let sum = List.map (fun (name, (t, cnt)) -> - name, MiniJson.String + name, `String (Printf.sprintf "%.3G us, %d %s" (t *. 1E6) cnt (CString.plural cnt "call"))) (CString.Map.bindings sum) in - let args = ("subtimes", MiniJson.Record sum) :: args in + let args = ("subtimes", `Assoc sum) :: args in duration ~time name "E" ~args ?last () let make_mem_diff ~(mstart:Gc.stat) ~(mend:Gc.stat) = @@ -130,7 +131,7 @@ let make_mem_diff ~(mstart:Gc.stat) ~(mend:Gc.stat) = in the same span vs how much survived it *) let major = mend.major_words -. mstart.major_words in let minor = mend.minor_words -. mstart.minor_words in - let pp tdiff = MiniJson.String (Printf.sprintf "%.3G w" tdiff) in + let pp tdiff = `String (Printf.sprintf "%.3G w" tdiff) in [("major",pp major); ("minor", pp minor)] (* NB: "process" and "init" are unconditional because they don't go diff --git a/lib/newProfile.mli b/lib/newProfile.mli index 28e2c07548a4..18bd57675eaf 100644 --- a/lib/newProfile.mli +++ b/lib/newProfile.mli @@ -9,11 +9,13 @@ (************************************************************************) module MiniJson : sig - type t = - | String of string - | Int of string (* string not int so that we can have large ints *) - | Record of (string * t) list - | Array of t list + (** Subtype of Yojson.Safe.t *) + type t = [ + | `Intlit of string + | `String of string + | `Assoc of (string * t) list + | `List of t list + ] end val profile : string -> ?args:(unit -> (string * MiniJson.t) list) -> (unit -> 'a) -> unit -> 'a diff --git a/toplevel/vernac.ml b/toplevel/vernac.ml index c224c360bd32..49de403e0e50 100644 --- a/toplevel/vernac.ml +++ b/toplevel/vernac.ml @@ -128,8 +128,8 @@ let load_vernac_core ~echo ~check ~state ?source file = | None -> "unknown" | Some loc -> string_of_int loc.line_nb in - [("cmd", String (Pp.string_of_ppcmds (Topfmt.pr_cmd_header ast))); - ("line", String lnum)]) + [("cmd", `String (Pp.string_of_ppcmds (Topfmt.pr_cmd_header ast))); + ("line", `String lnum)]) (fun () -> Flags.silently (interp_vernac ~check ~state) ast) ()) ()