Skip to content

Commit

Permalink
Refactor converting number to string by using optimized function (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
Watson1978 committed Jun 16, 2023
1 parent c86e60d commit 4bfb4b5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 44 deletions.
13 changes: 3 additions & 10 deletions ext/oj/code.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,17 @@ void oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out, bool with_class) {
} else {
char buf[32];
char *b = buf + sizeof(buf) - 1;
int neg = 0;
bool neg = false;
long num = attrs->num;
size_t cnt = 0;

if (0 > num) {
neg = 1;
neg = true;
num = -num;
}
*b-- = '\0';
if (0 < num) {
for (; 0 < num; num /= 10, b--) {
*b = (num % 10) + '0';
}
if (neg) {
*b = '-';
} else {
b++;
}
b = oj_longlong_to_string(num, neg, b);
} else {
*b = '0';
}
Expand Down
45 changes: 25 additions & 20 deletions ext/oj/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1010,11 +1010,33 @@ static const char digits_table[] = "\
80818283848586878889\
90919293949596979899";

char *oj_longlong_to_string(long long num, bool negative, char *buf) {
while (100 <= num) {
unsigned idx = num % 100 * 2;
*buf-- = digits_table[idx + 1];
*buf-- = digits_table[idx];
num /= 100;
}
if (num < 10) {
*buf-- = num + '0';
} else {
*buf-- = digits_table[num * 2 + 1];
*buf-- = digits_table[num * 2];
}

if (negative) {
*buf = '-';
} else {
buf++;
}
return buf;
}

void oj_dump_fixnum(VALUE obj, int depth, Out out, bool as_ok) {
char buf[32];
char *b = buf + sizeof(buf) - 1;
long long num = NUM2LL(obj);
int neg = 0;
bool neg = false;
size_t cnt = 0;
bool dump_as_string = false;

Expand All @@ -1023,7 +1045,7 @@ void oj_dump_fixnum(VALUE obj, int depth, Out out, bool as_ok) {
dump_as_string = true;
}
if (0 > num) {
neg = 1;
neg = true;
num = -num;
}
*b-- = '\0';
Expand All @@ -1032,24 +1054,7 @@ void oj_dump_fixnum(VALUE obj, int depth, Out out, bool as_ok) {
*b-- = '"';
}
if (0 < num) {
while (100 <= num) {
unsigned idx = num % 100 * 2;
*b-- = digits_table[idx + 1];
*b-- = digits_table[idx];
num /= 100;
}
if (num < 10) {
*b-- = num + '0';
} else {
*b-- = digits_table[num * 2 + 1];
*b-- = digits_table[num * 2];
}

if (neg) {
*b = '-';
} else {
b++;
}
b = oj_longlong_to_string(num, neg, b);
} else {
*b = '0';
}
Expand Down
5 changes: 1 addition & 4 deletions ext/oj/dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ inline static void dump_ulong(unsigned long num, Out out) {

*b-- = '\0';
if (0 < num) {
for (; 0 < num; num /= 10, b--) {
*b = (num % 10) + '0';
}
b++;
b = oj_longlong_to_string((long long)num, false, b);
} else {
*b = '0';
}
Expand Down
5 changes: 1 addition & 4 deletions ext/oj/fast.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ inline static char *ulong_fill(char *s, size_t num) {
char *b = buf + sizeof(buf) - 1;

*b-- = '\0';
for (; 0 < num; num /= 10, b--) {
*b = (num % 10) + '0';
}
b++;
b = oj_longlong_to_string((long long)num, false, b);
if ('\0' == *b) {
b--;
*b = '0';
Expand Down
13 changes: 7 additions & 6 deletions ext/oj/oj.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,13 @@ extern VALUE oj_custom_parse_cstr(int argc, VALUE *argv, char *json, size_t len)
extern bool oj_hash_has_key(VALUE hash, VALUE key);
extern void oj_parse_options(VALUE ropts, Options copts);

extern void oj_dump_obj_to_json(VALUE obj, Options copts, Out out);
extern void oj_dump_obj_to_json_using_params(VALUE obj, Options copts, Out out, int argc, VALUE *argv);
extern void oj_write_obj_to_file(VALUE obj, const char *path, Options copts);
extern void oj_write_obj_to_stream(VALUE obj, VALUE stream, Options copts);
extern void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out);
extern void oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts);
extern void oj_dump_obj_to_json(VALUE obj, Options copts, Out out);
extern void oj_dump_obj_to_json_using_params(VALUE obj, Options copts, Out out, int argc, VALUE *argv);
extern void oj_write_obj_to_file(VALUE obj, const char *path, Options copts);
extern void oj_write_obj_to_stream(VALUE obj, VALUE stream, Options copts);
extern void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out);
extern void oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts);
extern char *oj_longlong_to_string(long long num, bool negative, char *buf);

extern void oj_str_writer_push_key(StrWriter sw, const char *key);
extern void oj_str_writer_push_object(StrWriter sw, const char *key);
Expand Down

0 comments on commit 4bfb4b5

Please sign in to comment.