Skip to content

Commit f68e7bc

Browse files
committed
Faster integer formatting
This commit provides an alternative implementation for a long → decimal conversion. The main difference is that it uses an algorithm pulled from https://github.com/jeaiii/itoa. The source there is C++, it was converted by hand to C for inclusion with this gem. A writeup of this algorithm can be found here. jeaiii's algorithm is covered by the MIT License, see source code. On addition this version now also generates the string directly into the fbuffer, foregoing the need to run a separate memory copy. As a result, I see a speedup of 32% on Apple Silicon M1 for an integer set of benchmarks.
1 parent 2074f0c commit f68e7bc

File tree

4 files changed

+336
-9
lines changed

4 files changed

+336
-9
lines changed

ext/json/ext/fbuffer/fbuffer.h

+45-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "ruby.h"
55
#include "ruby/encoding.h"
6+
#include "../vendor/jeaiii/ltoa.h"
67

78
/* shims */
89
/* This is the fallback definition from Ruby 3.4 */
@@ -150,6 +151,18 @@ static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
150151
}
151152
}
152153

154+
static inline void fbuffer_append_reserved(FBuffer *fb, const char *newstr, unsigned long len)
155+
{
156+
MEMCPY(fb->ptr + fb->len, newstr, char, len);
157+
fb->len += len;
158+
}
159+
160+
static inline void fbuffer_append_reserved_char(FBuffer *fb, char chr)
161+
{
162+
fb->ptr[fb->len] = chr;
163+
fb->len += 1;
164+
}
165+
153166
static void fbuffer_append_str(FBuffer *fb, VALUE str)
154167
{
155168
const char *newstr = StringValuePtr(str);
@@ -167,25 +180,48 @@ static inline void fbuffer_append_char(FBuffer *fb, char newchr)
167180
fb->len++;
168181
}
169182

170-
static long fltoa(long number, char *buf)
183+
static inline long fultoa(unsigned long number, char *buf)
171184
{
172185
static const char digits[] = "0123456789";
173-
long sign = number;
174186
char* tmp = buf;
175187

176-
if (sign < 0) number = -number;
177188
do *tmp-- = digits[number % 10]; while (number /= 10);
178-
if (sign < 0) *tmp-- = '-';
179189
return buf - tmp;
180190
}
181191

182-
#define LONG_BUFFER_SIZE 20
192+
#define MAX_CHARS_FOR_LONG 20
193+
194+
/*
195+
* Appends the decimal string representation of \a number into the buffer.
196+
*/
183197
static void fbuffer_append_long(FBuffer *fb, long number)
184198
{
185-
char buf[LONG_BUFFER_SIZE];
186-
char *buffer_end = buf + LONG_BUFFER_SIZE;
187-
long len = fltoa(number, buffer_end - 1);
188-
fbuffer_append(fb, buffer_end - len, len);
199+
/*
200+
* The to_text_from_ulong implementation produces digits left-to-right,
201+
* allowing us to write directly into the buffer. However, we don't know
202+
* how many characters we'll need exactly.
203+
*
204+
* However, the number argument is always in the range 0xc000000000000000
205+
* to 0x3fffffffffffffff, or, in decimal, -4611686018427387904 to
206+
* 4611686018427387903. We therefore need at most 20 chars in the target
207+
* buffer.
208+
*/
209+
210+
fbuffer_inc_capa(fb, MAX_CHARS_FOR_LONG);
211+
212+
if (number < 0) {
213+
fbuffer_append_reserved_char(fb, '-');
214+
215+
/*
216+
* Since LONG_MIN is not in the valid range, `number = -number` always turns
217+
* a negative number into the positive.
218+
*/
219+
number = -number;
220+
}
221+
222+
char* d = fb->ptr + fb->len;
223+
char* end = to_text_from_ulong(d, number);
224+
fb->len += end - d;
189225
}
190226

191227
static VALUE fbuffer_finalize(FBuffer *fb)

ext/json/ext/generator/generator.c

+1
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,7 @@ void Init_generator(void)
16921692
cFragment = rb_const_get(mJSON, rb_intern("Fragment"));
16931693

16941694
VALUE mExt = rb_define_module_under(mJSON, "Ext");
1695+
16951696
VALUE mGenerator = rb_define_module_under(mExt, "Generator");
16961697

16971698
rb_global_variable(&eGeneratorError);

ext/json/ext/vendor/jeaiii/ltoa.h

