From 78746c1195c709882e6008b74483fa0cc9ffdcc6 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 9 Aug 2021 09:29:57 -0700 Subject: [PATCH] Avoid Dict to List conversions to improve performance. --- src/DataSource.elm | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/DataSource.elm b/src/DataSource.elm index 5b9e7ea6..7f64ab70 100644 --- a/src/DataSource.elm +++ b/src/DataSource.elm @@ -481,16 +481,19 @@ It would not work correctly if it chose between two responses that were reduced -} combineReducedDicts : Dict String WhatToDo -> Dict String WhatToDo -> Dict String WhatToDo combineReducedDicts dict1 dict2 = - (Dict.toList dict1 ++ Dict.toList dict2) - |> fromListDedupe Pages.StaticHttpRequest.merge + if Dict.size dict1 > Dict.size dict2 then + uniqueInsertAll dict2 dict1 + + else + uniqueInsertAll dict1 dict2 -fromListDedupe : (comparable -> a -> a -> a) -> List ( comparable, a ) -> Dict comparable a -fromListDedupe combineFn xs = - List.foldl - (\( key, value ) acc -> Dict.Extra.insertDedupe (combineFn key) key value acc) - Dict.empty - xs +uniqueInsertAll : Dict String WhatToDo -> Dict String WhatToDo -> Dict String WhatToDo +uniqueInsertAll dictToDedupeMerge startingDict = + Dict.foldl + (\key value acc -> Dict.Extra.insertDedupe (Pages.StaticHttpRequest.merge key) key value acc) + startingDict + dictToDedupeMerge lookup : KeepOrDiscard -> ApplicationType -> DataSource value -> RequestsAndPending -> Result Pages.StaticHttpRequest.Error ( Dict String WhatToDo, value )