Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/modgc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ jobs:
echo "TESTS=${TESTS}" >> $GITHUB_ENV
if: ${{ matrix.test_task == 'check' && matrix.skipped_tests }}

- name: Set timeout scale
run: echo "RUBY_TEST_TIMEOUT_SCALE=10" >> $GITHUB_ENV # subprocess assertion timeouts flake on slower/contended macOS runners, especially with the slower MMTk GC
if: ${{ contains(matrix.os, 'macos') }}

- name: Set up Launchable
id: launchable
uses: ./.github/actions/launchable/setup
Expand Down
7 changes: 3 additions & 4 deletions LEGAL
Original file line number Diff line number Diff line change
Expand Up @@ -714,10 +714,9 @@ mentioned below.

{MIT License}[rdoc-ref:@MIT+License]

[ext/json/vendor/ryu.h]
This file is adapted from the Ryu algorithm by Ulf Adams https://github.com/ulfjack/ryu.
It is dual-licensed under {Apache License 2.0}[rdoc-ref:@Apache+License+2.0] OR
{Boost Software License 1.0}[rdoc-ref:@Boost+Software+License+1.0].
[ext/json/vendor/fast_float_parser.h]
This file is adapted from the Fast Float C++ library by The fast_float authors https://github.com/fastfloat/fast_float
It is licensed under the MIT License

[ext/psych]
[test/psych]
Expand Down
2 changes: 1 addition & 1 deletion ext/json/parser/depend
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,6 @@ parser.o: $(hdrdir)/ruby/subst.h
parser.o: $(srcdir)/../fbuffer/fbuffer.h
parser.o: $(srcdir)/../json.h
parser.o: $(srcdir)/../simd/simd.h
parser.o: $(srcdir)/../vendor/ryu.h
parser.o: $(srcdir)/../vendor/fast_float_parser.h
parser.o: parser.c
# AUTOGENERATED DEPENDENCIES END
25 changes: 25 additions & 0 deletions ext/json/parser/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@
have_func("rb_hash_bulk_insert", "ruby.h") # Missing on TruffleRuby
have_func("ruby_xfree_sized", "ruby.h") # RUBY_VERSION >= 4.1

def have_builtin_func(name, check_expr, opt = "", &b)
checking_for checking_message(name.funcall_style, nil, opt) do
if try_compile(<<SRC, opt, &b)
int foo;
int main() { #{check_expr}; return 0; }
SRC
$defs.push(format("-DHAVE_BUILTIN_%s", name.tr_cpp))
true
else
false
end
end
end

have_builtin_func("__builtin_clzll", "__builtin_clzll(0)")

if have_header("x86intrin.h")
have_func("_lzcnt_u64", "x86intrin.h")
end

if have_header("intrin.h")
have_func("__lzcnt64", "intrin.h")
have_func("_BitScanReverse64", "intrin.h")
end

if RUBY_ENGINE == "ruby"
have_const("RUBY_TYPED_EMBEDDABLE", "ruby.h") # RUBY_VERSION >= 3.3
end
Expand Down
43 changes: 19 additions & 24 deletions ext/json/parser/parser.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../json.h"
#include "../vendor/ryu.h"
#include "../vendor/fast_float_parser.h"
#include "../simd/simd.h"

static VALUE mJSON, eNestingError, eParserError, Encoding_UTF_8;
Expand Down Expand Up @@ -1111,13 +1111,11 @@ static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantis
return rb_float_new(negative ? -0.0 : 0.0);
}

// Fall back to rb_cstr_to_dbl for potential subnormals (rare edge case)
// Ryu has rounding issues with subnormals around 1e-310 (< 2.225e-308)
if (RB_UNLIKELY(mantissa_digits > 17 || mantissa_digits + exponent < -307)) {
if (RB_UNLIKELY(mantissa_digits > 18 || mantissa_digits + exponent < -307)) {
return json_decode_large_float(start, end - start);
}

return DBL2NUM(ryu_s2d_from_parts(mantissa, mantissa_digits, (int32_t)exponent, negative));
return DBL2NUM(ffp_s2d(exponent, mantissa, negative));
}

