Skip to content

Commit

Permalink
Small performance improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Mamchur committed Jul 13, 2013
1 parent 37dc49b commit 43dd5b5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ - (IBAction)parse1:(id)sender {
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, &start);

const int COUNT = 1;
const int COUNT = 10000;
for (int i = 0; i < COUNT; i++) {
jsonlite_parser p = jsonlite_parser_init(512);
jsonlite_result res = jsonlite_parser_tokenize(p, buffer, l);
Expand Down
33 changes: 11 additions & 22 deletions objc/JsonLiteAccumulator.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,10 @@ static void ReleaseKeyValues(JsonLiteAccumulatorState *s) {
CFRelease((CFTypeRef)s->values[i]);
}

for (NSInteger i = 0; i < s->length; i++) {
id key = s->keys[i];
if (key == nil) {
break;
}

CFRelease((CFTypeRef)key);
for (NSInteger i = 0; * s->keys != NULL && i < s->length; i++) {
CFRelease((CFTypeRef)s->keys[i]);
}

memset(s->values, 0, s->length * sizeof(id)); // LCOV_EXCL_LINE
memset(s->keys, 0, s->length * sizeof(id)); // LCOV_EXCL_LINE

s->length = 0;
}

Expand Down Expand Up @@ -155,9 +148,9 @@ - (void)extendCapacity {
memcpy(newKeys, keys, capacity * sizeof(id)); // LCOV_EXCL_LINE
memcpy(newHashes, hashes, capacity * sizeof(CFHashCode)); // LCOV_EXCL_LINE

NSInteger keysOffset = newKeys - keys;
NSInteger valuesOffset = newValues - values;
NSInteger hashesOffset = newHashes - hashes;
ptrdiff_t keysOffset = newKeys - keys;
ptrdiff_t valuesOffset = newValues - values;
ptrdiff_t hashesOffset = newHashes - hashes;
for (JsonLiteAccumulatorState *c = state; c <= current; c++) {
c->keys += keysOffset;
c->values += valuesOffset;
Expand Down Expand Up @@ -185,16 +178,11 @@ - (void)pushState {
next->hashes = current->hashes + delta;
next->capacity = capacity - (next->keys - keys);
next->length = 0;
*next->keys = NULL;
*next->values = NULL;
current++;
}

- (void)popState {
memset(current->values, 0, current->length * sizeof(id)); // LCOV_EXCL_LINE
memset(current->keys, 0, current->length * sizeof(id)); // LCOV_EXCL_LINE
current->length = 0;
current--;
}

- (void)parser:(JsonLiteParser *)parser didFinishParsingWithError:(NSError *)error {
if ([error code] == JsonLiteCodeEndOfStream) {
jsonlite_token_pool_copy_tokens(keyPool);
Expand All @@ -219,7 +207,7 @@ - (void)parserDidEndObject:(JsonLiteParser *)parser {
[delegate accumulator:self didAccumulateDictionary:d];
}

[self popState];
current--;
}

- (void)parserDidStartArray:(JsonLiteParser *)parser {
Expand All @@ -234,7 +222,8 @@ - (void)parserDidEndArray:(JsonLiteParser *)parser {
if (flags.didAccumulateArray) {
[delegate accumulator:self didAccumulateArray:a];
}
[self popState];

current--;
}

- (void)parserFoundTrueToken:(JsonLiteParser *)parser {
Expand Down
2 changes: 1 addition & 1 deletion objc/JsonLiteParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ + (void)load {
- (id)initWithDepth:(NSUInteger)aDepth {
self = [super init];
if (self != nil) {
depth = aDepth == 0 ? 16 : aDepth;
depth = aDepth < 2 ? 16 : aDepth;
internal.parser = NULL;
}
return self;
Expand Down

0 comments on commit 43dd5b5

Please sign in to comment.