Skip to content

Commit 758a8fb

Browse files
authored
Do not sort object for better parsing speed. (#15)
1 parent 782cc26 commit 758a8fb

File tree

7 files changed

+13
-589
lines changed

7 files changed

+13
-589
lines changed

.editorconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,3 @@ indent_size = 4
88
[list.h]
99
indent_style = tab
1010
indent_size = 8
11-
[rbtree.*]
12-
indent_style = tab
13-
indent_size = 8

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ LD = gcc
99

1010
all: test_speed parse_json
1111

12-
json_parser.o: json_parser.c json_parser.h list.h rbtree.h
12+
json_parser.o: json_parser.c json_parser.h list.h
1313

14-
test_speed: json_parser.o rbtree.o test_speed.o
14+
test_speed: json_parser.o test_speed.o
1515
$(LD) -o test_speed $^
1616

17-
parse_json: json_parser.o rbtree.o test.o
17+
parse_json: json_parser.o test.o
1818
$(LD) -o parse_json $^
1919

2020
clean:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int json_object_size(const json_object_t *obj);
7272
7373
/* Find the JSON value under the key @name. Returns NULL if @name
7474
can not be found. The time complexity of this function is
75-
O(log(n)), where n is the size of the JSON object.
75+
O(n), where n is the size of the JSON object.
7676
@name: The key to find
7777
@obj: JSON object
7878
Note: The returned pointer to JSON value is const. */

json_parser.c

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
#include <ctype.h>
66
#include <math.h>
77
#include "list.h"
8-
#include "rbtree.h"
98
#include "json_parser.h"
109

1110
#define JSON_DEPTH_LIMIT 1024
1211

1312
struct __json_object
1413
{
1514
struct list_head head;
16-
struct rb_root root;
1715
int size;
1816
};
1917

@@ -38,7 +36,6 @@ struct __json_value
3836
struct __json_member
3937
{
4038
struct list_head list;
41-
struct rb_node rb;
4239
json_value_t value;
4340
char name[1];
4441
};
@@ -448,28 +445,6 @@ static int __parse_json_number(const char *cursor, const char **end,
448445
return 0;
449446
}
450447

451-
static void __insert_json_member(json_member_t *memb, struct list_head *pos,
452-
json_object_t *obj)
453-
{
454-
struct rb_node **p = &obj->root.rb_node;
455-
struct rb_node *parent = NULL;
456-
json_member_t *entry;
457-
458-
while (*p)
459-
{
460-
parent = *p;
461-
entry = rb_entry(*p, json_member_t, rb);
462-
if (strcmp(memb->name, entry->name) < 0)
463-
p = &(*p)->rb_left;
464-
else
465-
p = &(*p)->rb_right;
466-
}
467-
468-
rb_link_node(&memb->rb, parent, p);
469-
rb_insert_color(&memb->rb, &obj->root);
470-
list_add(&memb->list, pos);
471-
}
472-
473448
static int __parse_json_value(const char *cursor, const char **end,
474449
int depth, json_value_t *val);
475450

@@ -540,7 +515,7 @@ static int __parse_json_members(const char *cursor, const char **end,
540515
return ret;
541516
}
542517

543-
__insert_json_member(memb, obj->head.prev, obj);
518+
list_add_tail(&memb->list, &obj->head);
544519
cnt++;
545520

546521
while (isspace(*cursor))
@@ -584,7 +559,6 @@ static int __parse_json_object(const char *cursor, const char **end,
584559
return -3;
585560

586561
INIT_LIST_HEAD(&obj->head);
587-
obj->root.rb_node = NULL;
588562
ret = __parse_json_members(cursor, end, depth + 1, obj);
589563
if (ret < 0)
590564
{
@@ -834,7 +808,6 @@ static void __move_json_value(json_value_t *src, json_value_t *dest)
834808
case JSON_VALUE_OBJECT:
835809
INIT_LIST_HEAD(&dest->value.object.head);
836810
list_splice(&src->value.object.head, &dest->value.object.head);
837-
dest->value.object.root.rb_node = src->value.object.root.rb_node;
838811
dest->value.object.size = src->value.object.size;
839812
break;
840813

@@ -878,7 +851,6 @@ static int __set_json_value(int type, va_list ap, json_value_t *val)
878851

879852
case JSON_VALUE_OBJECT:
880853
INIT_LIST_HEAD(&val->value.object.head);
881-
val->value.object.root.rb_node = NULL;
882854
val->value.object.size = 0;
883855
break;
884856

@@ -940,7 +912,7 @@ static int __copy_json_members(const json_object_t *src, json_object_t *dest)
940912
}
941913

942914
memcpy(memb->name, entry->name, len + 1);
943-
__insert_json_member(memb, dest->head.prev, dest);
915+
list_add_tail(&memb->list, &dest->head);
944916
}
945917

946918
return src->size;
@@ -995,7 +967,6 @@ static int __copy_json_value(const json_value_t *src, json_value_t *dest)
995967

996968
case JSON_VALUE_OBJECT:
997969
INIT_LIST_HEAD(&dest->value.object.head);
998-
dest->value.object.root.rb_node = NULL;
999970
ret = __copy_json_members(&src->value.object, &dest->value.object);
1000971
if (ret < 0)
1001972
{
@@ -1086,19 +1057,13 @@ json_array_t *json_value_array(const json_value_t *val)
10861057
const json_value_t *json_object_find(const char *name,
10871058
const json_object_t *obj)
10881059
{
1089-
struct rb_node *p = obj->root.rb_node;
1060+
struct list_head *pos;
10901061
json_member_t *memb;
1091-
int n;
10921062

1093-
while (p)
1063+
list_for_each(pos, &obj->head)
10941064
{
1095-
memb = rb_entry(p, json_member_t, rb);
1096-
n = strcmp(name, memb->name);
1097-
if (n < 0)
1098-
p = p->rb_left;
1099-
else if (n > 0)
1100-
p = p->rb_right;
1101-
else
1065+
memb = list_entry(pos, json_member_t, list);
1066+
if (strcmp(name, memb->name) == 0)
11021067
return &memb->value;
11031068
}
11041069

@@ -1200,7 +1165,7 @@ static const json_value_t *__json_object_insert(const char *name,
12001165
return NULL;
12011166
}
12021167

1203-
__insert_json_member(memb, pos, obj);
1168+
list_add(&memb->list, pos);
12041169
obj->size++;
12051170
return &memb->value;
12061171
}
@@ -1266,7 +1231,6 @@ json_value_t *json_object_remove(const json_value_t *val,
12661231
return NULL;
12671232

12681233
list_del(&memb->list);
1269-
rb_erase(&memb->rb, &obj->root);
12701234
obj->size--;
12711235

12721236
__move_json_value(&memb->value, (json_value_t *)val);

0 commit comments

Comments
 (0)