static inline VALUE json_decode_array(JSON_ParserState *state, JSON_ParserConfig *config, long count)
Expand Down Expand Up @@ -1543,6 +1541,8 @@ ALWAYS_INLINE(static) bool json_parse_any(JSON_ParserState *state, JSON_ParserCo
json_eat_whitespace(state, config);

VALUE value;
const char *value_start = state->cursor;

switch (peek(state)) {
case 'n':
json_match_keyword(state, "null", 0);
Expand Down Expand Up @@ -1578,13 +1578,12 @@ ALWAYS_INLINE(static) bool json_parse_any(JSON_ParserState *state, JSON_ParserCo
break;

case '-': {
const char *start = state->cursor;
state->cursor++;

value = json_parse_number(state, config, true, start);
value = json_parse_number(state, config, true, value_start);

if (RB_UNLIKELY(UNDEF_P(value) && config->allow_nan && peek(state) == 'I')) {
state->cursor = start;
state->cursor = value_start;
json_match_keyword(state, "-Infinity", 1);
value = CMinusInfinity;
break;
Expand All @@ -1593,51 +1592,49 @@ ALWAYS_INLINE(static) bool json_parse_any(JSON_ParserState *state, JSON_ParserCo
// Top level numbers are ambiguous when parsing streams, we can't
// know if we parsed all the digits if we hit EOS.
if (RB_UNLIKELY(resumable && eos(state))) {
state->cursor = start;
state->cursor = value_start;
return false;
}

if (RB_UNLIKELY(UNDEF_P(value))) {
raise_syntax_error_at("invalid number: %s", state, start);
raise_syntax_error_at("invalid number: %s", state, value_start);
}
break;
}

case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': {
const char *start = state->cursor;

value = json_parse_number(state, config, false, start);
value = json_parse_number(state, config, false, value_start);

// Top level numbers are ambiguous when parsing streams, we can't
// know if we parsed all the digits if we hit EOS.
if (RB_UNLIKELY(resumable && eos(state))) {
state->cursor = start;
state->cursor = value_start;
return false;
}

if (RB_UNLIKELY(UNDEF_P(value))) {
raise_syntax_error_at("invalid number: %s", state, start);
raise_syntax_error_at("invalid number: %s", state, value_start);
}
break;
}

case '"': {
// %r{\A"[^"\\\t\n\x00]*(?:\\[bfnrtu\\/"][^"\\]*)*"}
const char *start = state->cursor;
value = json_parse_string(state, config, false);

if (RB_UNLIKELY(UNDEF_P(value))) {
bool is_eos = eos(state);
if (resumable) {
state->cursor = start;
if (resumable && is_eos) {
state->cursor = value_start;
return false;
}
raise_parse_error("unexpected end of input, expected closing \"", state, is_eos);
}
break;
}

case '[': {
const char *start = state->cursor++;
state->cursor++;
json_eat_whitespace(state, config);

const char next = peek(state);
Expand All @@ -1646,7 +1643,7 @@ ALWAYS_INLINE(static) bool json_parse_any(JSON_ParserState *state, JSON_ParserCo
value = json_decode_array(state, config, 0);
break;
} else if (resumable && next == 0) {
state->cursor = start;
state->cursor = value_start;
return false;
}

Expand All @@ -1666,8 +1663,6 @@ ALWAYS_INLINE(static) bool json_parse_any(JSON_ParserState *state, JSON_ParserCo
}

case '{': {
const char *object_start_cursor = state->cursor;

state->cursor++;
json_eat_whitespace(state, config);

Expand All @@ -1676,7 +1671,7 @@ ALWAYS_INLINE(static) bool json_parse_any(JSON_ParserState *state, JSON_ParserCo
value = json_decode_object(state, config, 0);
break;
} else if (resumable && eos(state)) {
state->cursor = object_start_cursor;
state->cursor = value_start;
return false;
}

Expand All @@ -1690,7 +1685,7 @@ ALWAYS_INLINE(static) bool json_parse_any(JSON_ParserState *state, JSON_ParserCo
.type = JSON_FRAME_OBJECT,
.phase = JSON_PHASE_OBJECT_KEY,
.value_stack_head = state->value_stack->head,
.start_offset = object_start_cursor - state->start,
.start_offset = value_start - state->start,
});
goto JSON_PHASE_OBJECT_KEY;
}
Expand Down
Loading