mirror of
https://github.com/json-c/json-c.git
synced 2026-04-01 19:39:07 +08:00
more efficient handling for smalls strings inside json_object
smalls strings inside json_objects had a high overhead because dynamic memory allocation was needed for each of them. This also meant that the pointer needed to be updated. This is now changed so that small strings can directly be stored inside the json_object. Note that on the regular 64 bit machines a pointer takes 8 bytes. So even without increasing memory, we could store string up to 7 bytes directly inside the object. The max size is configurable. I have selected up to 31 bytes (which means a buffer of 32 including the NUL byte). This brings a 24-bytes memory overhead, but I consider that still useful because the memory allocator usually also has quite some overhead (16 bytes) for dyn alloced memory blocks. In any case, the max buffer size can be tweaked via #define.
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define LEN_DIRECT_STRING_DATA 32 /**< how many bytes are directly stored in json_object for strings? */
|
||||
|
||||
typedef void (json_object_private_delete_fn)(struct json_object *o);
|
||||
|
||||
struct json_object
|
||||
@@ -32,7 +34,13 @@ struct json_object
|
||||
struct lh_table *c_object;
|
||||
struct array_list *c_array;
|
||||
struct {
|
||||
char *str;
|
||||
union {
|
||||
/* optimize: if we have small strings, we can store them
|
||||
* directly. This saves considerable CPU cycles AND memory.
|
||||
*/
|
||||
char *ptr;
|
||||
char data[LEN_DIRECT_STRING_DATA];
|
||||
} str;
|
||||
int len;
|
||||
} c_string;
|
||||
} o;
|
||||
|
||||
Reference in New Issue
Block a user