Skip to content

Commit

Permalink
add StringDynArray
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Jan 28, 2024
1 parent fe395ef commit de07f71
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 59 deletions.
72 changes: 13 additions & 59 deletions src/compiler/hxb/hxbWriter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,52 +102,6 @@ let dump_stats name stats =
print_endline (Printf.sprintf " %s: %i - %i" name imin imax)
) chunk_sizes *)

module StringHashtbl = Hashtbl.Make(struct
type t = string

let equal =
String.equal

let hash s =
(* What's the best here? *)
Hashtbl.hash s
end)

module StringPool = struct
type t = {
lut : int StringHashtbl.t;
items : string DynArray.t;
mutable closed : bool;
}

let create () = {
lut = StringHashtbl.create 16;
items = DynArray.create ();
closed = false;
}

let add sp s =
assert (not sp.closed);
let index = DynArray.length sp.items in
StringHashtbl.add sp.lut s index;
DynArray.add sp.items s;
index

let get sp s =
StringHashtbl.find sp.lut s

let get_or_add sp s =
try
get sp s
with Not_found ->
add sp s

let finalize sp =
assert (not sp.closed);
sp.closed <- true;
DynArray.to_list sp.items,DynArray.length sp.items
end

module Pool = struct
type ('key,'value) t = {
lut : ('key,int) Hashtbl.t;
Expand Down Expand Up @@ -1800,11 +1754,11 @@ module HxbWriter = struct
write_type_parameters writer ltp
end;
Chunk.write_option writer.chunk fctx.texpr_this (fun e -> write_type_instance writer e.etype);
let items,length = StringPool.finalize fctx.t_pool in
Chunk.write_uleb128 writer.chunk length;
List.iter (fun bytes ->
let a = StringPool.finalize fctx.t_pool in
Chunk.write_uleb128 writer.chunk a.length;
StringDynArray.iter a (fun bytes ->
Chunk.write_bytes writer.chunk (Bytes.unsafe_of_string bytes)
) items;
);
Chunk.write_uleb128 writer.chunk (DynArray.length fctx.vars);
DynArray.iter (fun (v,v_id) ->
v.v_id <- v_id;
Expand Down Expand Up @@ -2255,22 +2209,22 @@ module HxbWriter = struct
start_chunk writer EOF;
start_chunk writer EOM;

let finalize_string_pool kind items length =
let finalize_string_pool kind a =
start_chunk writer kind;
Chunk.write_uleb128 writer.chunk length;
List.iter (fun s ->
Chunk.write_uleb128 writer.chunk a.StringDynArray.length;
StringDynArray.iter a (fun s ->
let b = Bytes.unsafe_of_string s in
Chunk.write_bytes_length_prefixed writer.chunk b;
) items
)
in
begin
let items,length = StringPool.finalize writer.cp in
finalize_string_pool STR items length
let a = StringPool.finalize writer.cp in
finalize_string_pool STR a
end;
begin
let items,length = StringPool.finalize writer.docs in
if length > 0 then
finalize_string_pool DOC items length
let a = StringPool.finalize writer.docs in
if a.length > 0 then
finalize_string_pool DOC a
end

let get_sorted_chunks writer =
Expand Down
27 changes: 27 additions & 0 deletions src/core/ds/stringDynArray.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type t = {
mutable arr : string array;
mutable length : int;
}

let create length = {
arr = Array.make length "";
length = 0;
}

let length d =
d.length

let add d s =
let length = Array.length d.arr in
if d.length = length then begin
let new_arr = Array.make (length * 2) "" in
Array.blit d.arr 0 new_arr 0 length;
d.arr <- new_arr;
end;
d.arr.(d.length) <- s;
d.length <- d.length + 1

let iter d f =
for i = 0 to d.length - 1 do
f (Array.unsafe_get d.arr i)
done
43 changes: 43 additions & 0 deletions src/core/ds/stringPool.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module StringHashtbl = Hashtbl.Make(struct
type t = string

let equal =
String.equal

let hash s =
(* What's the best here? *)
Hashtbl.hash s
end)

type t = {
lut : int StringHashtbl.t;
items : StringDynArray.t;
mutable closed : bool;
}

let create () = {
lut = StringHashtbl.create 16;
items = StringDynArray.create 16;
closed = false;
}

let add sp s =
assert (not sp.closed);
let index = StringDynArray.length sp.items in
StringHashtbl.add sp.lut s index;
StringDynArray.add sp.items s;
index

let get sp s =
StringHashtbl.find sp.lut s

let get_or_add sp s =
try
get sp s
with Not_found ->
add sp s

let finalize sp =
assert (not sp.closed);
sp.closed <- true;
sp.items

0 comments on commit de07f71

Please sign in to comment.