[njs] Improved memory consumption for concatenation of numbers and strings.
noreply at nginx.com
noreply at nginx.com
Thu Jul 3 16:35:02 UTC 2025
details: https://github.com/nginx/njs/commit/de00a558699b1fb234dd0328aa084a39ea788355
branches: master
commit: de00a558699b1fb234dd0328aa084a39ea788355
user: Vadim Zhestikov <v.zhestikov at f5.com>
date: Mon, 23 Jun 2025 12:33:18 -0700
description:
Improved memory consumption for concatenation of numbers and strings.
---
src/njs_vmcode.c | 52 +++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 45 insertions(+), 7 deletions(-)
diff --git a/src/njs_vmcode.c b/src/njs_vmcode.c
index c7adf63f..ebca15e3 100644
--- a/src/njs_vmcode.c
+++ b/src/njs_vmcode.c
@@ -667,14 +667,52 @@ NEXT_LBL;
src = value1;
}
- ret = njs_primitive_value_to_string(vm, &dst, src);
- if (njs_slow_path(ret != NJS_OK)) {
- goto error;
- }
+ if (njs_is_number(src)) {
+ size_t size;
+ njs_string_t sp;
+ char buf[64];
- ret = njs_string_concat(vm, s1, s2, &name);
- if (njs_slow_path(ret == NJS_ERROR)) {
- goto error;
+ /* Alloc free path for "str" + int or int + "str" concatenation. */
+
+ num = njs_number(src);
+
+ if (isnan(num)) {
+ njs_atom_to_value(vm, &dst, NJS_ATOM_STRING_NaN);
+
+ } else if (isinf(num)) {
+
+ if (num < 0) {
+ njs_atom_to_value(vm, &dst, NJS_ATOM_STRING__Infinity);
+
+ } else {
+ njs_atom_to_value(vm, &dst, NJS_ATOM_STRING_Infinity);
+ }
+
+ } else {
+ size = njs_dtoa(num, buf);
+
+ sp.start = (u_char *) buf;
+ sp.size = size;
+ sp.length = size;
+
+ dst.string.data = &sp;
+ }
+
+ ret = njs_string_concat(vm, s1, s2, &name);
+ if (njs_slow_path(ret == NJS_ERROR)) {
+ goto error;
+ }
+
+ } else {
+ ret = njs_primitive_value_to_string(vm, &dst, src);
+ if (njs_slow_path(ret != NJS_OK)) {
+ goto error;
+ }
+
+ ret = njs_string_concat(vm, s1, s2, &name);
+ if (njs_slow_path(ret == NJS_ERROR)) {
+ goto error;
+ }
}
njs_value_assign(retval, &name);
More information about the nginx-devel
mailing list