From 8511856ba3121aad62902ccc9571b7976146198f Mon Sep 17 00:00:00 2001 From: Tuomas Hietanen Date: Thu, 30 Nov 2023 13:24:08 +0000 Subject: [PATCH] Reduced some dictionary double-lookups (#378) --- src/Saturn/Common.fs | 6 +++--- src/Saturn/Controller.fs | 7 +++---- src/Saturn/ControllerEndpoint.fs | 7 +++---- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/Saturn/Common.fs b/src/Saturn/Common.fs index e6894d71..a29b4228 100644 --- a/src/Saturn/Common.fs +++ b/src/Saturn/Common.fs @@ -37,9 +37,9 @@ module Common = let private getSavedSubPath (ctx : HttpContext) = let inline strOption (str : string) = if String.IsNullOrEmpty str then None else Some str - if ctx.Items.ContainsKey RouteKey - then ctx.Items.Item RouteKey |> string |> strOption - else None + match ctx.Items.TryGetValue RouteKey with + | true, route -> route |> string |> strOption + | false, _ -> None let private handlerWithRootedPath (path : string) (handler : HttpHandler) : HttpHandler = fun (next : HttpFunc) (ctx : HttpContext) -> diff --git a/src/Saturn/Controller.fs b/src/Saturn/Controller.fs index 8e50d8aa..c3322cd6 100644 --- a/src/Saturn/Controller.fs +++ b/src/Saturn/Controller.fs @@ -183,10 +183,9 @@ module Controller = member __.Plug(state, actions, handler) = let addPlug state action handler = let newplugs = - if state.Plugs.ContainsKey action then - state.Plugs.Add(action, (handler::state.Plugs.[action])) - else - state.Plugs.Add(action,[handler]) + match state.Plugs.TryGetValue action with + | true, acts -> state.Plugs.Add(action, (handler::acts)) + | false, _ -> state.Plugs.Add(action,[handler]) {state with Plugs = newplugs} if actions |> List.contains All then diff --git a/src/Saturn/ControllerEndpoint.fs b/src/Saturn/ControllerEndpoint.fs index 510ff1c0..9f785813 100644 --- a/src/Saturn/ControllerEndpoint.fs +++ b/src/Saturn/ControllerEndpoint.fs @@ -183,10 +183,9 @@ module Controller = member __.Plug(state, actions, handler) = let addPlug state action handler = let newplugs = - if state.Plugs.ContainsKey action then - state.Plugs.Add(action, (handler::state.Plugs.[action])) - else - state.Plugs.Add(action,[handler]) + match state.Plugs.TryGetValue action with + | true, acts -> state.Plugs.Add(action, (handler::acts)) + | false, _ -> state.Plugs.Add(action,[handler]) {state with Plugs = newplugs} if actions |> List.contains All then