[PATCH v4 12/14] box: tune tuple_field_raw_by_path for indexed data

Kirill Shcherbatov kshcherbatov at tarantool.org
Thu Oct 11 10:58:44 MSK 2018


We don't need to parse tuple in tuple_field_raw_by_path if
required field has been indexed. We do path lookup in field
tree of JSON paths and return data by it's offset from field_map
instead of whole tuple parsing.

Part of #1012.
---
 src/box/tuple_format.c     | 30 +++++++++++++++++++++---------
 test/engine/tuple.result   |  5 +++++
 test/engine/tuple.test.lua |  2 ++
 3 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/src/box/tuple_format.c b/src/box/tuple_format.c
index 30deac5..4738eb9 100644
--- a/src/box/tuple_format.c
+++ b/src/box/tuple_format.c
@@ -981,15 +981,12 @@ tuple_field_raw_by_path(struct tuple_format *format, const char *tuple,
 		goto error;
 	switch(node.type) {
 	case JSON_PATH_NUM: {
-		int index = node.num;
-		if (index == 0) {
+		fieldno = node.num;
+		if (fieldno == 0) {
 			*field = NULL;
 			return 0;
 		}
-		index -= TUPLE_INDEX_BASE;
-		*field = tuple_field_raw(format, tuple, field_map, index);
-		if (*field == NULL)
-			return 0;
+		fieldno -= TUPLE_INDEX_BASE;
 		break;
 	}
 	case JSON_PATH_STR: {
@@ -1006,9 +1003,8 @@ tuple_field_raw_by_path(struct tuple_format *format, const char *tuple,
 			 */
 			name_hash = field_name_hash(node.str, node.len);
 		}
-		*field = tuple_field_raw_by_name(format, tuple, field_map,
-						 node.str, node.len, name_hash);
-		if (*field == NULL)
+		if (tuple_fieldno_by_name(format->dict, node.str, node.len,
+					  name_hash, &fieldno) != 0)
 			return 0;
 		break;
 	}
@@ -1017,6 +1013,22 @@ tuple_field_raw_by_path(struct tuple_format *format, const char *tuple,
 		*field = NULL;
 		return 0;
 	}
+	/* Optimize indexed JSON field data access. */
+	struct tuple_field *indexed_field =
+		unlikely(fieldno >= format->field_count) ? NULL :
+		tuple_field_tree_lookup(&format->fields[fieldno],
+					path + parser.offset,
+					path_len - parser.offset);
+	if (indexed_field != NULL &&
+	    indexed_field->offset_slot != TUPLE_OFFSET_SLOT_NIL) {
+		*field = tuple + field_map[indexed_field->offset_slot];
+		return 0;
+	}
+
+	/* No such field in index. Continue parsing JSON path. */
+	*field = tuple_field_raw(format, tuple, field_map, fieldno);
+	if (*field == NULL)
+		return 0;
 	rc = tuple_field_by_relative_path(field, path + parser.offset,
 					  path_len - parser.offset);
 	if (rc == 0)
diff --git a/test/engine/tuple.result b/test/engine/tuple.result
index e551f1a..1842420 100644
--- a/test/engine/tuple.result
+++ b/test/engine/tuple.result
@@ -1142,6 +1142,11 @@ assert(idx2 ~= nil)
 t = s:insert{5, 7, {town = 'Matrix', FIO = {fname = 'Agent', sname = 'Smith'}}, 4, 5}
 ---
 ...
+-- Test field_map in tuple speed-up access by indexed path.
+t["[3][\"FIO\"][\"fname\"]"]
+---
+- Agent
+...
 idx:select()
 ---
 - - [5, 7, {'town': 'Matrix', 'FIO': {'fname': 'Agent', 'sname': 'Smith'}}, 4, 5]
diff --git a/test/engine/tuple.test.lua b/test/engine/tuple.test.lua
index e865c67..8be6505 100644
--- a/test/engine/tuple.test.lua
+++ b/test/engine/tuple.test.lua
@@ -366,6 +366,8 @@ s:create_index('test2', {parts = {{2, 'number'}, {3, 'number', path = '["FIO"]["
 idx2 = s:create_index('test2', {parts = {{2, 'number'}, {3, 'str', path = '["FIO"]["fname"]'}}})
 assert(idx2 ~= nil)
 t = s:insert{5, 7, {town = 'Matrix', FIO = {fname = 'Agent', sname = 'Smith'}}, 4, 5}
+-- Test field_map in tuple speed-up access by indexed path.
+t["[3][\"FIO\"][\"fname\"]"]
 idx:select()
 idx:min()
 idx:max()
-- 
2.7.4




More information about the Tarantool-patches mailing list