akheron / jansson

C library for encoding, decoding and manipulating JSON data
http://www.digip.org/jansson/
Other
3.05k stars 809 forks source link

add integer cache #559

Open irov opened 3 years ago

irov commented 3 years ago

Hello!

maybe add integer cache like python?

json_t *json_integer(json_int_t value) {
    static json_integer_t the_integers[512] = {JSON_INTEGER, (size_t)-1, 0};
    if (value <= 256 && value >= -255) {
        json_integer_t *integer = the_integers + value + 255;
        integer->value = value;
        return (json_t *)integer;
    }
    json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t));
    if (!integer)
        return NULL;
    json_init(&integer->json, JSON_INTEGER);

    integer->value = value;
    return &integer->json;
}
kennyk-peplink commented 3 years ago

How could that be justified to have them being cached?

Could that be a dynamically sized so we get a smaller footprint if we don't work on negative domain?

irov commented 3 years ago

How could that be justified to have them being cached?

I don't quite understand the question

Could that be a dynamically sized so we get a smaller footprint if we don't work on negative domain?

Maybe add define for min & max cache?

#ifndef JSON_INTEGER_CACHE_MIN
#define JSON_INTEGER_CACHE_MIN -255
#endif