Skip to content

Commit

Permalink
Changed NewProfile.MiniJson.t to polymorphic variants matching yojson
Browse files Browse the repository at this point in the history
  • Loading branch information
SkySkimmer committed Sep 15, 2023
1 parent 695d1f7 commit 664ec4d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
37 changes: 19 additions & 18 deletions lib/newProfile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) =
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions lib/newProfile.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions toplevel/vernac.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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) ())
()
Expand Down

0 comments on commit 664ec4d

Please sign in to comment.