File 2583-Optimise-functions-in-the-maps-module.patch of Package erlang

From c52f42bbaf16c02143ad2a840c5a4f9101235943 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20Muska=C5=82a?= <michal@muskala.eu>
Date: Thu, 19 Jul 2018 19:33:14 +0200
Subject: [PATCH 1/2] Optimise functions in the maps module

Using direct pattern matching on the map is more effient than pattern
matching on the result of maps:find/2, because it avoids allocating the
intermediate tuple.
---
 lib/stdlib/src/maps.erl | 40 +++++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 23 deletions(-)

diff --git a/lib/stdlib/src/maps.erl b/lib/stdlib/src/maps.erl
index 60463feec2..12302ce839 100644
--- a/lib/stdlib/src/maps.erl
+++ b/lib/stdlib/src/maps.erl
@@ -149,11 +149,9 @@ values(_) -> erlang:nif_error(undef).
       Fun :: fun((Value1 :: term()) -> Value2 :: term()).
 
 update_with(Key,Fun,Map) when is_function(Fun,1), is_map(Map) ->
-    try maps:get(Key,Map) of
-        Val -> maps:update(Key,Fun(Val),Map)
-    catch
-        error:{badkey,_} ->
-            erlang:error({badkey,Key},[Key,Fun,Map])
+    case Map of
+        #{Key := Value} -> Map#{Key := Fun(Value)};
+        #{} -> erlang:error({badkey,Key},[Key,Fun,Map])
     end;
 update_with(Key,Fun,Map) ->
     erlang:error(error_type(Map),[Key,Fun,Map]).
@@ -167,9 +165,9 @@ update_with(Key,Fun,Map) ->
       Init :: term().
 
 update_with(Key,Fun,Init,Map) when is_function(Fun,1), is_map(Map) ->
-    case maps:find(Key,Map) of
-        {ok,Val} -> maps:update(Key,Fun(Val),Map);
-        error -> maps:put(Key,Init,Map)
+    case Map of
+        #{Key := Value} -> Map#{Key := Fun(Value)};
+        #{} -> Map#{Key => Init}
     end;
 update_with(Key,Fun,Init,Map) ->
     erlang:error(error_type(Map),[Key,Fun,Init,Map]).
@@ -182,11 +180,9 @@ update_with(Key,Fun,Init,Map) ->
         Default :: term().
 
 get(Key,Map,Default) when is_map(Map) ->
-    case maps:find(Key, Map) of
-        {ok, Value} ->
-            Value;
-        error ->
-            Default
+    case Map of
+        #{Key := Value} -> Value;
+        #{} -> Default
     end;
 get(Key,Map,Default) ->
     erlang:error({badmap,Map},[Key,Map,Default]).
@@ -250,7 +246,7 @@ next(Iter) ->
     K :: term().
 
 without(Ks,M) when is_list(Ks), is_map(M) ->
-    lists:foldl(fun(K, M1) -> ?MODULE:remove(K, M1) end, M, Ks);
+    lists:foldl(fun ?MODULE:remove/2, M, Ks);
 without(Ks,M) ->
     erlang:error(error_type(M),[Ks,M]).
 
@@ -262,18 +258,16 @@ without(Ks,M) ->
     K :: term().
 
 with(Ks,Map1) when is_list(Ks), is_map(Map1) ->
-    Fun = fun(K, List) ->
-      case ?MODULE:find(K, Map1) of
-          {ok, V} ->
-              [{K, V} | List];
-          error ->
-              List
-      end
-    end,
-    ?MODULE:from_list(lists:foldl(Fun, [], Ks));
+    ?MODULE:from_list(with_1(Ks, Map1));
 with(Ks,M) ->
     erlang:error(error_type(M),[Ks,M]).
 
+with_1([K|Ks], Map) ->
+    case Map of
+        #{K := V} -> [{K,V}|with_1(Ks, Map)];
+        #{} -> with_1(Ks, Map)
+    end;
+with_1([], _Map) -> [].
 
 error_type(M) when is_map(M) -> badarg;
 error_type(V) -> {badmap, V}.
-- 
2.18.0

openSUSE Build Service is sponsored by