Skip to content

Commit

Permalink
Minor code cleanup and speed optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Jan 8, 2013
1 parent 8d3b807 commit a92339d
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 138 deletions.
222 changes: 111 additions & 111 deletions lib/ultrajson.h
Expand Up @@ -4,14 +4,14 @@ All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the ESN Social Software AB nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the ESN Social Software AB nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Expand Down Expand Up @@ -126,25 +126,25 @@ typedef int64_t JSLONG;

enum JSTYPES
{
JT_NULL, // NULL
JT_TRUE, //boolean true
JT_FALSE, //boolean false
JT_INT, //(JSINT32 (signed 32-bit))
JT_LONG, //(JSINT64 (signed 64-bit))
JT_DOUBLE, //(double)
JT_UTF8, //(char 8-bit)
JT_ARRAY, // Array structure
JT_OBJECT, // Key/Value structure
JT_INVALID, // Internal, do not return nor expect
JT_NULL, // NULL
JT_TRUE, //boolean true
JT_FALSE, //boolean false
JT_INT, //(JSINT32 (signed 32-bit))
JT_LONG, //(JSINT64 (signed 64-bit))
JT_DOUBLE, //(double)
JT_UTF8, //(char 8-bit)
JT_ARRAY, // Array structure
JT_OBJECT, // Key/Value structure
JT_INVALID, // Internal, do not return nor expect
};

typedef void * JSOBJ;
typedef void * JSITER;

typedef struct __JSONTypeContext
{
int type;
void *prv;
int type;
void *prv;
} JSONTypeContext;

/*
Expand All @@ -160,79 +160,79 @@ typedef void *(*JSPFN_REALLOC)(void *base, size_t size);

typedef struct __JSONObjectEncoder
{
void (*beginTypeContext)(JSOBJ obj, JSONTypeContext *tc);
void (*endTypeContext)(JSOBJ obj, JSONTypeContext *tc);
const char *(*getStringValue)(JSOBJ obj, JSONTypeContext *tc, size_t *_outLen);
JSINT64 (*getLongValue)(JSOBJ obj, JSONTypeContext *tc);
JSINT32 (*getIntValue)(JSOBJ obj, JSONTypeContext *tc);
double (*getDoubleValue)(JSOBJ obj, JSONTypeContext *tc);

/*
Begin iteration of an iteratable object (JS_ARRAY or JS_OBJECT)
Implementor should setup iteration state in ti->prv
*/
JSPFN_ITERBEGIN iterBegin;

/*
Retrieve next object in an iteration. Should return 0 to indicate iteration has reached end or 1 if there are more items.
Implementor is responsible for keeping state of the iteration. Use ti->prv fields for this
*/
JSPFN_ITERNEXT iterNext;

/*
Ends the iteration of an iteratable object.
Any iteration state stored in ti->prv can be freed here
*/
JSPFN_ITEREND iterEnd;

/*
Returns a reference to the value object of an iterator
The is responsible for the life-cycle of the returned string. Use iterNext/iterEnd and ti->prv to keep track of current object
*/
JSPFN_ITERGETVALUE iterGetValue;
/*
Return name of iterator.
The is responsible for the life-cycle of the returned string. Use iterNext/iterEnd and ti->prv to keep track of current object
*/
JSPFN_ITERGETNAME iterGetName;
/*
Release a value as indicated by setting ti->release = 1 in the previous getValue call.
The ti->prv array should contain the necessary context to release the value
*/
void (*releaseObject)(JSOBJ obj);

/* Library functions
Set to NULL to use STDLIB malloc,realloc,free */
JSPFN_MALLOC malloc;
JSPFN_REALLOC realloc;
JSPFN_FREE free;

/*
Configuration for max recursion, set to 0 to use default (see JSON_MAX_RECURSION_DEPTH)*/
int recursionMax;

/*
Configuration for max decimals of double floating poiunt numbers to encode (0-9) */
int doublePrecision;

/*
If true output will be ASCII with all characters above 127 encoded as \uXXXX. If false output will be UTF-8 or what ever charset strings are brought as */
int forceASCII;


/*
Set to an error message if error occured */
const char *errorMsg;
JSOBJ errorObj;

