aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorBjörn Gustavsson <[email protected]>2014-01-09 12:01:46 +0100
committerBjörn Gustavsson <[email protected]>2014-01-09 12:01:46 +0100
commit039ea3a6ed5dc147478f294910ba042850db2383 (patch)
treef357cf926092f1a111957c2a885598c148a8ffed /lib
parent31ecc95ef55de3e112fd0b5134366fccf742a92d (diff)
parent5a7b2115ca5b9c23aacf79b634133fea172a61fd (diff)
downloadotp-039ea3a6ed5dc147478f294910ba042850db2383.tar.gz
otp-039ea3a6ed5dc147478f294910ba042850db2383.tar.bz2
otp-039ea3a6ed5dc147478f294910ba042850db2383.zip
Merge branch 'sv/faster-orddict-from_list/OTP-11552'
* sv/faster-orddict-from_list/OTP-11552: improve performance for orddict:from_list/1
Diffstat (limited to 'lib')
-rw-r--r--lib/stdlib/src/orddict.erl8
1 files changed, 7 insertions, 1 deletions
diff --git a/lib/stdlib/src/orddict.erl b/lib/stdlib/src/orddict.erl
index da60fc1bb6..c98d78b34d 100644
--- a/lib/stdlib/src/orddict.erl
+++ b/lib/stdlib/src/orddict.erl
@@ -56,8 +56,10 @@ to_list(Dict) -> Dict.
List :: [{Key :: term(), Value :: term()}],
Orddict :: orddict().
+from_list([]) -> [];
+from_list([{_,_}]=Pair) -> Pair;
from_list(Pairs) ->
- lists:foldl(fun ({K,V}, D) -> store(K, V, D) end, [], Pairs).
+ lists:ukeysort(1, reverse_pairs(Pairs, [])).
-spec size(Orddict) -> non_neg_integer() when
Orddict :: orddict().
@@ -235,3 +237,7 @@ merge(F, [{K1,V1}|D1], [{_K2,V2}|D2]) -> %K1 == K2
[{K1,F(K1, V1, V2)}|merge(F, D1, D2)];
merge(F, [], D2) when is_function(F, 3) -> D2;
merge(F, D1, []) when is_function(F, 3) -> D1.
+
+reverse_pairs([{_,_}=H|T], Acc) ->
+ reverse_pairs(T, [H|Acc]);
+reverse_pairs([], Acc) -> Acc.