You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ro...@apache.org on 2015/02/03 16:13:31 UTC

[24/50] [abbrv] couchdb-mango git commit: Avoid regex in the common case

Avoid regex in the common case

Escaped characters in field names are highly unusual. We can quickly
test for their presence and if none are found do the path splitting
using the more efficient binary module instead of regular expressions. A
microbenchmark shows this approach to be a little more than twice as
fast as the original.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/ba40953a
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/ba40953a
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/ba40953a

Branch: refs/heads/master
Commit: ba40953a2d3053d4dbd92320da6105b3d4f8f1d8
Parents: 072d3f2
Author: Adam Kocoloski <ad...@cloudant.com>
Authored: Thu Dec 11 21:09:33 2014 -0500
Committer: Adam Kocoloski <ad...@cloudant.com>
Committed: Thu Dec 11 21:09:33 2014 -0500

----------------------------------------------------------------------
 src/mango_doc.erl | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/ba40953a/src/mango_doc.erl
----------------------------------------------------------------------
diff --git a/src/mango_doc.erl b/src/mango_doc.erl
index 666ef9b..a2de4b2 100644
--- a/src/mango_doc.erl
+++ b/src/mango_doc.erl
@@ -525,6 +525,15 @@ set_elem(I, [Item | Rest], Value) when I > 1 ->
     [Item | set_elem(I-1, Rest, Value)].
 
 parse_field(Field) ->
+    case binary:match(Field, <<"\\">>, []) of
+        nomatch ->
+            % Fast path, no regex required
+            {ok, check_non_empty(Field, binary:split(Field, <<".">>, [global]))};
+        _ ->
+            parse_field_slow(Field)
+    end.
+
+parse_field_slow(Field) ->
     Path = lists:map(fun
         (P) when P =:= <<>> ->
             ?MANGO_ERROR({invalid_field_name, Field});
@@ -533,6 +542,14 @@ parse_field(Field) ->
     end, re:split(Field, <<"(?<!\\\\)\\.">>)),
     {ok, Path}.
 
+check_non_empty(Field, Parts) ->
+    case lists:member(<<>>, Parts) of
+        true ->
+            ?MANGO_ERROR({invalid_field_name, Field});
+        false ->
+            Parts
+    end.
+
 -ifdef(TEST).
 -include_lib("eunit/include/eunit.hrl").