From d7a5cca1d4556d85d44f1b9b792944f0d8b997e7 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 15 Apr 2025 10:40:37 -0400 Subject: [PATCH] feat(headers): `set` will not reallocate whole list if not needed --- src/core/headers.ml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/core/headers.ml b/src/core/headers.ml index 0bbe76df..eb140b66 100644 --- a/src/core/headers.ml +++ b/src/core/headers.ml @@ -35,7 +35,15 @@ let get ?(f = fun x -> x) x h = try Some (get_exn ~f x h) with Not_found -> None let remove x h = List.filter (fun (k, _) -> not (equal_name_ k x)) h -let set x y h = (x, y) :: List.filter (fun (k, _) -> not (equal_name_ k x)) h + +let set x y h = + let h = + if contains x h then + remove x h + else + h + in + (x, y) :: h let pp out l = let pp_pair out (k, v) = Format.fprintf out "@[%s: %s@]" k v in