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
50 changes: 2 additions & 48 deletions ext/json/ext/fbuffer/fbuffer.h
Original file line number Diff line number Diff line change
@@ -1,55 +1,9 @@
#ifndef _FBUFFER_H_
#define _FBUFFER_H_

#include "ruby.h"
#include "ruby/encoding.h"
#include "../json.h"
#include "../vendor/jeaiii-ltoa.h"

/* shims */
/* This is the fallback definition from Ruby 3.4 */

#ifndef RBIMPL_STDBOOL_H
#if defined(__cplusplus)
# if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
# include <cstdbool>
# endif
#elif defined(HAVE_STDBOOL_H)
# include <stdbool.h>
#elif !defined(HAVE__BOOL)
typedef unsigned char _Bool;
# define bool _Bool
# define true ((_Bool)+1)
# define false ((_Bool)+0)
# define __bool_true_false_are_defined
#endif
#endif

#ifndef NOINLINE
#if defined(__has_attribute) && __has_attribute(noinline)
#define NOINLINE() __attribute__((noinline))
#else
#define NOINLINE()
#endif
#endif

#ifndef RB_UNLIKELY
#define RB_UNLIKELY(expr) expr
#endif

#ifndef RB_LIKELY
#define RB_LIKELY(expr) expr
#endif

#ifndef MAYBE_UNUSED
# define MAYBE_UNUSED(x) x
#endif

#ifdef RUBY_DEBUG
#ifndef JSON_DEBUG
#define JSON_DEBUG RUBY_DEBUG
#endif
#endif

enum fbuffer_type {
FBUFFER_HEAP_ALLOCATED = 0,
FBUFFER_STACK_ALLOCATED = 1,
Expand Down Expand Up @@ -290,4 +244,4 @@ static VALUE fbuffer_finalize(FBuffer *fb)
}
}

#endif
#endif // _FBUFFER_H_
1 change: 1 addition & 0 deletions ext/json/ext/generator/depend
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# AUTOGENERATED DEPENDENCIES START
generator.o: $(srcdir)/../fbuffer/fbuffer.h
generator.o: $(srcdir)/../json.h
generator.o: $(srcdir)/../simd/simd.h
generator.o: $(srcdir)/../vendor/fpconv.c
generator.o: $(srcdir)/../vendor/jeaiii-ltoa.h
Expand Down
34 changes: 9 additions & 25 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ruby.h"
#include "../json.h"
#include "../fbuffer/fbuffer.h"
#include "../vendor/fpconv.c"

Expand Down Expand Up @@ -36,10 +36,6 @@ typedef struct JSON_Generator_StateStruct {
bool strict;
} JSON_Generator_State;

#ifndef RB_UNLIKELY
#define RB_UNLIKELY(cond) (cond)
#endif

static VALUE mJSON, cState, cFragment, eGeneratorError, eNestingError, Encoding_UTF_8;

