This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch 3.2.x
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 2e4be0460b4f00361dd0c96534916f3f8ef2e794
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Sat Apr 2 01:31:19 2022 -0400

    Optimize key tree stemming by using maps instead of sets
    
    sets to due to a compiler bug in 22 consumes too much memory and is
    slower on Erlang 22+
    
    Reproducer: https://gist.github.com/nickva/ddc499e6e05678faf20d344c6e11daaa
    
    With sets:
    ```
    couch_key_tree:gen_and_stem().
    {8,6848.812683105469}
    ```
    
    With maps:
    ```
    couch_key_tree:gen_and_stem().
    {0,544.000732421875}
    ```
    
    This is a cherry-pick of 
https://github.com/apache/couchdb/commit/91de482fd66f4773b3b8583039c6bcaf1c5727ec
---
 src/couch/src/couch_key_tree.erl | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/couch/src/couch_key_tree.erl b/src/couch/src/couch_key_tree.erl
index 94150418e..7811d9532 100644
--- a/src/couch/src/couch_key_tree.erl
+++ b/src/couch/src/couch_key_tree.erl
@@ -471,7 +471,7 @@ stem(Trees, Limit) ->
         {_, Branches} = lists:foldl(fun(Tree, {Seen, TreeAcc}) ->
             {NewSeen, NewBranches} = stem_tree(Tree, Limit, Seen),
             {NewSeen, NewBranches ++ TreeAcc}
-        end, {sets:new(), []}, Trees),
+        end, {#{}, []}, Trees),
         lists:sort(Branches)
     catch throw:dupe_keys ->
         repair_tree(Trees, Limit)
@@ -522,11 +522,11 @@ stem_tree(Depth, {Key, Val, Children}, Limit, Seen0) ->
 
 
 check_key(Key, Seen) ->
-    case sets:is_element(Key, Seen) of
-        true ->
+    case Seen of
+        #{Key := true} ->
             throw(dupe_keys);
-        false ->
-            sets:add_element(Key, Seen)
+        _ ->
+            Seen#{Key => true}
     end.
 
 

Reply via email to