Skip to content

Commit

Permalink
Using smaller buffer size for number serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgabis committed Apr 15, 2018
1 parent 387c566 commit 921da6f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions parson.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@

#define STARTING_CAPACITY 16
#define MAX_NESTING 2048
#define FLOAT_FORMAT "%1.17g"

#define FLOAT_FORMAT "%1.17g" /* do not increase precision without incresing NUM_BUF_SIZE */
#define NUM_BUF_SIZE 64 /* double printed with "%1.17g" shouldn't be longer than 25 bytes so let's be paranoid and use 64 */

#define SIZEOF_TOKEN(a) (sizeof(a) - 1)
#define SKIP_CHAR(str) ((*str)++)
Expand Down Expand Up @@ -1416,7 +1418,7 @@ JSON_Value * json_value_deep_copy(const JSON_Value *value) {
}

size_t json_serialization_size(const JSON_Value *value) {
char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
char num_buf[NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
int res = json_serialize_to_buffer_r(value, NULL, 0, 0, num_buf);
return res < 0 ? 0 : (size_t)(res + 1);
}
Expand Down Expand Up @@ -1476,7 +1478,7 @@ char * json_serialize_to_string(const JSON_Value *value) {
}

size_t json_serialization_size_pretty(const JSON_Value *value) {
char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
char num_buf[NUM_BUF_SIZE]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
int res = json_serialize_to_buffer_r(value, NULL, 0, 1, num_buf);
return res < 0 ? 0 : (size_t)(res + 1);
}
Expand Down

0 comments on commit 921da6f

Please sign in to comment.