Skip to content

Commit b5d4cdb

Browse files
authored
Merge pull request #657 from WilhelmWiens/ReuseKeyLengthInPackUnpack_c
Optimized pack_unpack.c reuse key length
2 parents 44cb101 + 3e989ad commit b5d4cdb

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/pack_unpack.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ static json_t *pack_object(scanner_t *s, va_list *ap) {
258258
if (s->has_error)
259259
json_decref(value);
260260

261-
if (!s->has_error && json_object_set_new_nocheck(object, key, value)) {
261+
if (!s->has_error && json_object_setn_new_nocheck(object, key, len, value)) {
262262
set_error(s, "<internal>", json_error_out_of_memory,
263263
"Unable to add key \"%s\"", key);
264264
s->has_error = 1;
@@ -488,6 +488,7 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap) {
488488

489489
while (token(s) != '}') {
490490
const char *key;
491+
size_t key_len;
491492
json_t *value;
492493
int opt = 0;
493494

@@ -521,6 +522,7 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap) {
521522
set_error(s, "<args>", json_error_null_value, "NULL object key");
522523
goto out;
523524
}
525+
key_len = strlen(key);
524526

525527
next_token(s);
526528

@@ -533,7 +535,7 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap) {
533535
/* skipping */
534536
value = NULL;
535537
} else {
536-
value = json_object_get(root, key);
538+
value = json_object_getn(root, key, key_len);
537539
if (!value && !opt) {
538540
set_error(s, "<validation>", json_error_item_not_found,
539541
"Object item not found: %s", key);
@@ -544,7 +546,7 @@ static int unpack_object(scanner_t *s, json_t *root, va_list *ap) {
544546
if (unpack(s, value, ap))
545547
goto out;
546548

547-
hashtable_set(&key_set, key, strlen(key), json_null());
549+
hashtable_set(&key_set, key, key_len, json_null());
548550
next_token(s);
549551
}
550552

0 commit comments

Comments
 (0)