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
2 changes: 1 addition & 1 deletion .github/workflows/dependabot_automerge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
id: metadata

- name: Wait for status checks
uses: lewagon/wait-on-check-action@78dd4dd5d9b337c14c3c81f79e53bf7d222435c1 # v1.6.1
uses: lewagon/wait-on-check-action@9312864dfbc9fd208e9c0417843430751c042800 # v1.7.0
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.event.pull_request.head.sha || github.sha }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/zjit-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
rustup install ${{ matrix.rust_version }} --profile minimal
rustup default ${{ matrix.rust_version }}

- uses: taiki-e/install-action@85b24a67ef0c632dfefad70b9d5ce8fddb040754 # v2.75.10
- uses: taiki-e/install-action@eea29cff9a2b68892c0845ae3e4f45fc47ee9354 # v2.75.13
with:
tool: nextest@0.9
if: ${{ matrix.test_task == 'zjit-check' }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/zjit-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ jobs:
ruby-version: '3.1'
bundler: none

- uses: taiki-e/install-action@85b24a67ef0c632dfefad70b9d5ce8fddb040754 # v2.75.10
- uses: taiki-e/install-action@eea29cff9a2b68892c0845ae3e4f45fc47ee9354 # v2.75.13
with:
tool: nextest@0.9
if: ${{ matrix.test_task == 'zjit-check' }}
Expand Down
12 changes: 12 additions & 0 deletions benchmark/float_predicate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
prelude: |
floats = [1.0, -1.0, 0.0, Float::NAN, Float::INFINITY, -Float::INFINITY]
benchmark:
float_nan?: floats.each { |f| f.nan? }
float_finite?: floats.each { |f| f.finite? }
float_infinite?: floats.each { |f| f.infinite? }
float_zero?: floats.each { |f| f.zero? }
float_positive?: floats.each { |f| f.positive? }
float_negative?: floats.each { |f| f.negative? }

loop_count: 1000000
9 changes: 9 additions & 0 deletions benchmark/integer_predicate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
prelude: |
nums = (0..9).to_a
benchmark:
integer_zero?: nums.each { |n| n.zero? }
integer_even?: nums.each { |n| n.even? }
integer_odd?: nums.each { |n| n.odd? }

loop_count: 1000000
16 changes: 12 additions & 4 deletions ext/json/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -855,21 +855,29 @@ NOINLINE(static) VALUE json_decode_large_float(const char *start, long len)
/* Ruby JSON optimized float decoder using vendored Ryu algorithm
* Accepts pre-extracted mantissa and exponent from first-pass validation
*/
static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantissa, int mantissa_digits, int32_t exponent, bool negative,
static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantissa, int mantissa_digits, int64_t exponent, bool negative,
const char *start, const char *end)
{
if (RB_UNLIKELY(config->decimal_class)) {
VALUE text = rb_str_new(start, end - start);
return rb_funcallv(config->decimal_class, config->decimal_method_id, 1, &text);
}

if (RB_UNLIKELY(exponent > INT32_MAX)) {
return negative ? CMinusInfinity : CInfinity;
}

if (RB_UNLIKELY(exponent < INT32_MIN)) {
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)) {
return json_decode_large_float(start, end - start);
}

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

static inline VALUE json_decode_array(JSON_ParserState *state, JSON_ParserConfig *config, long count)
Expand Down Expand Up @@ -1144,7 +1152,7 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
const char first_digit = *state->cursor;

// Variables for Ryu optimization - extract digits during parsing
int32_t exponent = 0;
int64_t exponent = 0;
int decimal_point_pos = -1;
uint64_t mantissa = 0;

Expand Down Expand Up @@ -1188,7 +1196,7 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
raise_parse_error_at("invalid number: %s", state, start);
}

exponent = negative_exponent ? -((int32_t)abs_exponent) : ((int32_t)abs_exponent);
exponent = negative_exponent ? -abs_exponent : abs_exponent;
}

