Skip to content

Commit

Permalink
Allocators
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirek Rusin authored and Mirek Rusin committed Mar 2, 2011
1 parent 72bda54 commit 3acfac2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
27 changes: 23 additions & 4 deletions CoreJSON/CoreJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,32 @@ inline CFIndex __JSONElementsAppend(CoreJSONRef json, CFTypeRef value) {
return index;
}

#pragma Memory allocation

inline void *__JSONAllocatorAllocate(void *ctx, unsigned int sz) {
return CFAllocatorAllocate(ctx, sz, 0);
}

inline void __JSONAllocatorDeallocate(void *ctx, void *ptr) {
CFAllocatorDeallocate(ctx, ptr);
}

inline void *__JSONAllocatorReallocate(void *ctx, void *ptr, unsigned int sz) {
return CFAllocatorReallocate(ctx, ptr, sz, 0);
}

#pragma Public API

inline CoreJSONRef JSONCreate(CFAllocatorRef allocator) {
CoreJSONRef json = CFAllocatorAllocate(allocator, sizeof(CoreJSON), 0);
if (json) {
json->allocator = allocator ? CFRetain(allocator) : NULL;

json->yajlAllocFuncs.ctx = (void *)json->allocator;
json->yajlAllocFuncs.malloc = __JSONAllocatorAllocate;
json->yajlAllocFuncs.realloc = __JSONAllocatorReallocate;
json->yajlAllocFuncs.free = __JSONAllocatorDeallocate;

json->yajlParserCallbacks.yajl_null = __JSONParserAppendNull;
json->yajlParserCallbacks.yajl_boolean = __JSONParserAppendBooleanWithInteger;

Expand All @@ -383,10 +402,10 @@ inline CoreJSONRef JSONCreate(CFAllocatorRef allocator) {
json->yajlParserCallbacks.yajl_string = __JSONParserAppendStringWithBytes;

json->yajlParserConfig.allowComments = 1;
json->yajlParserConfig.checkUTF8 = 1;
json->yajlParserConfig.checkUTF8 = 0;

json->yajlGeneratorConfig.beautify = 1;
json->yajlGeneratorConfig.indentString = " ";
json->yajlGeneratorConfig.beautify = 0;
json->yajlGeneratorConfig.indentString = "";
json->yajlGenerator = yajl_gen_alloc(&json->yajlGeneratorConfig, NULL);

json->elementsIndex = 0;
Expand Down Expand Up @@ -427,7 +446,7 @@ inline void JSONParseWithString(CoreJSONRef json, CFStringRef string) {
// TODO: Let's make sure we've got a clean plate first
//CFArrayRemoveAllValues(json->elements);

json->yajlParser = yajl_alloc(&json->yajlParserCallbacks, &json->yajlParserConfig, NULL, (void *)json);
json->yajlParser = yajl_alloc(&json->yajlParserCallbacks, &json->yajlParserConfig, &json->yajlAllocFuncs, (void *)json);

__JSONUTF8String utf8 = __JSONUTF8StringMake(json->allocator, string);
if ((json->yajlParserStatus = yajl_parse(json->yajlParser, __JSONUTF8StringGetBuffer(utf8), (unsigned int)__JSONUTF8StringGetMaximumSize(utf8))) != yajl_status_ok) {
Expand Down
9 changes: 8 additions & 1 deletion CoreJSON/CoreJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ typedef struct {
yajl_parser_config yajlParserConfig;
yajl_status yajlParserStatus;
yajl_callbacks yajlParserCallbacks;

yajl_alloc_funcs yajlAllocFuncs;

yajl_gen yajlGenerator;
yajl_gen_config yajlGeneratorConfig;
yajl_status yajlGeneratorStatus;
Expand All @@ -146,6 +147,12 @@ typedef CoreJSON *CoreJSONRef;

CFIndex __JSONElementsAppend(CoreJSONRef json, CFTypeRef value);

#pragma Memory allocation

void *__JSONAllocatorAllocate (void *ctx, unsigned int sz);
void __JSONAllocatorDeallocate (void *ctx, void *ptr);
void *__JSONAllocatorReallocate (void *ctx, void *ptr, unsigned int sz);

#pragma Public API

extern CoreJSONRef JSONCreate (CFAllocatorRef allocator);
Expand Down
29 changes: 21 additions & 8 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@ CoreJSON is iOS, OSX (will also work on Linux) Core Foundation based fast parser

Comparison with other JSON frameworks:

iPhone 4.2 Simulator running on MacBook Pro 3.06GHz Intel Core 2 Duo, OSX SL 10.6.6:

| Framework | What | Min | Mean | Max
|:---------------------|--------:|--------:|--------:|--------:
| JSONKit | read | 4.032 | 4.571 | 13.548
| CoreJSON | read | 4.892 | 5.487 | 10.001
| YAJL | read | 7.637 | 7.670 | 8.084
| Apple JSON | read | 7.290 | 8.241 | 14.693
| TouchJSON | read | 13.565 | 13.970 | 24.359
| JSON Framework | read | 9.866 | 12.001 | 30.772

iPhone 4:

| Framework | What | Min | Mean | Max
|:---------------------|--------:|--------:|--------:|--------:
| JSONKit | read | 3.960 | 4.963 | 71.561
| CoreJSON | read | 5.356 | 5.708 | 5.881
| Apple JSON | read | 7.245 | 7.568 | 13.036
| YAJL | read | 7.693 | 7.756 | 8.530
| JSON Framework | read | 9.167 | 9.336 | 11.189
| TouchJSON | read | 13.231 | 13.485 | 17.559

_Tests performed on iPad Simulator with https://github.com/samsoffes/json-benchmarks_
| JSONKit | read | 35.887 | 37.185 | 40.931
| CoreJSON | read | 59.742 | 64.204 | 132.731
| Apple JSON | read | 114.739 | 119.915 | 229.629
| YAJL | read | 120.171 | 124.833 | 262.097
| JSON Framework | read | 147.569 | 152.352 | 205.846
| TouchJSON | read | 229.279 | 234.811 | 370.086

_Tests performed with https://github.com/samsoffes/json-benchmarks_

## Usage

Expand Down

0 comments on commit 3acfac2

Please sign in to comment.