1
0
mirror of https://github.com/ultrajson/ultrajson.git synced 2024-11-24 05:12:02 +01:00

- Optimized decoder by not always creating 64 bit values when decoded values could be held inside 32-bit value (int)

This commit is contained in:
Jonas Tärnström 2011-03-08 02:35:43 +01:00
parent 86d016e729
commit a4a451140b
4 changed files with 28 additions and 23 deletions

@ -334,18 +334,10 @@
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\python\JSONtoObj.c"
>
</File>
<File
RelativePath=".\main.cpp"
>
</File>
<File
RelativePath="..\python\objToJSON.c"
>
</File>
</Filter>
<Filter
Name="Header Files"

@ -384,68 +384,68 @@ public:
JSOBJ Object_newString(char *start, char *end)
{
//return (JSOBJ) 1;
/**/return (JSOBJ) 1;
return (JSOBJ) new StringObject(start, end);
}
void Object_objectAddKey(JSOBJ obj, JSOBJ name, JSOBJ value)
{
//return;
/**/return;
((MapObject *)obj)->addKey( (StringObject *) name, (BaseObject *) value);
}
void Object_arrayAddItem(JSOBJ obj, JSOBJ value)
{
//return;
/**/return;
((ListObject *)obj)->addItem( (ListObject *) value);
}
JSOBJ Object_newTrue()
{
//return (JSOBJ) 1;
/**/return (JSOBJ) 1;
return (JSOBJ) new BoolObject(true, JT_TRUE);
}
JSOBJ Object_newFalse()
{
//return (JSOBJ) 1;
/**/return (JSOBJ) 1;
return (JSOBJ) new BoolObject(false, JT_FALSE);
}
JSOBJ Object_newNull()
{
//return (JSOBJ) 1;
/**/return (JSOBJ) 1;
return (JSOBJ) new NullObject();
}
JSOBJ Object_newObject()
{
//return (JSOBJ) 1;
/**/return (JSOBJ) 1;
return (JSOBJ) new MapObject();
}
JSOBJ Object_newArray()
{
//return (JSOBJ) 1;
/**/return (JSOBJ) 1;
return (JSOBJ) new ListObject();
}
JSOBJ Object_newLong(JSINT64 value)
{
//return (JSOBJ) 1;
/**/return (JSOBJ) 1;
return (JSOBJ) new LongObject(value);
}
JSOBJ Object_newInteger(JSINT32 value)
{
//return (JSOBJ) 1;
/**/return (JSOBJ) 1;
return (JSOBJ) new IntegerObject(value);
}
JSOBJ Object_newDouble(double value)
{
//return (JSOBJ) 1;
/**/return (JSOBJ) 1;
return (JSOBJ) new DoubleObject(value);
}
@ -566,9 +566,11 @@ int main (int argc, char **argv)
{
for (int index = 0; index < N; index ++)
{
JSON_EncodeObject(obj, &encoder, buffer, sizeof (buffer));
//JSON_EncodeObject(obj, &encoder, buffer, sizeof (buffer));
//obj->decRef();
obj = (BaseObject *) JSON_DecodeObject (&decoder, indata, sizeof (indata));
//obj->print(0, stderr);
//obj->decRef();
//obj = (BaseObject *) JSON_DecodeObject (&decoder, buffer, strlen(buffer));
}
cCount += 1;

Binary file not shown.

@ -38,6 +38,7 @@ Copyright (c) 2007 Nick Galbreath -- nickg [at] modp [dot] com. All rights rese
#include <math.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
struct DecoderState
{
@ -58,7 +59,6 @@ PFN_DECODER g_identTable[256] = { NULL };
/*
FIXME: Maybe move this to inside of createDouble function. Might increase memory locality and worst case
possibly polute the global namespace less */
static const double g_pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
/*
@ -75,6 +75,8 @@ static const double g_pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000
double createDouble(double intNeg, double intValue, double frcValue, int frcDecimalCount)
{
static const double g_pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
return (intValue + (frcValue / g_pow10[frcDecimalCount])) * intNeg;
}
@ -173,7 +175,16 @@ BREAK_INT_LOOP:
//FIXME: Check value size here, don't decode everything as 64-bit
RETURN_JSOBJ_NULLCHECK(ds->dec->newLong( (JSINT64) (intValue * intNeg)));
if (intValue > (INT_MAX - 1))
{
RETURN_JSOBJ_NULLCHECK(ds->dec->newLong( (JSINT64) (intValue * intNeg)));
}
else
{
RETURN_JSOBJ_NULLCHECK(ds->dec->newInt( (JSINT32) (intValue * intNeg)));
}
DECODE_FRACTION: