[PATCH v7 3/5] box: tune tuple_field_raw_by_path for indexed data

Kirill Shcherbatov kshcherbatov at tarantool.org
Wed Jan 9 11:29:38 MSK 2019


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.h           | 19 -------------------
 src/box/tuple_format.c    | 23 +++++++++++------------
 src/box/tuple_format.h    | 24 ------------------------
 test/engine/json.result   |  5 +++++
 test/engine/json.test.lua |  2 ++
 5 files changed, 18 insertions(+), 55 deletions(-)

diff --git a/src/box/tuple.h b/src/box/tuple.h
index 83e5b7013..dac467d00 100644
--- a/src/box/tuple.h
+++ b/src/box/tuple.h
@@ -547,25 +547,6 @@ tuple_field_by_path(const struct tuple *tuple, const char *path,
 	                               path_hash, field);
 }
 
-/**
- * Get tuple field by its name.
- * @param tuple Tuple to get field from.
- * @param name Field name.
- * @param name_len Length of @a name.
- * @param name_hash Hash of @a name.
- *
- * @retval not NULL MessagePack field.
- * @retval     NULL No field with @a name.
- */
-static inline const char *
-tuple_field_by_name(const struct tuple *tuple, const char *name,
-		    uint32_t name_len, uint32_t name_hash)
-{
-	return tuple_field_raw_by_name(tuple_format(tuple), tuple_data(tuple),
-				       tuple_field_map(tuple), name, name_len,
-				       name_hash);
-}
-
 /**
  * @brief Tuple Interator
  */
diff --git a/src/box/tuple_format.c b/src/box/tuple_format.c
index c81c23fd1..401752654 100644
--- a/src/box/tuple_format.c
+++ b/src/box/tuple_format.c
@@ -1073,10 +1073,7 @@ tuple_field_raw_by_path(struct tuple_format *format, const char *tuple,
 		goto error;
 	switch(token.type) {
 	case JSON_TOKEN_NUM: {
-		int index = token.num;
-		*field = tuple_field_raw(format, tuple, field_map, index);
-		if (*field == NULL)
-			return 0;
+		fieldno = token.num;
 		break;
 	}
 	case JSON_TOKEN_STR: {
@@ -1093,10 +1090,8 @@ tuple_field_raw_by_path(struct tuple_format *format, const char *tuple,
 			 */
 			name_hash = field_name_hash(token.str, token.len);
 		}
-		*field = tuple_field_raw_by_name(format, tuple, field_map,
-						 token.str, token.len,
-						 name_hash);
-		if (*field == NULL)
+		if (tuple_fieldno_by_name(format->dict, token.str, token.len,
+					  name_hash, &fieldno) != 0)
 			return 0;
 		break;
 	}
@@ -1105,13 +1100,17 @@ tuple_field_raw_by_path(struct tuple_format *format, const char *tuple,
 		*field = NULL;
 		return 0;
 	}
-	rc = tuple_field_go_to_path(field, path + lexer.offset,
-				    path_len - lexer.offset);
+	/* Optimize indexed JSON field data access. */
+	struct key_part part;
+	part.fieldno = fieldno;
+	part.path = (char *)path + lexer.offset;
+	part.path_len = path_len - lexer.offset;
+	rc = tuple_field_by_part_raw_slowpath(format, tuple, field_map, &part,
+					      field);
 	if (rc == 0)
 		return 0;
 	/* Setup absolute error position. */
 	rc += lexer.offset;
-
 error:
 	assert(rc > 0);
 	diag_set(ClientError, ER_ILLEGAL_PARAMS,
@@ -1128,7 +1127,7 @@ tuple_field_by_part_raw_slowpath(struct tuple_format *format, const char *data,
 	struct tuple_field *field =
 		tuple_format_field_by_path(format, part->fieldno, part->path,
 					   part->path_len);
-	if (field != NULL) {
+	if (field != NULL && field->offset_slot != TUPLE_OFFSET_SLOT_NIL) {
 		int32_t offset_slot = field->offset_slot;
 		assert(-offset_slot * sizeof(uint32_t) <=
 		       format->field_map_size);
diff --git a/src/box/tuple_format.h b/src/box/tuple_format.h
index 3b630c3bb..dd7cd147a 100644
--- a/src/box/tuple_format.h
+++ b/src/box/tuple_format.h
@@ -437,30 +437,6 @@ tuple_field_raw(struct tuple_format *format, const char *tuple,
 	return tuple;
 }
 
-/**
- * Get tuple field by its name.
- * @param format Tuple format.
- * @param tuple MessagePack tuple's body.
- * @param field_map Tuple field map.
- * @param name Field name.
- * @param name_len Length of @a name.
- * @param name_hash Hash of @a name.
- *
- * @retval not NULL MessagePack field.
- * @retval     NULL No field with @a name.
- */
-static inline const char *
-tuple_field_raw_by_name(struct tuple_format *format, const char *tuple,
-			const uint32_t *field_map, const char *name,
-			uint32_t name_len, uint32_t name_hash)
-{
-	uint32_t fieldno;
-	if (tuple_fieldno_by_name(format->dict, name, name_len, name_hash,
-				  &fieldno) != 0)
-		return NULL;
-	return tuple_field_raw(format, tuple, field_map, fieldno);
-}
-
 /**
  * Retrieve msgpack data by JSON path.
  * @param data Pointer to msgpack with data.
diff --git a/test/engine/json.result b/test/engine/json.result
index 711f7f256..ef3440a2d 100644
--- a/test/engine/json.result
+++ b/test/engine/json.result
@@ -214,6 +214,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/json.test.lua b/test/engine/json.test.lua
index 2a20fc3e5..7f2aed790 100644
--- a/test/engine/json.test.lua
+++ b/test/engine/json.test.lua
@@ -59,6 +59,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.19.2




More information about the Tarantool-patches mailing list