[njs] Optimized qjs_to_bytes().

noreply at nginx.com noreply at nginx.com
Tue Oct 8 23:04:01 UTC 2024


details:   https://github.com/nginx/njs/commit/39a2d4bf212346d1487e4d27383453cafefa17ea
branches:  master
commit:    39a2d4bf212346d1487e4d27383453cafefa17ea
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon, 7 Oct 2024 22:46:40 -0700
description:
Optimized qjs_to_bytes().

Doing JS_IsString() check first before a heavy-weight call to
JS_GetTypedArrayBuffer() which throws an exception when argument is not
a typed array.

---
 src/qjs.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/qjs.c b/src/qjs.c
index e7653569..3d378fcc 100644
--- a/src/qjs.c
+++ b/src/qjs.c
@@ -175,6 +175,10 @@ qjs_to_bytes(JSContext *ctx, qjs_bytes_t *bytes, JSValueConst value)
     size_t   byte_offset, byte_length;
     JSValue  val;
 
+    if (JS_IsString(value)) {
+        goto string;
+    }
+
     val = JS_GetTypedArrayBuffer(ctx, value, &byte_offset, &byte_length, NULL);
     if (!JS_IsException(val)) {
         bytes->start = JS_GetArrayBuffer(ctx, &bytes->length, val);
@@ -195,8 +199,6 @@ qjs_to_bytes(JSContext *ctx, qjs_bytes_t *bytes, JSValueConst value)
         return 0;
     }
 
-    bytes->tag = JS_TAG_STRING;
-
     if (!JS_IsString(value)) {
         val = JS_ToString(ctx, value);
 
@@ -209,6 +211,9 @@ qjs_to_bytes(JSContext *ctx, qjs_bytes_t *bytes, JSValueConst value)
         }
     }
 
+string:
+
+    bytes->tag = JS_TAG_STRING;
     bytes->start = (u_char *) JS_ToCStringLen(ctx, &bytes->length, value);
 
     return (bytes->start != NULL) ? 0 : -1;


More information about the nginx-devel mailing list