Skip to content

Commit

Permalink
Opimtize key tree stemming by using maps instead of sets
Browse files Browse the repository at this point in the history
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}
```
  • Loading branch information
nickva committed Mar 21, 2022
1 parent c9a95b0 commit 91de482
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/couch/src/couch_key_tree.erl
Expand Up @@ -496,7 +496,7 @@ stem(Trees, Limit) ->
{NewSeen, NewBranches} = stem_tree(Tree, Limit, Seen),
{NewSeen, NewBranches ++ TreeAcc}
end,
{sets:new(), []},
{#{}, []},
Trees
),
lists:sort(Branches)
Expand Down Expand Up @@ -553,11 +553,11 @@ stem_tree(Depth, {Key, Val, Children}, Limit, Seen0) ->
end.

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.

repair_tree(Trees, Limit) ->
Expand Down

0 comments on commit 91de482

Please sign in to comment.