Browse Source

Merge pull request #632 from json-c/json_object-split

Json object split
tags/json-c-0.15-20200726
Eric Hawicz GitHub 5 years ago
parent
commit
99bb2121c6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 457 additions and 263 deletions
  1. +6
    -0
      CMakeLists.txt
  2. +7
    -0
      ChangeLog
  3. +15
    -0
      README.json_object-split.md
  4. +3
    -0
      cmake/config.h.in
  5. +364
    -227
      json_object.c
  6. +1
    -0
      json_object_iterator.c
  7. +54
    -33
      json_object_private.h
  8. +7
    -3
      tests/test_set_value.c

+ 6
- 0
CMakeLists.txt View File

@@ -221,6 +221,12 @@ check_type_size(int64_t SIZEOF_INT64_T)
check_type_size(long SIZEOF_LONG)
check_type_size("long long" SIZEOF_LONG_LONG)
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(
"


+ 7
- 0
ChangeLog View File

@@ -12,6 +12,13 @@ Other changes
* Issue #471: always create directories with mode 0755, regardless of umask.
* Added a JSON_TOKENER_ALLOW_TRAILING_CHARS flag to allow multiple objects
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
- 0
README.json_object-split.md View 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}()



+ 3
- 0
cmake/config.h.in View File

@@ -194,6 +194,9 @@
/* The number of bytes in type 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 */
#cmakedefine SPEC___THREAD @SPEC___THREAD@



+ 364
- 227
json_object.c
File diff suppressed because it is too large
View File


+ 1
- 0
json_object_iterator.c View File

@@ -9,6 +9,7 @@
*
*******************************************************************************
*/
#include "config.h"

#include <stddef.h>



+ 54
- 33
json_object_private.h View File

@@ -20,14 +20,18 @@
extern "C" {
#endif

/**< how many bytes are directly stored in json_object for strings? */
#define LEN_DIRECT_STRING_DATA 32

struct json_object;
#include "json_inttypes.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*/
typedef enum json_object_int_type
@@ -40,39 +44,56 @@ struct json_object
{
enum json_type o_type;
uint32_t _ref_count;
json_object_private_delete_fn *_delete;
json_object_to_json_string_fn *_to_json_string;
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;
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, ...);


+ 7
- 3
tests/test_set_value.c View File

@@ -56,14 +56,18 @@ int main(int argc, char **argv)
#define MID "A MID STRING"
// 12345678901234567890123456789012....
#define HUGE "A string longer than 32 chars as to check non local buf codepath"
tmp = json_object_new_string(SHORT);
assert(strcmp(json_object_get_string(tmp), SHORT) == 0);
json_object_set_string(tmp, MID);
tmp = json_object_new_string(MID);
assert(strcmp(json_object_get_string(tmp), MID) == 0);
assert(strcmp(json_object_to_json_string(tmp), "\"" MID "\"") == 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_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);
printf("STRING PASSED\n");



Loading…
Cancel
Save