Skip to content

Commit

Permalink
Merge pull request #66 from CraigFe/allocation-microoptimisations
Browse files Browse the repository at this point in the history
Add memory microoptimisations
  • Loading branch information
craigfe authored Jun 11, 2021
2 parents 46bf94e + cdd180e commit 5a05905
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
35 changes: 24 additions & 11 deletions src/repr/type_binary.ml
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,30 @@ module Encode = struct

let bytes boxed = if boxed then boxed_bytes else unboxed_bytes

let list l n =
let l = unstage l in
stage (fun x k ->
len n (List.length x) k;
List.iter (fun e -> l e k) x)

let array l n =
let l = unstage l in
stage (fun x k ->
len n (Array.length x) k;
Array.iter (fun e -> l e k) x)
let list =
let rec encode_elements encode_elt k = function
| [] -> ()
| x :: xs ->
encode_elt x k;
(encode_elements [@tailcall]) encode_elt k xs
in
fun l n ->
let l = unstage l in
stage (fun x k ->
len n (List.length x) k;
encode_elements l k x)

let array =
let encode_elements encode_elt k arr =
for i = 0 to Array.length arr - 1 do
encode_elt (Array.unsafe_get arr i) k
done
in
fun l n ->
let l = unstage l in
stage (fun x k ->
len n (Array.length x) k;
encode_elements l k x)

let pair a b =
let a = unstage a and b = unstage b in
Expand Down
7 changes: 3 additions & 4 deletions src/repr/type_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ let fold_variant :
| _ -> assert false)
| CV1 ({ ctag1; cwit1; _ }, v) -> (
match cases.(ctag1) with
| Dispatch.Arrow { f; arg_wit } -> (
match Witness.cast cwit1 arg_wit v with
| Some v -> unstage f v
| None -> assert false)
| Dispatch.Arrow { f; arg_wit } ->
let v = Witness.cast_exn cwit1 arg_wit v in
unstage f v
| _ -> assert false))
11 changes: 11 additions & 0 deletions src/repr/witness.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,16 @@ let make : type a. unit -> a t =
let eq : type a b. a t -> b t -> (a, b) eq option =
fun (module A) (module B) -> match A.Eq with B.Eq -> Some Refl | _ -> None

let eq_exn : type a b. a t -> b t -> (a, b) eq =
fun (module A) (module B) ->
match A.Eq with
| B.Eq -> Refl
| _ -> failwith "Repr.internal_error: unexpected runtime type inequality"

let cast : type a b. a t -> b t -> a -> b option =
fun awit bwit a -> match eq awit bwit with Some Refl -> Some a | None -> None

let cast_exn : type a b. a t -> b t -> a -> b =
fun awit bwit a ->
let Refl = eq_exn awit bwit in
a
2 changes: 2 additions & 0 deletions src/repr/witness.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ type 'a t

val make : unit -> 'a t
val eq : 'a t -> 'b t -> ('a, 'b) eq option
val eq_exn : 'a t -> 'b t -> ('a, 'b) eq
val cast : 'a t -> 'b t -> 'a -> 'b option
val cast_exn : 'a t -> 'b t -> 'a -> 'b

0 comments on commit 5a05905

Please sign in to comment.