diff options
author | ILYA Khlopotov <iilyak@apache.org> | 2017-06-07 09:59:30 -0700 |
---|---|---|
committer | ILYA Khlopotov <iilyak@apache.org> | 2017-06-07 10:49:53 -0700 |
commit | db7e9efdf8e74431af8816d4fa3965ff8cde5a7c (patch) | |
tree | 727fba5d41a229f01e2ccab2949cae803c48087e | |
parent | 93707fc08ad42792719a319af233d422a5f1aebd (diff) | |
download | couchdb-db7e9efdf8e74431af8816d4fa3965ff8cde5a7c.tar.gz |
Avoid using length to detect non empty list
length(Values) is O(n) operation. Which could get expensive for long
lists. Change the code to rely on pattern matching to detect non empty
lists.
-rw-r--r-- | src/mango/src/mango_selector.erl | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/src/mango/src/mango_selector.erl b/src/mango/src/mango_selector.erl index 13e7d883b..bcf347201 100644 --- a/src/mango/src/mango_selector.erl +++ b/src/mango/src/mango_selector.erl @@ -186,7 +186,7 @@ norm_ops({[{Field, Cond}]}) -> {[{Field, norm_ops(Cond)}]}; % An implicit $and -norm_ops({Props}) when length(Props) > 1 -> +norm_ops({[_, _ | _] = Props}) -> {[{<<"$and">>, [norm_ops({[P]}) || P <- Props]}]}; % A bare value condition means equality @@ -461,7 +461,7 @@ match({[{<<"$elemMatch">>, _Arg}]}, _Value, _Cmp) -> % Matches when all elements in values match the % sub-selector Arg. -match({[{<<"$allMatch">>, Arg}]}, Values, Cmp) when is_list(Values), length(Values) > 0 -> +match({[{<<"$allMatch">>, Arg}]}, [_ | _] = Values, Cmp) -> try lists:foreach(fun(V) -> case match(Arg, V, Cmp) of @@ -564,5 +564,5 @@ match({[{Field, Cond}]}, Value, Cmp) -> match(Cond, SubValue, Cmp) end; -match({Props} = Sel, _Value, _Cmp) when length(Props) > 1 -> +match({[_, _ | _] = _Props} = Sel, _Value, _Cmp) -> erlang:error({unnormalized_selector, Sel}). |