+278
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
/*
2+
3+
Copyright (c) 2024,2025 Enrico Thierbach - https://github.com/radiospiel
4+
5+
This file is released under the terms of the MIT License. It is based on the
6+
work of James Edward Anhalt III, with the original license listed below.
7+
8+
MIT License
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy
11+
of this software and associated documentation files (the "Software"), to deal
12+
in the Software without restriction, including without limitation the rights
13+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
copies of the Software, and to permit persons to whom the Software is
15+
furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in all
18+
copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
SOFTWARE.
27+
*/
28+
29+
/*
30+
MIT License
31+
32+
Copyright (c) 2022 James Edward Anhalt III - https://github.com/jeaiii/itoa
33+
34+
Permission is hereby granted, free of charge, to any person obtaining a copy
35+
of this software and associated documentation files (the "Software"), to deal
36+
in the Software without restriction, including without limitation the rights
37+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38+
copies of the Software, and to permit persons to whom the Software is
39+
furnished to do so, subject to the following conditions:
40+
41+
The above copyright notice and this permission notice shall be included in all
42+
copies or substantial portions of the Software.
43+
44+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
50+
SOFTWARE.
51+
*/
52+
53+
#ifndef JEAIII_TO_TEXT_H_
54+
#define JEAIII_TO_TEXT_H_
55+
56+
typedef unsigned int u32_t;
57+
typedef unsigned long u64_t;
58+
59+
#pragma clang diagnostic push
60+
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
61+
62+
#pragma GCC diagnostic push
63+
#pragma GCC diagnostic ignored "-Wmissing-braces"
64+
65+
#define u32(x) ((u32_t)(x))
66+
#define u64(x) ((u64_t)(x))
67+
68+
struct pair
69+
{
70+
char dd[2];
71+
};
72+
73+
#define cast_to_pair_ptr(b) ((struct pair*)(void*)(b))
74+
75+
static struct pair digits_dd[100] =
76+
{
77+
{ '0', '0' }, { '0', '1' }, { '0', '2' }, { '0', '3' }, { '0', '4' }, { '0', '5' }, { '0', '6' }, { '0', '7' }, { '0', '8' }, { '0', '9' },
78+
{ '1', '0' }, { '1', '1' }, { '1', '2' }, { '1', '3' }, { '1', '4' }, { '1', '5' }, { '1', '6' }, { '1', '7' }, { '1', '8' }, { '1', '9' },
79+
{ '2', '0' }, { '2', '1' }, { '2', '2' }, { '2', '3' }, { '2', '4' }, { '2', '5' }, { '2', '6' }, { '2', '7' }, { '2', '8' }, { '2', '9' },
80+
{ '3', '0' }, { '3', '1' }, { '3', '2' }, { '3', '3' }, { '3', '4' }, { '3', '5' }, { '3', '6' }, { '3', '7' }, { '3', '8' }, { '3', '9' },
81+
{ '4', '0' }, { '4', '1' }, { '4', '2' }, { '4', '3' }, { '4', '4' }, { '4', '5' }, { '4', '6' }, { '4', '7' }, { '4', '8' }, { '4', '9' },
82+
{ '5', '0' }, { '5', '1' }, { '5', '2' }, { '5', '3' }, { '5', '4' }, { '5', '5' }, { '5', '6' }, { '5', '7' }, { '5', '8' }, { '5', '9' },
83+
{ '6', '0' }, { '6', '1' }, { '6', '2' }, { '6', '3' }, { '6', '4' }, { '6', '5' }, { '6', '6' }, { '6', '7' }, { '6', '8' }, { '6', '9' },
84+
{ '7', '0' }, { '7', '1' }, { '7', '2' }, { '7', '3' }, { '7', '4' }, { '7', '5' }, { '7', '6' }, { '7', '7' }, { '7', '8' }, { '7', '9' },
85+
{ '8', '0' }, { '8', '1' }, { '8', '2' }, { '8', '3' }, { '8', '4' }, { '8', '5' }, { '8', '6' }, { '8', '7' }, { '8', '8' }, { '8', '9' },
86+
{ '9', '0' }, { '9', '1' }, { '9', '2' }, { '9', '3' }, { '9', '4' }, { '9', '5' }, { '9', '6' }, { '9', '7' }, { '9', '8' }, { '9', '9' },
87+
};
88+
89+
#define NUL 'x'
90+
91+
static struct pair digits_fd[100] =
92+
{
93+
{ '0', NUL }, { '1', NUL }, { '2', NUL }, { '3', NUL }, { '4', NUL }, { '5', NUL }, { '6', NUL }, { '7', NUL }, { '8', NUL }, { '9', NUL },
94+
{ '1', '0' }, { '1', '1' }, { '1', '2' }, { '1', '3' }, { '1', '4' }, { '1', '5' }, { '1', '6' }, { '1', '7' }, { '1', '8' }, { '1', '9' },
95+
{ '2', '0' }, { '2', '1' }, { '2', '2' }, { '2', '3' }, { '2', '4' }, { '2', '5' }, { '2', '6' }, { '2', '7' }, { '2', '8' }, { '2', '9' },
96+
{ '3', '0' }, { '3', '1' }, { '3', '2' }, { '3', '3' }, { '3', '4' }, { '3', '5' }, { '3', '6' }, { '3', '7' }, { '3', '8' }, { '3', '9' },
97+
{ '4', '0' }, { '4', '1' }, { '4', '2' }, { '4', '3' }, { '4', '4' }, { '4', '5' }, { '4', '6' }, { '4', '7' }, { '4', '8' }, { '4', '9' },
98+
{ '5', '0' }, { '5', '1' }, { '5', '2' }, { '5', '3' }, { '5', '4' }, { '5', '5' }, { '5', '6' }, { '5', '7' }, { '5', '8' }, { '5', '9' },
99+
{ '6', '0' }, { '6', '1' }, { '6', '2' }, { '6', '3' }, { '6', '4' }, { '6', '5' }, { '6', '6' }, { '6', '7' }, { '6', '8' }, { '6', '9' },
100+
{ '7', '0' }, { '7', '1' }, { '7', '2' }, { '7', '3' }, { '7', '4' }, { '7', '5' }, { '7', '6' }, { '7', '7' }, { '7', '8' }, { '7', '9' },
101+
{ '8', '0' }, { '8', '1' }, { '8', '2' }, { '8', '3' }, { '8', '4' }, { '8', '5' }, { '8', '6' }, { '8', '7' }, { '8', '8' }, { '8', '9' },
102+
{ '9', '0' }, { '9', '1' }, { '9', '2' }, { '9', '3' }, { '9', '4' }, { '9', '5' }, { '9', '6' }, { '9', '7' }, { '9', '8' }, { '9', '9' },
103+
};
104+
105+
#undef NUL
106+
107+
static unsigned long mask24 = (u64(1) << 24) - 1;
108+
static unsigned long mask32 = (u64(1) << 32) - 1;
109+
static unsigned long mask57 = (u64(1) << 57) - 1;
110+
111+
static
112+
char* to_text_from_ulong(char* b, u64_t n) {
113+
if (n < u32(1e2))
114+
{
115+
*cast_to_pair_ptr(b) = digits_fd[n];
116+
return n < 10 ? b + 1 : b + 2;
117+
}
118+
if (n < u32(1e6))
119+
{
120+
if (n < u32(1e4))
121+
{
122+
u32_t f0 = u32(10 * (1 << 24) / 1e3 + 1) * n;
123+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 24];
124+
b -= n < u32(1e3);
125+
u32_t f2 = (f0 & mask24) * 100;
126+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 24];
127+
return b + 4;
128+
}
129+
u64_t f0 = u64(10 * (1ull << 32ull)/ 1e5 + 1) * n;
130+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 32];
131+
b -= n < u32(1e5);
132+
u64_t f2 = (f0 & mask32) * 100;
133+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32];
134+
u64_t f4 = (f2 & mask32) * 100;
135+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32];
136+
return b + 6;
137+
}
138+
if (n < u64(1ull << 32ull))
139+
{
140+
if (n < u32(1e8))
141+
{
142+
u64_t f0 = u64(10 * (1ull << 48ull) / 1e7 + 1) * n >> 16;
143+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 32];
144+
b -= n < u32(1e7);
145+
u64_t f2 = (f0 & mask32) * 100;
146+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32];
147+
u64_t f4 = (f2 & mask32) * 100;
148+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32];
149+
u64_t f6 = (f4 & mask32) * 100;
150+
*cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 32];
151+
return b + 8;
152+
}
153+
u64_t f0 = u64(10 * (1ull << 57ull) / 1e9 + 1) * n;
154+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 57];
155+
b -= n < u32(1e9);
156+
u64_t f2 = (f0 & mask57) * 100;
157+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 57];
158+
u64_t f4 = (f2 & mask57) * 100;
159+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 57];
160+
u64_t f6 = (f4 & mask57) * 100;
161+
*cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 57];
162+
u64_t f8 = (f6 & mask57) * 100;
163+
*cast_to_pair_ptr(b + 8) = digits_dd[f8 >> 57];
164+
return b + 10;
165+
}
166+
167+
// if we get here U must be u64 but some compilers don't know that, so reassign n to a u64 to avoid warnings
168+
u32_t z = n % u32(1e8);
169+
u64_t u = n / u32(1e8);
170+
171+
if (u < u32(1e2))
172+
{
173+
// u can't be 1 digit (if u < 10 it would have been handled above as a 9 digit 32bit number)
174+
*cast_to_pair_ptr(b) = digits_dd[u];
175+
b += 2;
176+
}
177+
else if (u < u32(1e6))
178+
{
179+
if (u < u32(1e4))
180+
{
181+
u32_t f0 = u32(10 * (1 << 24) / 1e3 + 1) * u;
182+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 24];
183+
b -= u < u32(1e3);
184+
u32_t f2 = (f0 & mask24) * 100;
185+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 24];
186+
b += 4;
187+
}
188+
else
189+
{
190+
u64_t f0 = u64(10 * (1ull << 32ull) / 1e5 + 1) * u;
191+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 32];
192+
b -= u < u32(1e5);
193+
u64_t f2 = (f0 & mask32) * 100;
194+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32];
195+
u64_t f4 = (f2 & mask32) * 100;
196+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32];
197+
b += 6;
198+
}
199+
}
200+
else if (u < u32(1e8))
201+
{
202+
u64_t f0 = u64(10 * (1ull << 48ull) / 1e7 + 1) * u >> 16;
203+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 32];
204+
b -= u < u32(1e7);
205+
u64_t f2 = (f0 & mask32) * 100;
206+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32];
207+
u64_t f4 = (f2 & mask32) * 100;
208+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32];
209+
u64_t f6 = (f4 & mask32) * 100;
210+
*cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 32];
211+
b += 8;
212+
}
213+
else if (u < u64(1ull << 32ull))
214+
{
215+
u64_t f0 = u64(10 * (1ull << 57ull) / 1e9 + 1) * u;
216+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 57];
217+
b -= u < u32(1e9);
218+
u64_t f2 = (f0 & mask57) * 100;
219+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 57];
220+
u64_t f4 = (f2 & mask57) * 100;
221+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 57];
222+
u64_t f6 = (f4 & mask57) * 100;
223+
*cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 57];
224+
u64_t f8 = (f6 & mask57) * 100;
225+
*cast_to_pair_ptr(b + 8) = digits_dd[f8 >> 57];
226+
b += 10;
227+
}
228+
else
229+
{
230+
u32_t y = u % u32(1e8);
231+
u /= u32(1e8);
232+
233+
// u is 2, 3, or 4 digits (if u < 10 it would have been handled above)
234+
if (u < u32(1e2))
235+
{
236+
*cast_to_pair_ptr(b) = digits_dd[u];
237+
b += 2;
238+
}
239+
else
240+
{
241+
u32_t f0 = u32(10 * (1 << 24) / 1e3 + 1) * u;
242+
*cast_to_pair_ptr(b) = digits_fd[f0 >> 24];
243+
b -= u < u32(1e3);
244+
u32_t f2 = (f0 & mask24) * 100;
245+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 24];
246+
b += 4;
247+
}
248+
// do 8 digits
249+
u64_t f0 = (u64((1ull << 48ull) / 1e6 + 1) * y >> 16) + 1;
250+
*cast_to_pair_ptr(b) = digits_dd[f0 >> 32];
251+
u64_t f2 = (f0 & mask32) * 100;
252+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32];
253+
u64_t f4 = (f2 & mask32) * 100;
254+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32];
255+
u64_t f6 = (f4 & mask32) * 100;
256+
*cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 32];
257+
b += 8;
258+
}
259+
// do 8 digits
260+
u64_t f0 = (u64((1ull << 48ull) / 1e6 + 1) * z >> 16) + 1;
261+
*cast_to_pair_ptr(b) = digits_dd[f0 >> 32];
262+
u64_t f2 = (f0 & mask32) * 100;
263+
*cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32];
264+
u64_t f4 = (f2 & mask32) * 100;
265+
*cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32];
266+
u64_t f6 = (f4 & mask32) * 100;
267+
*cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 32];
268+
return b + 8;
269+
}
270+
271+
#undef u32
272+
#undef u64
273+
274+
#pragma clang diagnostic pop
275+
#pragma GCC diagnostic pop
276+
277+
#endif // JEAIII_TO_TEXT_H_
278+

test/json/json_generator_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -698,4 +698,16 @@ def test_json_generate_as_json_convert_to_proc
698698
object = Object.new
699699
assert_equal object.object_id.to_json, JSON.generate(object, strict: true, as_json: :object_id)
700700
end
701+
702+
def test_numbers_of_various_sizes
703+
numbers = [
704+
0, 1, -1, 9, -9, 13, -13, 91, -91, 513, -513, 7513, -7513,
705+
17591, -17591, -4611686018427387904, 4611686018427387903,
706+
2**62, 2**63, 2**64, -(2**62), -(2**63), -(2**64)
707+
]
708+
709+
numbers.each do |number|
710+
assert_equal "[#{number}]", JSON.generate([number])
711+
end
712+
end
701713
end

0 commit comments

Comments
 (0)