mirror of
https://github.com/json-c/json-c.git
synced 2026-04-09 15:29:07 +08:00
Merge pull request #632 from json-c/json_object-split
Json object split
This commit is contained in:
@@ -221,6 +221,12 @@ check_type_size(int64_t SIZEOF_INT64_T)
|
|||||||
check_type_size(long SIZEOF_LONG)
|
check_type_size(long SIZEOF_LONG)
|
||||||
check_type_size("long long" SIZEOF_LONG_LONG)
|
check_type_size("long long" SIZEOF_LONG_LONG)
|
||||||
check_type_size("size_t" SIZEOF_SIZE_T)
|
check_type_size("size_t" SIZEOF_SIZE_T)
|
||||||
|
if (MSVC)
|
||||||
|
list(APPEND CMAKE_EXTRA_INCLUDE_FILES BaseTsd.h)
|
||||||
|
check_type_size("SSIZE_T" SIZEOF_SSIZE_T)
|
||||||
|
else()
|
||||||
|
check_type_size("ssize_t" SIZEOF_SSIZE_T)
|
||||||
|
endif()
|
||||||
|
|
||||||
check_c_source_compiles(
|
check_c_source_compiles(
|
||||||
"
|
"
|
||||||
|
|||||||
@@ -12,6 +12,13 @@ Other changes
|
|||||||
* Issue #471: always create directories with mode 0755, regardless of umask.
|
* Issue #471: always create directories with mode 0755, regardless of umask.
|
||||||
* Added a JSON_TOKENER_ALLOW_TRAILING_CHARS flag to allow multiple objects
|
* Added a JSON_TOKENER_ALLOW_TRAILING_CHARS flag to allow multiple objects
|
||||||
to be parsed even when JSON_TOKENER_STRICT is set.
|
to be parsed even when JSON_TOKENER_STRICT is set.
|
||||||
|
* Split the internal json_object structure into several sub-types, one for
|
||||||
|
each json_type (json_object_object, json_object_string, etc...).
|
||||||
|
This improves memory usage and speed, with the benchmark under
|
||||||
|
bench/ report 5.8% faster test time and 6%(max RSS)-12%(peak heap)
|
||||||
|
less memory usage.
|
||||||
|
Memory used just for json_object structures decreased 27%, so use cases
|
||||||
|
with fewer arrays and/or strings would benefit more.
|
||||||
|
|
||||||
|
|
||||||
***
|
***
|
||||||
|
|||||||
15
README.json_object-split.md
Normal file
15
README.json_object-split.md
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
This branch implements the changes briefly described at:
|
||||||
|
|
||||||
|
https://github.com/json-c/json-c/wiki/Proposal:-struct-json_object-split
|
||||||
|
|
||||||
|
These were originally mentioned in:
|
||||||
|
Issue #535 - short string optimization: excessive array length
|
||||||
|
|
||||||
|
The changes are expected to impact and possibly invalidate:
|
||||||
|
Issue #552 - Some anlysis about memory allocator in json-c
|
||||||
|
|
||||||
|
and will likely cause notable conflicts in any other significant un-merged
|
||||||
|
changes, such as PR#620 - Introduce json_object_new_string_{ext,noalloc}()
|
||||||
|
|
||||||
|
|
||||||
@@ -194,6 +194,9 @@
|
|||||||
/* The number of bytes in type size_t */
|
/* The number of bytes in type size_t */
|
||||||
#cmakedefine SIZEOF_SIZE_T @SIZEOF_SIZE_T@
|
#cmakedefine SIZEOF_SIZE_T @SIZEOF_SIZE_T@
|
||||||
|
|
||||||
|
/* The number of bytes in type ssize_t */
|
||||||
|
#cmakedefine SIZEOF_SSIZE_T @SIZEOF_SSIZE_T@
|
||||||
|
|
||||||
/* Specifier for __thread */
|
/* Specifier for __thread */
|
||||||
#cmakedefine SPEC___THREAD @SPEC___THREAD@
|
#cmakedefine SPEC___THREAD @SPEC___THREAD@
|
||||||
|
|
||||||
|
|||||||
605
json_object.c
605
json_object.c
File diff suppressed because it is too large
Load Diff
@@ -9,6 +9,7 @@
|
|||||||
*
|
*
|
||||||
*******************************************************************************
|
*******************************************************************************
|
||||||
*/
|
*/
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
|||||||
@@ -20,14 +20,18 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**< how many bytes are directly stored in json_object for strings? */
|
|
||||||
#define LEN_DIRECT_STRING_DATA 32
|
|
||||||
|
|
||||||
struct json_object;
|
struct json_object;
|
||||||
#include "json_inttypes.h"
|
#include "json_inttypes.h"
|
||||||
#include "json_types.h"
|
#include "json_types.h"
|
||||||
|
|
||||||
typedef void(json_object_private_delete_fn)(struct json_object *o);
|
#ifdef HAVE_UNISTD_H
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif /* HAVE_UNISTD_H */
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#include <BaseTsd.h>
|
||||||
|
typedef SSIZE_T ssize_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
/* json object int type, support extension*/
|
/* json object int type, support extension*/
|
||||||
typedef enum json_object_int_type
|
typedef enum json_object_int_type
|
||||||
@@ -40,39 +44,56 @@ struct json_object
|
|||||||
{
|
{
|
||||||
enum json_type o_type;
|
enum json_type o_type;
|
||||||
uint32_t _ref_count;
|
uint32_t _ref_count;
|
||||||
json_object_private_delete_fn *_delete;
|
|
||||||
json_object_to_json_string_fn *_to_json_string;
|
json_object_to_json_string_fn *_to_json_string;
|
||||||
struct printbuf *_pb;
|
struct printbuf *_pb;
|
||||||
union data
|
|
||||||
{
|
|
||||||
json_bool c_boolean;
|
|
||||||
double c_double;
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
union
|
|
||||||
{
|
|
||||||
int64_t c_int64;
|
|
||||||
uint64_t c_uint64;
|
|
||||||
} cint;
|
|
||||||
enum json_object_int_type cint_type;
|
|
||||||
} c_int;
|
|
||||||
struct lh_table *c_object;
|
|
||||||
struct array_list *c_array;
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
json_object_delete_fn *_user_delete;
|
json_object_delete_fn *_user_delete;
|
||||||
void *_userdata;
|
void *_userdata;
|
||||||
|
// Actually longer, always malloc'd as some more-specific type.
|
||||||
|
// The rest of a struct json_object_${o_type} follows
|
||||||
|
};
|
||||||
|
|
||||||
|
struct json_object_object
|
||||||
|
{
|
||||||
|
struct json_object base;
|
||||||
|
struct lh_table *c_object;
|
||||||
|
};
|
||||||
|
struct json_object_array
|
||||||
|
{
|
||||||
|
struct json_object base;
|
||||||
|
struct array_list *c_array;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct json_object_boolean
|
||||||
|
{
|
||||||
|
struct json_object base;
|
||||||
|
json_bool c_boolean;
|
||||||
|
};
|
||||||
|
struct json_object_double
|
||||||
|
{
|
||||||
|
struct json_object base;
|
||||||
|
double c_double;
|
||||||
|
};
|
||||||
|
struct json_object_int
|
||||||
|
{
|
||||||
|
struct json_object base;
|
||||||
|
enum json_object_int_type cint_type;
|
||||||
|
union
|
||||||
|
{
|
||||||
|
int64_t c_int64;
|
||||||
|
uint64_t c_uint64;
|
||||||
|
} cint;
|
||||||
|
};
|
||||||
|
struct json_object_string
|
||||||
|
{
|
||||||
|
struct json_object base;
|
||||||
|
ssize_t len; // Signed b/c negative lengths indicate data is a pointer
|
||||||
|
// Consider adding an "alloc" field here, if json_object_set_string calls
|
||||||
|
// to expand the length of a string are common operations to perform.
|
||||||
|
union
|
||||||
|
{
|
||||||
|
char idata[1]; // Immediate data. Actually longer
|
||||||
|
char *pdata; // Only when len < 0
|
||||||
|
} c_string;
|
||||||
};
|
};
|
||||||
|
|
||||||
void _json_c_set_last_err(const char *err_fmt, ...);
|
void _json_c_set_last_err(const char *err_fmt, ...);
|
||||||
|
|||||||
@@ -56,14 +56,18 @@ int main(int argc, char **argv)
|
|||||||
#define MID "A MID STRING"
|
#define MID "A MID STRING"
|
||||||
// 12345678901234567890123456789012....
|
// 12345678901234567890123456789012....
|
||||||
#define HUGE "A string longer than 32 chars as to check non local buf codepath"
|
#define HUGE "A string longer than 32 chars as to check non local buf codepath"
|
||||||
tmp = json_object_new_string(SHORT);
|
tmp = json_object_new_string(MID);
|
||||||
assert(strcmp(json_object_get_string(tmp), SHORT) == 0);
|
|
||||||
json_object_set_string(tmp, MID);
|
|
||||||
assert(strcmp(json_object_get_string(tmp), MID) == 0);
|
assert(strcmp(json_object_get_string(tmp), MID) == 0);
|
||||||
json_object_set_string(tmp, HUGE);
|
assert(strcmp(json_object_to_json_string(tmp), "\"" MID "\"") == 0);
|
||||||
assert(strcmp(json_object_get_string(tmp), HUGE) == 0);
|
|
||||||
json_object_set_string(tmp, SHORT);
|
json_object_set_string(tmp, SHORT);
|
||||||
assert(strcmp(json_object_get_string(tmp), SHORT) == 0);
|
assert(strcmp(json_object_get_string(tmp), SHORT) == 0);
|
||||||
|
assert(strcmp(json_object_to_json_string(tmp), "\"" SHORT "\"") == 0);
|
||||||
|
json_object_set_string(tmp, HUGE);
|
||||||
|
assert(strcmp(json_object_get_string(tmp), HUGE) == 0);
|
||||||
|
assert(strcmp(json_object_to_json_string(tmp), "\"" HUGE "\"") == 0);
|
||||||
|
json_object_set_string(tmp, SHORT);
|
||||||
|
assert(strcmp(json_object_get_string(tmp), SHORT) == 0);
|
||||||
|
assert(strcmp(json_object_to_json_string(tmp), "\"" SHORT "\"") == 0);
|
||||||
json_object_put(tmp);
|
json_object_put(tmp);
|
||||||
printf("STRING PASSED\n");
|
printf("STRING PASSED\n");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user