-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Follow up to #213 Decided to inline the relevant parts of it to the test suite as it highlighted so many problems. One of which I don't know how to test. Moreover, with it highlighting so many problems it might highlight them again later. Also removed some dead code from said benchmark and added attribution to @michalmuskala
- Loading branch information
Showing
3 changed files
with
80 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Original code by Michal Muskala | ||
# https://gist.github.com/michalmuskala/5ff53581b4b53adec2fff7fb4d69fd52 | ||
defmodule BenchKeyword do | ||
@compile :inline_list_funcs | ||
|
||
@moduledoc """ | ||
Together with the benchmark illustrated multiple problems in the memory measurement code. | ||
""" | ||
|
||
def delete_v0(keywords, key) when is_list(keywords) and is_atom(key) do | ||
:lists.filter(fn {k, _} -> k != key end, keywords) | ||
end | ||
|
||
def delete_v2(keywords, key) when is_list(keywords) and is_atom(key) do | ||
delete_v2_key(keywords, key, []) | ||
end | ||
|
||
defp delete_v2_key([{key, _} | tail], key, heads) do | ||
delete_v2_key(tail, key, heads) | ||
end | ||
|
||
defp delete_v2_key([{_, _} = pair | tail], key, heads) do | ||
delete_v2_key(tail, key, [pair | heads]) | ||
end | ||
|
||
defp delete_v2_key([], _key, heads) do | ||
:lists.reverse(heads) | ||
end | ||
|
||
def delete_v3(keywords, key) when is_list(keywords) and is_atom(key) do | ||
case :lists.keymember(key, 1, keywords) do | ||
true -> delete_v3_key(keywords, key, []) | ||
_ -> keywords | ||
end | ||
end | ||
|
||
defp delete_v3_key([{key, _} | tail], key, heads) do | ||
delete_v3_key(tail, key, heads) | ||
end | ||
|
||
defp delete_v3_key([{_, _} = pair | tail], key, heads) do | ||
delete_v3_key(tail, key, [pair | heads]) | ||
end | ||
|
||
defp delete_v3_key([], _key, heads) do | ||
:lists.reverse(heads) | ||
end | ||
end |