[njs] Moving long_string fields of njs_value_t into a separate struct.

Dmitry Volyntsev xeioex at nginx.com
Tue Feb 20 16:19:28 UTC 2018


details:   http://hg.nginx.org/njs/rev/0a3645d22d22
branches:  
changeset: 447:0a3645d22d22
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Feb 20 19:12:52 2018 +0300
description:
Moving long_string fields of njs_value_t into a separate struct.

diffstat:

 njs/njs_object.c |   4 +-
 njs/njs_string.c |  83 ++++++++++++++++++++++++++++---------------------------
 njs/njs_vm.c     |  34 ++++++++++++----------
 njs/njs_vm.h     |  63 +++++++++++++++++++++++------------------
 4 files changed, 97 insertions(+), 87 deletions(-)

diffs (447 lines):

diff -r e12ed0068e57 -r 0a3645d22d22 njs/njs_object.c
--- a/njs/njs_object.c	Mon Feb 12 15:15:53 2018 +0300
+++ b/njs/njs_object.c	Tue Feb 20 19:12:52 2018 +0300
@@ -159,11 +159,11 @@ njs_object_hash_test(nxt_lvlhsh_query_t 
         start = prop->name.short_string.start;
 
     } else {
-        if (lhq->key.length != prop->name.data.string_size) {
+        if (lhq->key.length != prop->name.long_string.size) {
             return NXT_DECLINED;
         }
 
-        start = prop->name.data.u.string->start;
+        start = prop->name.long_string.data->start;
     }
 
     if (memcmp(start, lhq->key.start, lhq->key.length) == 0) {
diff -r e12ed0068e57 -r 0a3645d22d22 njs/njs_string.c
--- a/njs/njs_string.c	Mon Feb 12 15:15:53 2018 +0300
+++ b/njs/njs_string.c	Tue Feb 20 19:12:52 2018 +0300
@@ -151,15 +151,15 @@ njs_string_create(njs_vm_t *vm, njs_valu
          */
         value->short_string.size = NJS_STRING_LONG;
         value->short_string.length = 0;
-        value->data.external0 = 0xff;
-        value->data.string_size = size;
+        value->long_string.external = 0xff;
+        value->long_string.size = size;
 
         string = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_string_t));
         if (nxt_slow_path(string == NULL)) {
             return NXT_ERROR;
         }
 
-        value->data.u.string = string;
+        value->long_string.data = string;
 
         string->start = start;
         string->length = length;
@@ -210,8 +210,8 @@ njs_string_alloc(njs_vm_t *vm, njs_value
      */
     value->short_string.size = NJS_STRING_LONG;
     value->short_string.length = 0;
-    value->data.external0 = 0;
-    value->data.string_size = size;
+    value->long_string.external = 0;
+    value->long_string.size = size;
 
     if (size != length && length > NJS_STRING_MAP_STRIDE) {
         map_offset = njs_string_map_offset(size);
@@ -226,7 +226,7 @@ njs_string_alloc(njs_vm_t *vm, njs_value
                                  sizeof(njs_string_t) + total);
 
     if (nxt_fast_path(string != NULL)) {
-        value->data.u.string = string;
+        value->long_string.data = string;
 
         string->start = (u_char *) string + sizeof(njs_string_t);
         string->length = length;
@@ -284,9 +284,9 @@ njs_string_validate(njs_vm_t *vm, njs_st
         }
 
     } else {
-        string->start = value->data.u.string->start;
-        size = value->data.string_size;
-        length = value->data.u.string->length;
+        string->start = value->long_string.data->start;
+        size = value->long_string.size;
+        length = value->long_string.data->length;
 
         if (length == 0 && length != size) {
             length = nxt_utf8_length(string->start, size);
@@ -312,14 +312,14 @@ njs_string_validate(njs_vm_t *vm, njs_st
 
                     memcpy(start, string->start, size);
                     string->start = start;
-                    value->data.u.string->start = start;
+                    value->long_string.data->start = start;
 
                     map = (uint32_t *) (start + map_offset);
                     map[0] = 0;
                 }
             }
 
-            value->data.u.string->length = length;
+            value->long_string.data->length = length;
         }
     }
 
@@ -343,9 +343,9 @@ njs_string_prop(njs_string_prop_t *strin
         length = value->short_string.length;
 
     } else {
-        string->start = value->data.u.string->start;
-        size = value->data.string_size;
-        length = value->data.u.string->length;
+        string->start = value->long_string.data->start;
+        size = value->long_string.size;
+        length = value->long_string.data->length;
     }
 
     string->size = size;
@@ -454,8 +454,8 @@ njs_string_prototype_length(njs_vm_t *vm
         length = value->short_string.length;
 
         if (size == NJS_STRING_LONG) {
-            size = value->data.string_size;
-            length = value->data.u.string->length;
+            size = value->long_string.size;
+            length = value->long_string.data->length;
         }
 
         length = (length == 0) ? size : length;
@@ -486,14 +486,14 @@ njs_string_eq(const njs_value_t *v1, con
         start2 = v2->short_string.start;
 
     } else {
-        size = v1->data.string_size;
-
-        if (size != v2->data.string_size) {
+        size = v1->long_string.size;
+
+        if (size != v2->long_string.size) {
             return 0;
         }
 
-        start1 = v1->data.u.string->start;
-        start2 = v2->data.u.string->start;
+        start1 = v1->long_string.data->start;
+        start2 = v2->long_string.data->start;
     }
 
     return (memcmp(start1, start2, size) == 0);
@@ -513,8 +513,8 @@ njs_string_cmp(const njs_value_t *v1, co
         start1 = v1->short_string.start;
 
     } else {
-        size1 = v1->data.string_size;
-        start1 = v1->data.u.string->start;
+        size1 = v1->long_string.size;
+        start1 = v1->long_string.data->start;
     }
 
     size2 = v2->short_string.size;
@@ -523,8 +523,8 @@ njs_string_cmp(const njs_value_t *v1, co
         start2 = v2->short_string.start;
 
     } else {
-        size2 = v2->data.string_size;
-        start2 = v2->data.u.string->start;
+        size2 = v2->long_string.size;
+        start2 = v2->long_string.data->start;
     }
 
     size = nxt_min(size1, size2);
@@ -2903,8 +2903,8 @@ njs_string_replacement_copy(njs_string_r
         string->start = NULL;
 
     } else {
-        string->start = value->data.u.string->start;
-        size = value->data.string_size;
+        string->start = value->long_string.data->start;
+        size = value->long_string.size;
     }
 
     string->size = size;
@@ -2965,8 +2965,8 @@ njs_string_to_number(njs_value_t *value,
         p = value->short_string.start;
 
     } else {
-        size = value->data.string_size;
-        p = value->data.u.string->start;
+        size = value->long_string.size;
+        p = value->long_string.data->start;
     }
 
     end = p + size;
@@ -3044,8 +3044,8 @@ njs_string_to_index(njs_value_t *value)
         p = value->short_string.start;
 
     } else {
-        size = value->data.string_size;
-        p = value->data.u.string->start;
+        size = value->long_string.size;
+        p = value->long_string.data->start;
     }
 
     if (size == 0) {
@@ -3091,8 +3091,8 @@ njs_string_to_c_string(njs_vm_t *vm, njs
         }
 
     } else {
-        start = value->data.u.string->start;
-        size = value->data.string_size;
+        start = value->long_string.data->start;
+        size = value->long_string.size;
 
         if (start[size] == '\0') {
             return start;
@@ -3643,7 +3643,7 @@ njs_string_decode(njs_vm_t *vm, njs_valu
             vm->retval.short_string.length = length;
 
         } else {
-            vm->retval.data.u.string->length = length;
+            vm->retval.long_string.data->length = length;
         }
     }
 
@@ -3671,8 +3671,9 @@ njs_values_hash_test(nxt_lvlhsh_query_t 
     }
 
     if (njs_is_string(value)
-        && value->data.string_size == lhq->key.length
-        && memcmp(value->data.u.string->start, lhq->key.start, lhq->key.length)
+        && value->long_string.size == lhq->key.length
+        && memcmp(value->long_string.data->start, lhq->key.start,
+                  lhq->key.length)
            == 0)
     {
         return NXT_OK;
@@ -3714,8 +3715,8 @@ njs_value_index(njs_vm_t *vm, njs_parser
         start = (u_char *) src;
 
     } else {
-        size = src->data.string_size;
-        start = src->data.u.string->start;
+        size = src->long_string.size;
+        start = src->long_string.data->start;
     }
 
     lhq.key_hash = nxt_djb_hash(start, size);
@@ -3738,7 +3739,7 @@ njs_value_index(njs_vm_t *vm, njs_parser
             /* Long string value is allocated together with string. */
             value_size = sizeof(njs_value_t) + sizeof(njs_string_t);
 
-            length = src->data.u.string->length;
+            length = src->long_string.data->length;
 
             if (size != length && length > NJS_STRING_MAP_STRIDE) {
                 size = njs_string_map_offset(size)
@@ -3756,10 +3757,10 @@ njs_value_index(njs_vm_t *vm, njs_parser
 
         if (start != (u_char *) src) {
             string = (njs_string_t *) ((u_char *) value + sizeof(njs_value_t));
-            value->data.u.string = string;
+            value->long_string.data = string;
 
             string->start = (u_char *) string + sizeof(njs_string_t);
-            string->length = src->data.u.string->length;
+            string->length = src->long_string.data->length;
             string->retain = 0xffff;
 
             memcpy(string->start, start, size);
diff -r e12ed0068e57 -r 0a3645d22d22 njs/njs_vm.c
--- a/njs/njs_vm.c	Mon Feb 12 15:15:53 2018 +0300
+++ b/njs/njs_vm.c	Tue Feb 20 19:12:52 2018 +0300
@@ -303,11 +303,11 @@ njs_value_retain(njs_value_t *value)
 
     if (njs_is_string(value)) {
 
-        if (value->data.external0 != 0xff) {
-            string = value->data.u.string;
+        if (value->long_string.external != 0xff) {
+            string = value->long_string.data;
 
             nxt_thread_log_debug("retain:%uxD \"%*s\"", string->retain,
-                                 value->data.string_size, string->start);
+                                 value->long_string.size, string->start);
 
             if (string->retain != 0xffff) {
                 string->retain++;
@@ -324,11 +324,11 @@ njs_value_release(njs_vm_t *vm, njs_valu
 
     if (njs_is_string(value)) {
 
-        if (value->data.external0 != 0xff) {
-            string = value->data.u.string;
+        if (value->long_string.external != 0xff) {
+            string = value->long_string.data;
 
             nxt_thread_log_debug("release:%uxD \"%*s\"", string->retain,
-                                 value->data.string_size, string->start);
+                                 value->long_string.size, string->start);
 
             if (string->retain != 0xffff) {
                 string->retain--;
@@ -2132,18 +2132,20 @@ njs_values_strict_equal(const njs_value_
             start2 = val2->short_string.start;
 
         } else {
-            size = val1->data.string_size;
-
-            if (size != val2->data.string_size) {
+            size = val1->long_string.size;
+
+            if (size != val2->long_string.size) {
                 return 0;
             }
 
-            if (val1->data.u.string->length != val2->data.u.string->length) {
+            if (val1->long_string.data->length
+                != val2->long_string.data->length)
+            {
                 return 0;
             }
 
-            start1 = val1->data.u.string->start;
-            start2 = val2->data.u.string->start;
+            start1 = val1->long_string.data->start;
+            start2 = val2->long_string.data->start;
         }
 
         return (memcmp(start1, start2, size) == 0);
@@ -3466,8 +3468,8 @@ again:
                 memcpy(start, value.short_string.start, size);
 
             } else {
-                size = value.data.string_size;
-                start = value.data.u.string->start;
+                size = value.long_string.size;
+                start = value.long_string.data->start;
             }
 
             dst->length = size;
@@ -3784,8 +3786,8 @@ njs_debug(njs_index_t index, njs_value_t
             p = value->short_string.start;
 
         } else {
-            length = value->data.string_size;
-            p = value->data.u.string->start;
+            length = value->long_string.size;
+            p = value->long_string.data->start;
         }
 
         nxt_thread_log_debug("%p [\"%*s\"]", index, length, p);
diff -r e12ed0068e57 -r 0a3645d22d22 njs/njs_vm.h
--- a/njs/njs_vm.h	Mon Feb 12 15:15:53 2018 +0300
+++ b/njs/njs_vm.h	Tue Feb 20 19:12:52 2018 +0300
@@ -143,8 +143,8 @@ union njs_value_s {
      * to provide 4 bits to encode scope in njs_index_t.  This space is
      * used to store short strings.  The maximum size of a short string
      * is 14 (NJS_STRING_SHORT).  If the short_string.size field is 15
-     * (NJS_STRING_LONG) then the size is in the data.string_size field
-     * and the data.u.string field points to a long string.
+     * (NJS_STRING_LONG) then the size is in the long_string.size field
+     * and the long_string.data field points to a long string.
      *
      * The number of the string types is limited to 2 types to minimize
      * overhead of processing string fields.  It is also possible to add
@@ -163,28 +163,23 @@ union njs_value_s {
          */
         uint8_t                       truth;
 
-        /* 0xff if u.data.string is external string. */
-        uint8_t                       external0;
-        uint8_t                       _spare;
-
-        /* A long string size. */
-        uint32_t                      string_size;
+        uint16_t                      _spare1;
+        uint32_t                      _spare2;
 
         union {
-            double                     number;
-            njs_string_t               *string;
-            njs_object_t               *object;
-            njs_array_t                *array;
-            njs_object_value_t         *object_value;
-            njs_function_t             *function;
-            njs_function_lambda_t      *lambda;
-            njs_regexp_t               *regexp;
-            njs_date_t                 *date;
-            njs_getter_t               getter;
-            njs_extern_t               *external;
-            njs_value_t                *value;
-            njs_property_next_t        *next;
-            void                       *data;
+            double                    number;
+            njs_object_t              *object;
+            njs_array_t               *array;
+            njs_object_value_t        *object_value;
+            njs_function_t            *function;
+            njs_function_lambda_t     *lambda;
+            njs_regexp_t              *regexp;
+            njs_date_t                *date;
+            njs_getter_t              getter;
+            njs_extern_t              *external;
+            njs_value_t               *value;
+            njs_property_next_t       *next;
+            void                      *data;
         } u;
     } data;
 
@@ -200,6 +195,18 @@ union njs_value_s {
         u_char                        start[NJS_STRING_SHORT];
     } short_string;
 
+    struct {
+        njs_value_type_t              type:8;  /* 5 bits */
+        uint8_t                       truth;
+
+        /* 0xff if data is external string. */
+        uint8_t                       external;
+        uint8_t                       _spare;
+
+        uint32_t                      size;
+        njs_string_t                  *data;
+    } long_string;
+
     njs_value_type_t                  type:8;  /* 5 bits */
 };
 
@@ -326,11 +333,11 @@ typedef union {
 /* NJS_STRING_LONG is set for both big and little endian platforms. */
 
 #define njs_long_string(s) {                                                  \
-    .data = {                                                                 \
+    .long_string = {                                                          \
         .type = NJS_STRING,                                                   \
         .truth = (NJS_STRING_LONG << 4) | NJS_STRING_LONG,                    \
-        .string_size = sizeof(s) - 1,                                         \
-        .u.string = & (njs_string_t) {                                        \
+        .size = sizeof(s) - 1,                                                \
+        .data = & (njs_string_t) {                                            \
             .start = (u_char *) s,                                            \
             .length = sizeof(s) - 1,                                          \
         }                                                                     \
@@ -430,8 +437,8 @@ typedef njs_ret_t (*njs_vmcode_operation
             (str)->start = (u_char *) (value)->short_string.start;            \
                                                                               \
         } else {                                                              \
-            (str)->length = (value)->data.string_size;                        \
-            (str)->start = (u_char *) (value)->data.u.string->start;          \
+            (str)->length = (value)->long_string.size;                        \
+            (str)->start = (u_char *) (value)->long_string.data->start;       \
         }                                                                     \
     } while (0)
 
@@ -455,7 +462,7 @@ typedef njs_ret_t (*njs_vmcode_operation
             (value)->short_string.length = length;                            \
                                                                               \
         } else {                                                              \
-            (value)->data.u.string->length = length;                          \
+            (value)->long_string.data->length = length;                       \
         }                                                                     \
     } while (0)
 


More information about the nginx-devel mailing list