mirror of
https://github.com/ruby/ruby.git
synced 2025-08-24 13:34:17 +02:00

Ref: https://github.com/ruby/json/issues/655 The actual buffer is still on the heap, but this saves a pair of malloc/free. This helps a lot on micro-benchmarks Before: ``` ruby 3.3.4 (2024-07-09 revisionbe1089c8ec
) +YJIT [arm64-darwin23] Warming up -------------------------------------- Oj 531.598k i/100ms JSON reuse 417.666k i/100ms Calculating ------------------------------------- Oj 5.735M (± 1.3%) i/s (174.35 ns/i) - 28.706M in 5.005900s JSON reuse 4.604M (± 1.4%) i/s (217.18 ns/i) - 23.389M in 5.080779s Comparison: Oj: 5735475.6 i/s JSON reuse: 4604380.3 i/s - 1.25x slower ``` After: ``` ruby 3.3.4 (2024-07-09 revisionbe1089c8ec
) +YJIT [arm64-darwin23] Warming up -------------------------------------- Oj 518.700k i/100ms JSON reuse 483.370k i/100ms Calculating ------------------------------------- Oj 5.722M (± 1.8%) i/s (174.76 ns/i) - 29.047M in 5.077823s JSON reuse 5.278M (± 1.5%) i/s (189.46 ns/i) - 26.585M in 5.038172s Comparison: Oj: 5722283.8 i/s JSON reuse: 5278061.7 i/s - 1.08x slower ``` Bench: ```ruby require 'benchmark/ips' require 'oj' require 'json' json_encoder = JSON::State.new(JSON.dump_default_options) test_data = [1, "string", { a: 1, b: 2 }, [3, 4, 5]] Oj.default_options = Oj.default_options.merge(mode: :compat) Benchmark.ips do |x| x.config(time: 5, warmup: 2) x.report("Oj") do Oj.dump(test_data) end x.report("JSON reuse") do json_encoder.generate(test_data) end x.compare!(order: :baseline) end ```72110f7992
60 lines
2.1 KiB
C
60 lines
2.1 KiB
C
#ifndef _PARSER_H_
|
|
#define _PARSER_H_
|
|
|
|
#include "ruby.h"
|
|
|
|
#ifndef MAYBE_UNUSED
|
|
# define MAYBE_UNUSED(x) x
|
|
#endif
|
|
|
|
#define option_given_p(opts, key) (rb_hash_lookup2(opts, key, Qundef) != Qundef)
|
|
|
|
typedef struct JSON_ParserStruct {
|
|
VALUE Vsource;
|
|
char *source;
|
|
long len;
|
|
char *memo;
|
|
VALUE create_id;
|
|
VALUE object_class;
|
|
VALUE array_class;
|
|
VALUE decimal_class;
|
|
VALUE match_string;
|
|
FBuffer fbuffer;
|
|
int max_nesting;
|
|
char allow_nan;
|
|
char parsing_name;
|
|
char symbolize_names;
|
|
char freeze;
|
|
char create_additions;
|
|
} JSON_Parser;
|
|
|
|
#define GET_PARSER \
|
|
GET_PARSER_INIT; \
|
|
if (!json->Vsource) rb_raise(rb_eTypeError, "uninitialized instance")
|
|
#define GET_PARSER_INIT \
|
|
JSON_Parser *json; \
|
|
TypedData_Get_Struct(self, JSON_Parser, &JSON_Parser_type, json)
|
|
|
|
#define MinusInfinity "-Infinity"
|
|
#define EVIL 0x666
|
|
|
|
static uint32_t unescape_unicode(const unsigned char *p);
|
|
static int convert_UTF32_to_UTF8(char *buf, uint32_t ch);
|
|
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
|
|
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
|
|
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result);
|
|
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result);
|
|
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
|
|
static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int symbolize);
|
|
static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result);
|
|
static VALUE convert_encoding(VALUE source);
|
|
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self);
|
|
static VALUE cParser_parse(VALUE self);
|
|
static void JSON_mark(void *json);
|
|
static void JSON_free(void *json);
|
|
static VALUE cJSON_parser_s_allocate(VALUE klass);
|
|
static VALUE cParser_source(VALUE self);
|
|
|
|
static const rb_data_type_t JSON_Parser_type;
|
|
|
|
#endif
|