Skip to content

Commit

Permalink
hpack: use String.unsafe_get when we know the string length (#244)
Browse files Browse the repository at this point in the history
* hpack: use String.unsafe_get when we know the string length

* changelog entry
  • Loading branch information
anmonteiro authored Jun 23, 2024
1 parent 4170477 commit b4c779c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Unreleased
- h2: drop the dependency on `httpaf`, use
[`httpun-types`](https://ocaml.org/p/httpun-types/latest) instead
([#243](https://github.com/anmonteiro/ocaml-h2/pull/243))
- hpack: use `String.unsafe_get` when the string length is known
([#244](https://github.com/anmonteiro/ocaml-h2/pull/244))

0.11.0 2023-10-26
--------------
Expand Down
31 changes: 18 additions & 13 deletions hpack/src/static_table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ let table =
let lookup_token_index name =
match String.length name with
| 3 ->
(match name.[0] with
(match String.unsafe_get name 0 with
| 'a' when name = "age" -> 20
| 'v' when name = "via" -> 59
| _ -> -1)
| 4 ->
(match name.[0] with
(match String.unsafe_get name 0 with
| 'd' when name = "date" -> 32
| 'e' when name = "etag" -> 33
| 'f' when name = "from" -> 36
Expand All @@ -170,13 +170,13 @@ let lookup_token_index name =
| 'v' when name = "vary" -> 58
| _ -> -1)
| 5 ->
(match name.[0] with
(match String.unsafe_get name 0 with
| ':' when name = ":path" -> 3
| 'a' when name = "allow" -> 21
| 'r' when name = "range" -> 49
| _ -> -1)
| 6 ->
(match name.[0] with
(match String.unsafe_get name 0 with
| 'a' when name = "accept" -> 18
| 'c' when name = "cookie" -> 31
| 'e' when name = "expect" -> 34
Expand All @@ -198,14 +198,17 @@ let lookup_token_index name =
| 'a' when name = "location" -> 45
| _ -> -1)
| 10 ->
(match name.[0] with
(match String.unsafe_get name 0 with
| ':' when name = ":authority" -> 0
| 's' when name = "set-cookie" -> 54
| 'u' when name = "user-agent" -> 57
| _ -> -1)
| 11 -> (match name.[0] with 'r' when name = "retry-after" -> 52 | _ -> -1)
| 11 ->
(match String.unsafe_get name 0 with
| 'r' when name = "retry-after" -> 52
| _ -> -1)
| 12 ->
(match name.[0] with
(match String.unsafe_get name 0 with
| 'c' when name = "content-type" -> 30
| 'm' when name = "max-forwards" -> 46
| _ -> -1)
Expand All @@ -219,7 +222,7 @@ let lookup_token_index name =
| 'o' when name = "last-modified" -> 43
| _ -> -1)
| 14 ->
(match name.[0] with
(match String.unsafe_get name 0 with
| 'a' when name = "accept-charset" -> 14
| 'c' when name = "content-length" -> 27
| _ -> -1)
Expand All @@ -236,24 +239,26 @@ let lookup_token_index name =
| 'i' when name = "www-authenticate" -> 60
| _ -> -1)
| 17 ->
(match name.[0] with
(match String.unsafe_get name 0 with
| 'i' when name = "if-modified-since" -> 39
| 't' when name = "transfer-encoding" -> 56
| _ -> -1)
| 18 ->
(match name.[0] with 'p' when name = "proxy-authenticate" -> 47 | _ -> -1)
(match String.unsafe_get name 0 with
| 'p' when name = "proxy-authenticate" -> 47
| _ -> -1)
| 19 ->
(match name.[0] with
(match String.unsafe_get name 0 with
| 'c' when name = "content-disposition" -> 24
| 'i' when name = "if-unmodified-since" -> 42
| 'p' when name = "proxy-authorization" -> 48
| _ -> -1)
| 25 ->
(match name.[0] with
(match String.unsafe_get name 0 with
| 's' when name = "strict-transport-security" -> 55
| _ -> -1)
| 27 ->
(match name.[0] with
(match String.unsafe_get name 0 with
| 'a' when name = "access-control-allow-origin" -> 19
| _ -> -1)
| _ -> -1
2 changes: 1 addition & 1 deletion hpack/test/hpack-test-case
Submodule hpack-test-case updated 65 files
+20 −0 LICENSE
+63 −0 swift-nio-hpack-huffman/story_00.json
+59 −0 swift-nio-hpack-huffman/story_01.json
+370 −0 swift-nio-hpack-huffman/story_02.json
+373 −0 swift-nio-hpack-huffman/story_03.json
+373 −0 swift-nio-hpack-huffman/story_04.json
+397 −0 swift-nio-hpack-huffman/story_05.json
+373 −0 swift-nio-hpack-huffman/story_06.json
+376 −0 swift-nio-hpack-huffman/story_07.json
+394 −0 swift-nio-hpack-huffman/story_08.json
+376 −0 swift-nio-hpack-huffman/story_09.json
+370 −0 swift-nio-hpack-huffman/story_10.json
+400 −0 swift-nio-hpack-huffman/story_11.json
+400 −0 swift-nio-hpack-huffman/story_12.json
+373 −0 swift-nio-hpack-huffman/story_13.json
+370 −0 swift-nio-hpack-huffman/story_14.json
+367 −0 swift-nio-hpack-huffman/story_15.json
+406 −0 swift-nio-hpack-huffman/story_16.json
+379 −0 swift-nio-hpack-huffman/story_17.json
+382 −0 swift-nio-hpack-huffman/story_18.json
+376 −0 swift-nio-hpack-huffman/story_19.json
+6,167 −0 swift-nio-hpack-huffman/story_20.json
+16,521 −0 swift-nio-hpack-huffman/story_21.json
+16,313 −0 swift-nio-hpack-huffman/story_22.json
+14,496 −0 swift-nio-hpack-huffman/story_23.json
+1,287 −0 swift-nio-hpack-huffman/story_24.json
+9,406 −0 swift-nio-hpack-huffman/story_25.json
+4,791 −0 swift-nio-hpack-huffman/story_26.json
+10,548 −0 swift-nio-hpack-huffman/story_27.json
+5,678 −0 swift-nio-hpack-huffman/story_28.json
+14,786 −0 swift-nio-hpack-huffman/story_29.json
+30,196 −0 swift-nio-hpack-huffman/story_30.json
+4,790 −0 swift-nio-hpack-huffman/story_31.json
+63 −0 swift-nio-hpack-plain-text/story_00.json
+59 −0 swift-nio-hpack-plain-text/story_01.json
+370 −0 swift-nio-hpack-plain-text/story_02.json
+373 −0 swift-nio-hpack-plain-text/story_03.json
+373 −0 swift-nio-hpack-plain-text/story_04.json
+397 −0 swift-nio-hpack-plain-text/story_05.json
+373 −0 swift-nio-hpack-plain-text/story_06.json
+376 −0 swift-nio-hpack-plain-text/story_07.json
+394 −0 swift-nio-hpack-plain-text/story_08.json
+376 −0 swift-nio-hpack-plain-text/story_09.json
+370 −0 swift-nio-hpack-plain-text/story_10.json
+400 −0 swift-nio-hpack-plain-text/story_11.json
+400 −0 swift-nio-hpack-plain-text/story_12.json
+373 −0 swift-nio-hpack-plain-text/story_13.json
+370 −0 swift-nio-hpack-plain-text/story_14.json
+367 −0 swift-nio-hpack-plain-text/story_15.json
+406 −0 swift-nio-hpack-plain-text/story_16.json
+379 −0 swift-nio-hpack-plain-text/story_17.json
+382 −0 swift-nio-hpack-plain-text/story_18.json
+376 −0 swift-nio-hpack-plain-text/story_19.json
+6,167 −0 swift-nio-hpack-plain-text/story_20.json
+16,521 −0 swift-nio-hpack-plain-text/story_21.json
+16,313 −0 swift-nio-hpack-plain-text/story_22.json
+14,496 −0 swift-nio-hpack-plain-text/story_23.json
+1,287 −0 swift-nio-hpack-plain-text/story_24.json
+9,406 −0 swift-nio-hpack-plain-text/story_25.json
+4,791 −0 swift-nio-hpack-plain-text/story_26.json
+10,548 −0 swift-nio-hpack-plain-text/story_27.json
+5,678 −0 swift-nio-hpack-plain-text/story_28.json
+14,786 −0 swift-nio-hpack-plain-text/story_29.json
+30,196 −0 swift-nio-hpack-plain-text/story_30.json
+4,790 −0 swift-nio-hpack-plain-text/story_31.json
9 changes: 5 additions & 4 deletions hpack/util/gen_static.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ let find_pos names =
loop 0

let make_token_map static_table =
let tbl = Hashtbl.create 60 in
let tbl = Hashtbl.create (Array.length static_table) in
Array.iter
(fun (i, name, _) ->
let length = String.length name in
Expand All @@ -96,11 +96,12 @@ let make_token_map static_table =
| Some string_tbl -> string_tbl
| None -> Hashtbl.create 10
in
Hashtbl.add string_tbl name i)
add_name name i string_tbl;
Hashtbl.replace tbl length string_tbl)
static_table;
Hashtbl.fold
(fun length names ret ->
let bindings = Hashtbl.fold (fun k v lst -> (k, v) :: lst) names [] in
let bindings = Hashtbl.to_seq names |> List.of_seq in
(length, find_pos names, bindings) :: ret)
tbl
[]
Expand Down Expand Up @@ -147,7 +148,7 @@ let mk_lookup_token token_map =
(Exp.match_
(Exp.apply
(Exp.ident
{ txt = Ldot (Lident "String", "get")
{ txt = Ldot (Lident "String", "unsafe_get")
; loc = !default_loc
})
[ ( Nolabel
Expand Down

0 comments on commit b4c779c

Please sign in to comment.