/* Buffer stuff */
char *start;
char *offset;
char *end;
int heap;
int level;
void (*beginTypeContext)(JSOBJ obj, JSONTypeContext *tc);
void (*endTypeContext)(JSOBJ obj, JSONTypeContext *tc);
const char *(*getStringValue)(JSOBJ obj, JSONTypeContext *tc, size_t *_outLen);
JSINT64 (*getLongValue)(JSOBJ obj, JSONTypeContext *tc);
JSINT32 (*getIntValue)(JSOBJ obj, JSONTypeContext *tc);
double (*getDoubleValue)(JSOBJ obj, JSONTypeContext *tc);

/*
Begin iteration of an iteratable object (JS_ARRAY or JS_OBJECT)
Implementor should setup iteration state in ti->prv
*/
JSPFN_ITERBEGIN iterBegin;

/*
Retrieve next object in an iteration. Should return 0 to indicate iteration has reached end or 1 if there are more items.
Implementor is responsible for keeping state of the iteration. Use ti->prv fields for this
*/
JSPFN_ITERNEXT iterNext;

/*
Ends the iteration of an iteratable object.
Any iteration state stored in ti->prv can be freed here
*/
JSPFN_ITEREND iterEnd;

/*
Returns a reference to the value object of an iterator
The is responsible for the life-cycle of the returned string. Use iterNext/iterEnd and ti->prv to keep track of current object
*/
JSPFN_ITERGETVALUE iterGetValue;

/*
Return name of iterator.
The is responsible for the life-cycle of the returned string. Use iterNext/iterEnd and ti->prv to keep track of current object
*/
JSPFN_ITERGETNAME iterGetName;

/*
Release a value as indicated by setting ti->release = 1 in the previous getValue call.
The ti->prv array should contain the necessary context to release the value
*/
void (*releaseObject)(JSOBJ obj);

/* Library functions
Set to NULL to use STDLIB malloc,realloc,free */
JSPFN_MALLOC malloc;
JSPFN_REALLOC realloc;
JSPFN_FREE free;

/*
Configuration for max recursion, set to 0 to use default (see JSON_MAX_RECURSION_DEPTH)*/
int recursionMax;

/*
Configuration for max decimals of double floating poiunt numbers to encode (0-9) */
int doublePrecision;

/*
If true output will be ASCII with all characters above 127 encoded as \uXXXX. If false output will be UTF-8 or what ever charset strings are brought as */
int forceASCII;


/*
Set to an error message if error occured */
const char *errorMsg;
JSOBJ errorObj;

/* Buffer stuff */
char *start;
char *offset;
char *end;
int heap;
int level;

} JSONObjectEncoder;

Expand Down Expand Up @@ -262,24 +262,24 @@ EXPORTFUNCTION char *JSON_EncodeObject(JSOBJ obj, JSONObjectEncoder *enc, char *

typedef struct __JSONObjectDecoder
{
JSOBJ (*newString)(wchar_t *start, wchar_t *end);
void (*objectAddKey)(JSOBJ obj, JSOBJ name, JSOBJ value);
void (*arrayAddItem)(JSOBJ obj, JSOBJ value);
JSOBJ (*newTrue)(void);
JSOBJ (*newFalse)(void);
JSOBJ (*newNull)(void);
JSOBJ (*newObject)(void);
JSOBJ (*newArray)(void);
JSOBJ (*newInt)(JSINT32 value);
JSOBJ (*newLong)(JSINT64 value);
JSOBJ (*newDouble)(double value);
void (*releaseObject)(JSOBJ obj);
JSPFN_MALLOC malloc;
JSPFN_FREE free;
JSPFN_REALLOC realloc;

char *errorStr;
char *errorOffset;
JSOBJ (*newString)(wchar_t *start, wchar_t *end);
void (*objectAddKey)(JSOBJ obj, JSOBJ name, JSOBJ value);
void (*arrayAddItem)(JSOBJ obj, JSOBJ value);
JSOBJ (*newTrue)(void);
JSOBJ (*newFalse)(void);
JSOBJ (*newNull)(void);
JSOBJ (*newObject)(void);
JSOBJ (*newArray)(void);
JSOBJ (*newInt)(JSINT32 value);
JSOBJ (*newLong)(JSINT64 value);
JSOBJ (*newDouble)(double value);
void (*releaseObject)(JSOBJ obj);
JSPFN_MALLOC malloc;
JSPFN_FREE free;
JSPFN_REALLOC realloc;

char *errorStr;
char *errorOffset;



Expand Down
43 changes: 16 additions & 27 deletions lib/ultrajsondec.c
Expand Up @@ -29,7 +29,7 @@ Portions of code from MODP_ASCII - Ascii transformations (upper/lower, etc)
http://code.google.com/p/stringencoders/
Copyright (c) 2007 Nick Galbreath -- nickg [at] modp [dot] com. All rights reserved.
Numeric decoder contains derivate code from TCL library
Numeric decoder derived from from TCL library
http://www.opensource.apple.com/source/tcl/tcl-14/tcl/license.terms
* Copyright (c) 1988-1993 The Regents of the University of California.
* Copyright (c) 1994 Sun Microsystems, Inc.
Expand Down Expand Up @@ -99,11 +99,12 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds)
1.0e256
};