if (integer) {
Expand Down
23 changes: 12 additions & 11 deletions pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ is_bigendian(void)
{
static int init = 0;
static int endian_value;
char *p;
const char *p;

if (init) return endian_value;
init = 1;
Expand Down Expand Up @@ -771,7 +771,7 @@ pack_pack(rb_execution_context_t *ec, VALUE ary, VALUE fmt, VALUE buffer)
/* FALL THROUGH */
case 'p': /* pointer to string */
while (len-- > 0) {
char *t = 0;
const char *t = 0;
from = NEXTFROM;
if (!NIL_P(from)) {
STR_FROM(from);
Expand Down Expand Up @@ -1001,8 +1001,8 @@ static VALUE
pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)
{
#define hexdigits ruby_hexdigits
char *s, *send;
char *p, *pend;
const char *s, *send;
const char *p, *pend;
VALUE ary, associates = Qfalse;
long len;
AVOID_CC_BUG long tmp_len;
Expand Down Expand Up @@ -1076,7 +1076,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)
if (len > send - s) len = send - s;
{
long end = len;
char *t = s + len - 1;
const char *t = s + len - 1;

while (t >= s) {
if (*t != ' ' && *t != '\0') break;
Expand All @@ -1089,7 +1089,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)

case 'Z':
{
char *t = s;
const char *t = s;

if (len > send-s) len = send-s;
while (t < s+len && *t) t++;
Expand Down Expand Up @@ -1523,7 +1523,8 @@ pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)
case 'M':
{
VALUE buf = rb_str_new(0, send - s);
char *ptr = RSTRING_PTR(buf), *ss = s;
char *ptr = RSTRING_PTR(buf);
const char *ss = s;
int csum = 0;
int c1, c2;

Expand Down Expand Up @@ -1578,7 +1579,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)
case 'P':
if (sizeof(char *) <= (size_t)(send - s)) {
VALUE tmp = Qnil;
char *t;
const char *t;

UNPACK_FETCH(&t, char *);
if (t) {
Expand All @@ -1601,7 +1602,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)
break;
else {
VALUE tmp = Qnil;
char *t;
const char *t;

UNPACK_FETCH(&t, char *);
if (t) {
Expand All @@ -1621,7 +1622,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)
if (type == 'r') {
pack_flags |= INTEGER_PACK_2COMP;
}
char *s0 = s;
const char *s0 = s;
while (len > 0 && s < send) {
if (*s & 0x80) {
s++;
Expand All @@ -1648,7 +1649,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)

case 'w':
{
char *s0 = s;
const char *s0 = s;
while (len > 0 && s < send) {
if (*s & 0x80) {
s++;
Expand Down
14 changes: 14 additions & 0 deletions test/json/json_ryu_fallback_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,18 @@ def test_invalid_numbers_rejected
end
end
end

def test_large_exponent_numbers
assert_equal Float::INFINITY, JSON.parse("1e4294967296")
assert_equal 0.0, JSON.parse("1e-4294967296")
assert_equal 0.0, JSON.parse("99999999999999999e-4294967296")
assert_equal Float::INFINITY, JSON.parse("1e4294967295")
assert_equal Float::INFINITY, JSON.parse("1e4294967297")

assert_equal -Float::INFINITY, JSON.parse("-1e4294967296")
assert_equal -0.0, JSON.parse("-1e-4294967296")
assert_equal -0.0, JSON.parse("-99999999999999999e-4294967296")
assert_equal -Float::INFINITY, JSON.parse("-1e4294967295")
assert_equal -Float::INFINITY, JSON.parse("-1e4294967297")
end
end
9 changes: 9 additions & 0 deletions zjit/src/cruby_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,18 @@ pub fn init() -> Annotations {
annotate!(rb_cInteger, "[]", inline_integer_aref);
annotate!(rb_cInteger, "to_s", types::StringExact);
annotate!(rb_cString, "to_s", inline_string_to_s, types::StringExact);
annotate!(rb_cFloat, "nan?", types::BoolExact, no_gc, leaf, elidable);
annotate!(rb_cFloat, "finite?", types::BoolExact, no_gc, leaf, elidable);
annotate!(rb_cFloat, "infinite?", types::Fixnum.union(types::NilClass), no_gc, leaf, elidable);
let thread_singleton = unsafe { rb_singleton_class(rb_cThread) };
annotate!(thread_singleton, "current", inline_thread_current, types::BasicObject, no_gc, leaf);

annotate_builtin!(rb_cInteger, "zero?", types::BoolExact);
annotate_builtin!(rb_cInteger, "even?", types::BoolExact);
annotate_builtin!(rb_cInteger, "odd?", types::BoolExact);
annotate_builtin!(rb_cFloat, "zero?", types::BoolExact);
annotate_builtin!(rb_cFloat, "positive?", types::BoolExact);
annotate_builtin!(rb_cFloat, "negative?", types::BoolExact);
annotate_builtin!(rb_mKernel, "Float", types::Float);
annotate_builtin!(rb_mKernel, "Integer", types::Integer);
// TODO(max): Annotate rb_mKernel#class as returning types::Class. Right now there is a subtle
Expand Down
Loading