Skip to content

Commit

Permalink
refactor: statically allocated lookup for version conversion (#193)
Browse files Browse the repository at this point in the history
* refactor: statically allocated lookup for version conversion

* wip
  • Loading branch information
anmonteiro authored Oct 16, 2023
1 parent 9867aed commit 32d8d1b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
1 change: 0 additions & 1 deletion lib/response.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
*---------------------------------------------------------------------------*)

open Import
module Status = H2.Status

type t =
{ (* `H2.Status.t` is a strict superset of `Httpaf.Status.t` *)
Expand Down
33 changes: 17 additions & 16 deletions lib/versions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,29 @@ module HTTP = struct
let pp fmt version = Format.fprintf fmt "%s" (to_string version)

module Raw = struct
type v = t

include Httpaf.Version

let equal v1 v2 = compare v1 v2 = 0
let v1_0 = { major = 1; minor = 0 }
let v1_1 = { major = 1; minor = 1 }
let v2_0 = { major = 2; minor = 0 }
let equal v1 v2 = compare v1 v2 = 0

let to_version = function
| { major = 1; minor = 0 } -> Some HTTP_1_0
| { major = 1; minor = 1 } -> Some HTTP_1_1
| { major = 2; minor = 0 } -> Some HTTP_2
| _ -> None

let to_version_exn version =
match to_version version with
| Some x -> x
| None -> raise (Failure "Versions.ALPN.of_version_exn")

let of_version = function
| HTTP_1_0 -> v1_0
| HTTP_1_1 -> v1_1
| HTTP_2 -> v2_0
(* indexing: version_map[major][minor] *)
let version_map = [| [||]; [| HTTP_1_0; HTTP_1_1 |]; [| HTTP_2 |] |]
let[@inline] to_version_exn t = version_map.(t.major).(t.minor)

let to_version t =
match to_version_exn t with
| version -> Some version
| exception Invalid_argument _ -> None

let rev_version_map = [| v1_0; v1_1; v2_0 |]

let of_version (v : v) =
let idx : int = Obj.magic v in
rev_version_map.(idx)
end
end

Expand Down

0 comments on commit 32d8d1b

Please sign in to comment.