Skip to content

Commit 89aa9bd

Browse files
authored
Merge pull request #248 from ppedrot/leaner-symbol-table
Store symbol tables in a more compact way for compiled programs.
2 parents af3bb10 + eec36ec commit 89aa9bd

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/compiler.ml

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,23 @@ module Symbols : sig
7171
val show : D.State.t -> D.constant -> string
7272

7373
type table
74+
type pruned_table
7475
val pp_table : Format.formatter -> table -> unit
76+
val pp_pruned_table : Format.formatter -> pruned_table -> unit
7577
val table : table D.State.component
7678
val compile_table : table -> D.symbol_table
7779
val lock : table -> table
7880
val locked : table -> bool
7981
val equal : table -> table -> bool
80-
val size : table -> int
81-
val prune : table -> alive:D.Constants.Set.t -> table
82+
val size : pruned_table -> int
83+
val prune : table -> alive:D.Constants.Set.t -> pruned_table
8284
(* debug *)
83-
val symbols : table -> string list
85+
val symbols : pruned_table -> string list
8486

8587
val global_table : unit -> table
8688
val uuid : table -> UUID.t
8789

88-
val build_shift : ?lock_base:bool -> flags:flags -> base:D.State.t -> table -> (D.State.t * D.constant D.Constants.Map.t, string) Stdlib.Result.t
90+
val build_shift : ?lock_base:bool -> flags:flags -> base:D.State.t -> pruned_table -> (D.State.t * D.constant D.Constants.Map.t, string) Stdlib.Result.t
8991

9092
end = struct
9193

@@ -103,22 +105,26 @@ type table = {
103105
uuid : Util.UUID.t;
104106
} [@@deriving show]
105107

108+
type pruned_table = {
109+
c2s0 : string D.Constants.Map.t;
110+
c2t0 : D.term D.Constants.Map.t;
111+
} [@@deriving show]
112+
106113
let locked { locked } = locked
107114
let lock t = { t with locked = true }
108115
let uuid { uuid } = uuid
109116
let equal t1 t2 =
110117
locked t1 && locked t2 && uuid t1 = uuid t2
111118

112-
let size t = D.Constants.Map.cardinal t.c2t
119+
let size t = D.Constants.Map.cardinal t.c2t0
113120

114-
let symbols { c2s } =
115-
List.map (fun (c,s) -> s ^ ":" ^ string_of_int c) (D.Constants.Map.bindings c2s)
121+
let symbols { c2s0 } =
122+
List.map (fun (c,s) -> s ^ ":" ^ string_of_int c) (D.Constants.Map.bindings c2s0)
116123

117124
let prune t ~alive =
118-
{ t with
119-
c2s = D.Constants.Map.filter (fun k _ -> D.Constants.Set.mem k alive) t.c2s;
120-
c2t = D.Constants.Map.filter (fun k _ -> D.Constants.Set.mem k alive) t.c2t;
121-
ast2ct = F.Map.filter (fun _ (k,_) -> D.Constants.Set.mem k alive) t.ast2ct;
125+
{
126+
c2s0 = D.Constants.Map.filter (fun k _ -> D.Constants.Set.mem k alive) t.c2s;
127+
c2t0 = D.Constants.Map.filter (fun k _ -> D.Constants.Set.mem k alive) t.c2t;
122128
}
123129

124130
let table = D.State.declare
@@ -243,7 +249,7 @@ let build_shift ?(lock_base=false) ~flags:{ print_units } ~base symbols =
243249
heuristic in unfolding) *)
244250
List.fold_left (fun (base,shift as acc) (v, t) ->
245251
if v < 0 then
246-
let name = Map.find v symbols.c2s in
252+
let name = Map.find v symbols.c2s0 in
247253
try
248254
let c, _ = F.Map.find (F.from_string name) base.ast2ct in
249255
if c == v then acc
@@ -262,7 +268,7 @@ let build_shift ?(lock_base=false) ~flags:{ print_units } ~base symbols =
262268
let base = { base with c2t = Map.add v t base.c2t } in
263269
base, shift
264270
)
265-
(base,Map.empty) (List.rev (Map.bindings symbols.c2t)))
271+
(base,Map.empty) (List.rev (Map.bindings symbols.c2t0)))
266272

267273
let build_shift ?lock_base ~flags ~base symbols =
268274
try Stdlib.Result.Ok (build_shift ?lock_base ~flags ~base symbols)
@@ -516,7 +522,7 @@ let empty = {
516522
end
517523

518524
type compilation_unit = {
519-
symbol_table : Symbols.table;
525+
symbol_table : Symbols.pruned_table;
520526
version : string;
521527
code : Flat.program;
522528
}

0 commit comments

Comments
 (0)