From 32d8d1bfce54daed2f5329b3a281f43310ccca9b Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Sun, 15 Oct 2023 20:31:42 -0700 Subject: [PATCH] refactor: statically allocated lookup for version conversion (#193) * refactor: statically allocated lookup for version conversion * wip --- lib/response.ml | 1 - lib/versions.ml | 33 +++++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/response.ml b/lib/response.ml index 70fa357..9769b27 100644 --- a/lib/response.ml +++ b/lib/response.ml @@ -30,7 +30,6 @@ *---------------------------------------------------------------------------*) open Import -module Status = H2.Status type t = { (* `H2.Status.t` is a strict superset of `Httpaf.Status.t` *) diff --git a/lib/versions.ml b/lib/versions.ml index 1056e58..c5d8f77 100644 --- a/lib/versions.ml +++ b/lib/versions.ml @@ -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