int sign, expSign = FALSE;
int sign = FALSE;
int expSign = FALSE;
double fraction, dblExp, *d;
register const char *p;
register int c;
int neg = 1;
const char *p;
int expNeg = 1;
int fracNeg = 1;
int exp = 0; /* Exponent read from "EX" field. */
int fracExp = 0; /* Exponent that derives from the fractional
* part. Under normal circumstatnces, it is
Expand All @@ -117,8 +118,6 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds)
int mantSize; /* Number of digits in mantissa. */
int decPt = -1; /* Number of mantissa digits BEFORE decimal
* point. */
const char *pExp; /* Temporarily holds location of exponent
* in string. */
JSUINT64 frac;
JSUINT64 overflowLimit = (JSUINT64) LLONG_MAX;

Expand All @@ -131,15 +130,13 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds)
{
sign = TRUE;
p ++;
neg = -1;
fracNeg = -1;
overflowLimit = (JSUINT64) LLONG_MIN;
}
else
{
if (*p == '+')
p ++;
sign = FALSE;

}

frac = 0;
Expand All @@ -161,7 +158,7 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds)

if (frac > overflowLimit)
{
return SetError(ds, -1, neg == -1 ? "Value is too small" : "Value is too big");
return SetError(ds, -1, fracNeg == -1 ? "Value is too small" : "Value is too big");
}
p ++;
break;
Expand All @@ -180,13 +177,13 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds)
if (*p == '-')
{
expSign = TRUE;
p ++;
expNeg = -1;
p ++;
}
else
{
if (*p == '+')
p ++;
expSign = FALSE;
}

for (;;)
Expand Down Expand Up @@ -214,18 +211,18 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds)

if (frac >> 31)
{
return ds->dec->newLong( (JSINT64) frac * (JSINT64) neg);
return ds->dec->newLong( (JSINT64) frac * (JSINT64) fracNeg);
}
else
{
return ds->dec->newInt( (JSINT32) frac * (JSINT32) neg);
return ds->dec->newInt( (JSINT32) frac * (JSINT32) fracNeg);
}
}
}

END_EXPONENT_LOOP:

fraction = frac;
fraction = (double) frac;

if (decPt == -1)
{
Expand All @@ -234,16 +231,9 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds)

fracExp = decPt - mantSize;

if (expSign)
{
exp = fracExp - exp;
}
else
{
exp = fracExp + exp;
}
exp = fracExp + (exp * expNeg);

/*
/*
* Generate a floating-point number that represents the exponent.
* Do this by processing the exponent one bit at a time to combine
* many powers of 2 of 10. Then combine the exponent with the
Expand Down Expand Up @@ -284,9 +274,8 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds)
fraction *= dblExp;
}

done:
ds->start = (char *) p;
return ds->dec->newDouble(fraction * (double) neg);
return ds->dec->newDouble(fraction * (double) fracNeg);
}

FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_true ( struct DecoderState *ds)
Expand Down

0 comments on commit a92339d

Please sign in to comment.