[njs] A small Array.prototype.sort() optimization.

Igor Sysoev igor at sysoev.ru
Wed Mar 29 12:56:49 UTC 2017


details:   http://hg.nginx.org/njs/rev/90743d1bb614
branches:  
changeset: 323:90743d1bb614
user:      Igor Sysoev <igor at sysoev.ru>
date:      Wed Mar 29 15:54:37 2017 +0300
description:
A small Array.prototype.sort() optimization.

diffstat:

 njs/njs_array.c |  34 ++++++++++++++++++----------------
 1 files changed, 18 insertions(+), 16 deletions(-)

diffs (52 lines):

diff -r 8cdbd57379e8 -r 90743d1bb614 njs/njs_array.c
--- a/njs/njs_array.c	Wed Mar 29 15:54:33 2017 +0300
+++ b/njs/njs_array.c	Wed Mar 29 15:54:37 2017 +0300
@@ -1802,9 +1802,9 @@ njs_array_prototype_sort_continuation(nj
     if (njs_is_number(&sort->retval)) {
 
         /*
-         * The sort function is impelemented with the insertion sort algorithm.
+         * The sort function is implemented with the insertion sort algorithm.
          * Its worst and average computational complexity is O^2.  This point
-         * should be considired as return point from comparison function so
+         * should be considered as return point from comparison function so
          * "goto next" moves control to the appropriate step of the algorithm.
          * The first iteration also goes there because sort->retval is zero.
          */
@@ -1824,20 +1824,22 @@ njs_array_prototype_sort_continuation(nj
         do {
             if (n > 0) {
 
-                if (njs_is_valid(&start[n]) && njs_is_valid(&start[n - 1])) {
-                    arguments[0] = njs_value_void;
-
-                    /* GC: array elt, array */
-                    arguments[1] = start[n - 1];
-                    arguments[2] = start[n];
-
-                    sort->index = n;
-
-                    return njs_function_apply(vm, sort->function, arguments, 3,
-                                              (njs_index_t) &sort->retval);
-                }
-
-                if (!njs_is_valid(&start[n - 1]) && njs_is_valid(&start[n])) {
+                if (njs_is_valid(&start[n])) {
+
+                    if (njs_is_valid(&start[n - 1])) {
+                        arguments[0] = njs_value_void;
+
+                        /* GC: array elt, array */
+                        arguments[1] = start[n - 1];
+                        arguments[2] = start[n];
+
+                        sort->index = n;
+
+                        return njs_function_apply(vm, sort->function,
+                                                  arguments, 3,
+                                                  (njs_index_t) &sort->retval);
+                    }
+
                     /* Move invalid values to the end of array. */
                     goto swap;
                 }


More information about the nginx-devel mailing list