Skip to content

Commit c07b56a

Browse files
committed
Merge json_decode_float_slice in json_decode_large_float
17 digits is already a pretty large float, we can consider it as the slow path, so it's best not to inline it.
1 parent 87a6d01 commit c07b56a

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

ext/json/ext/parser/parser.c

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -791,15 +791,22 @@ static VALUE json_decode_large_integer(const char *start, long len)
791791
static inline VALUE
792792
json_decode_integer(const char *start, const char *end)
793793
{
794-
long len = end - start;
795-
if (RB_LIKELY(len < MAX_FAST_INTEGER_SIZE)) {
796-
return fast_decode_integer(start, end);
797-
}
798-
return json_decode_large_integer(start, len);
794+
long len = end - start;
795+
if (RB_LIKELY(len < MAX_FAST_INTEGER_SIZE)) {
796+
return fast_decode_integer(start, end);
797+
}
798+
return json_decode_large_integer(start, len);
799799
}
800800

801801
static VALUE json_decode_large_float(const char *start, long len)
802802
{
803+
if (RB_LIKELY(len < 64)) {
804+
char buffer[64];
805+
MEMCPY(buffer, start, char, len);
806+
buffer[len] = '\0';
807+
return DBL2NUM(rb_cstr_to_dbl(buffer, 1));
808+
}
809+
803810
VALUE buffer_v;
804811
char *buffer = RB_ALLOCV_N(char, buffer_v, len + 1);
805812
MEMCPY(buffer, start, char, len);
@@ -809,21 +816,6 @@ static VALUE json_decode_large_float(const char *start, long len)
809816
return number;
810817
}
811818

812-
/* Helper: Parse float from string slice using rb_cstr_to_dbl
813-
* Handles small strings efficiently with stack buffer
814-
*/
815-
static inline VALUE json_decode_float_slice(const char *start, long len)
816-
{
817-
if (RB_LIKELY(len < 64)) {
818-
char buffer[64];
819-
MEMCPY(buffer, start, char, len);
820-
buffer[len] = '\0';
821-
return DBL2NUM(rb_cstr_to_dbl(buffer, 1));
822-
} else {
823-
return json_decode_large_float(start, len);
824-
}
825-
}
826-
827819
/* Ruby JSON optimized float decoder using vendored Ryu algorithm
828820
* Accepts pre-extracted mantissa and exponent from first-pass validation
829821
*/
@@ -834,7 +826,7 @@ static inline VALUE json_ryu_parse_float(uint64_t m10, int m10digits, int32_t e1
834826
// Ryu has rounding issues with subnormals around 1e-310 (< 2.225e-308)
835827
if (m10digits + e10 < -307) {
836828
// Use original string directly via shared helper
837-
return json_decode_float_slice(start, end - start);
829+
return json_decode_large_float(start, end - start);
838830
}
839831

840832
double result = ryu_s2d_from_parts(m10, m10digits, e10, signedM);
@@ -849,7 +841,7 @@ static VALUE json_decode_float(JSON_ParserConfig *config, const char *start, con
849841
VALUE text = rb_str_new(start, len);
850842
return rb_funcallv(config->decimal_class, config->decimal_method_id, 1, &text);
851843
} else {
852-
return json_decode_float_slice(start, len);
844+
return json_decode_large_float(start, len);
853845
}
854846
}
855847

0 commit comments

Comments
 (0)