static ID i_to_s, i_to_json, i_new, i_pack, i_unpack, i_create_id, i_extend, i_encode;
Expand Down Expand Up @@ -85,24 +81,18 @@ static void generate_json_fragment(FBuffer *buffer, struct generate_json_data *d

static int usascii_encindex, utf8_encindex, binary_encindex;

#ifdef RBIMPL_ATTR_NORETURN
RBIMPL_ATTR_NORETURN()
#endif
static void raise_generator_error_str(VALUE invalid_object, VALUE str)
NORETURN(static void) raise_generator_error_str(VALUE invalid_object, VALUE str)
{
rb_enc_associate_index(str, utf8_encindex);
VALUE exc = rb_exc_new_str(eGeneratorError, str);
rb_ivar_set(exc, rb_intern("@invalid_object"), invalid_object);
rb_exc_raise(exc);
}

#ifdef RBIMPL_ATTR_NORETURN
RBIMPL_ATTR_NORETURN()
#endif
#ifdef RBIMPL_ATTR_FORMAT
RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
#endif
static void raise_generator_error(VALUE invalid_object, const char *fmt, ...)
NORETURN(static void) raise_generator_error(VALUE invalid_object, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
Expand Down Expand Up @@ -137,13 +127,7 @@ typedef struct _search_state {
#endif /* HAVE_SIMD */
} search_state;

#if (defined(__GNUC__ ) || defined(__clang__))
#define FORCE_INLINE __attribute__((always_inline))
#else
#define FORCE_INLINE
#endif

static inline FORCE_INLINE void search_flush(search_state *search)
static ALWAYS_INLINE() void search_flush(search_state *search)
{
// Do not remove this conditional without profiling, specifically escape-heavy text.
// escape_UTF8_char_basic will advance search->ptr and search->cursor (effectively a search_flush).
Expand Down Expand Up @@ -186,7 +170,7 @@ static inline unsigned char search_escape_basic(search_state *search)
return 0;
}

static inline FORCE_INLINE void escape_UTF8_char_basic(search_state *search)
static ALWAYS_INLINE() void escape_UTF8_char_basic(search_state *search)
{
const unsigned char ch = (unsigned char)*search->ptr;
switch (ch) {
Expand Down Expand Up @@ -273,7 +257,7 @@ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len)

#ifdef HAVE_SIMD

static inline FORCE_INLINE char *copy_remaining_bytes(search_state *search, unsigned long vec_len, unsigned long len)
static ALWAYS_INLINE() char *copy_remaining_bytes(search_state *search, unsigned long vec_len, unsigned long len)
{
// Flush the buffer so everything up until the last 'len' characters are unflushed.
search_flush(search);
Expand All @@ -296,7 +280,7 @@ static inline FORCE_INLINE char *copy_remaining_bytes(search_state *search, unsi

#ifdef HAVE_SIMD_NEON

static inline FORCE_INLINE unsigned char neon_next_match(search_state *search)
static ALWAYS_INLINE() unsigned char neon_next_match(search_state *search)
{
uint64_t mask = search->matches_mask;
uint32_t index = trailing_zeros64(mask) >> 2;
Expand Down Expand Up @@ -410,7 +394,7 @@ static inline unsigned char search_escape_basic_neon(search_state *search)

#ifdef HAVE_SIMD_SSE2

static inline FORCE_INLINE unsigned char sse2_next_match(search_state *search)
static ALWAYS_INLINE() unsigned char sse2_next_match(search_state *search)
{
int mask = search->matches_mask;
int index = trailing_zeros(mask);
Expand All @@ -434,7 +418,7 @@ static inline FORCE_INLINE unsigned char sse2_next_match(search_state *search)
#define TARGET_SSE2
#endif

static inline TARGET_SSE2 FORCE_INLINE unsigned char search_escape_basic_sse2(search_state *search)
static inline TARGET_SSE2 ALWAYS_INLINE() unsigned char search_escape_basic_sse2(search_state *search)
{
if (RB_UNLIKELY(search->has_matches)) {
// There are more matches if search->matches_mask > 0.
Expand Down
85 changes: 85 additions & 0 deletions ext/json/ext/json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#ifndef _JSON_H_
#define _JSON_H_

#include "ruby.h"
#include "ruby/encoding.h"

#if defined(RUBY_DEBUG) && RUBY_DEBUG
# define JSON_ASSERT RUBY_ASSERT
#else
# ifdef JSON_DEBUG
# include <assert.h>
# define JSON_ASSERT(x) assert(x)
# else
# define JSON_ASSERT(x)
# endif
#endif

/* shims */

#if SIZEOF_UINT64_T == SIZEOF_LONG_LONG
# define INT64T2NUM(x) LL2NUM(x)
# define UINT64T2NUM(x) ULL2NUM(x)
#elif SIZEOF_UINT64_T == SIZEOF_LONG
# define INT64T2NUM(x) LONG2NUM(x)
# define UINT64T2NUM(x) ULONG2NUM(x)
#else
# error No uint64_t conversion
#endif

/* This is the fallback definition from Ruby 3.4 */
#ifndef RBIMPL_STDBOOL_H
#if defined(__cplusplus)
# if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
# include <cstdbool>
# endif
#elif defined(HAVE_STDBOOL_H)
# include <stdbool.h>
#elif !defined(HAVE__BOOL)
typedef unsigned char _Bool;
# define bool _Bool
# define true ((_Bool)+1)
# define false ((_Bool)+0)
# define __bool_true_false_are_defined
#endif
#endif

#ifndef NORETURN
#define NORETURN(x) x
#endif

#ifndef NOINLINE
#if defined(__has_attribute) && __has_attribute(noinline)
#define NOINLINE(x) __attribute__((noinline)) x
#else
#define NOINLINE(x) x
#endif
#endif

#ifndef ALWAYS_INLINE
#if defined(__has_attribute) && __has_attribute(always_inline)
#define ALWAYS_INLINE(x) inline __attribute__((always_inline)) x
#else
#define ALWAYS_INLINE(x) inline x
#endif
#endif

#ifndef RB_UNLIKELY
#define RB_UNLIKELY(expr) expr
#endif

#ifndef RB_LIKELY
#define RB_LIKELY(expr) expr
#endif

#ifndef MAYBE_UNUSED
# define MAYBE_UNUSED(x) x
#endif

#ifdef RUBY_DEBUG
#ifndef JSON_DEBUG
#define JSON_DEBUG RUBY_DEBUG
#endif
#endif

#endif // _JSON_H_
1 change: 1 addition & 0 deletions ext/json/ext/parser/depend
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# AUTOGENERATED DEPENDENCIES START
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: parser.c
Expand Down
49 changes: 2 additions & 47 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
@@ -1,46 +1,7 @@
#include "ruby.h"
#include "ruby/encoding.h"
#include "../json.h"
#include "../vendor/ryu.h"

/* shims */
/* This is the fallback definition from Ruby 3.4 */

#ifndef RBIMPL_STDBOOL_H
#if defined(__cplusplus)
# if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
# include <cstdbool>
# endif
#elif defined(HAVE_STDBOOL_H)
# include <stdbool.h>
#elif !defined(HAVE__BOOL)
typedef unsigned char _Bool;
# define bool _Bool
# define true ((_Bool)+1)
# define false ((_Bool)+0)
# define __bool_true_false_are_defined
#endif
#endif

#if SIZEOF_UINT64_T == SIZEOF_LONG_LONG
# define INT64T2NUM(x) LL2NUM(x)
# define UINT64T2NUM(x) ULL2NUM(x)
#elif SIZEOF_UINT64_T == SIZEOF_LONG
# define INT64T2NUM(x) LONG2NUM(x)
# define UINT64T2NUM(x) ULONG2NUM(x)
#else
# error No uint64_t conversion
#endif

#include "../simd/simd.h"

#ifndef RB_UNLIKELY
#define RB_UNLIKELY(expr) expr
#endif

#ifndef RB_LIKELY
#define RB_LIKELY(expr) expr
#endif

static VALUE mJSON, eNestingError, Encoding_UTF_8;
static VALUE CNaN, CInfinity, CMinusInfinity;

Expand Down Expand Up @@ -985,17 +946,11 @@ static const bool string_scan_table[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

#if (defined(__GNUC__ ) || defined(__clang__))
#define FORCE_INLINE __attribute__((always_inline))
#else
#define FORCE_INLINE
#endif

#ifdef HAVE_SIMD
static SIMD_Implementation simd_impl = SIMD_NONE;
#endif /* HAVE_SIMD */

static inline bool FORCE_INLINE string_scan(JSON_ParserState *state)
static ALWAYS_INLINE() bool string_scan(JSON_ParserState *state)
{
#ifdef HAVE_SIMD
#if defined(HAVE_SIMD_NEON)
Expand Down
Loading
Loading