Skip to content

Commit f8c57a3

Browse files
authored
Merge pull request #12 from casperisfine/single-parser
Use a single static parser for both performance and avoiding leaks
2 parents 107d30e + 65e0aa6 commit f8c57a3

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

benchmark/leak.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env ruby
2+
3+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
4+
require 'fast_jsonparser'
5+
6+
Thread.abort_on_exception = true
7+
8+
3.times do
9+
Thread.new do
10+
loop do
11+
begin
12+
FastJsonparser.parse('{"foo": "bar"')
13+
rescue
14+
end
15+
end
16+
end
17+
end
18+
19+
loop do
20+
sleep 1
21+
p `ps -o rss -p #{$$}`.chomp.split("\n").last.to_i
22+
end

ext/fast_jsonparser/fast_jsonparser.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
VALUE rb_eFastJsonparserUnknownError, rb_eFastJsonparserParseError;
66

77
using namespace simdjson;
8+
static dom::parser parser;
89

910
// Convert tape to Ruby's Object
1011
static VALUE make_ruby_object(dom::element element, bool symbolize_keys)
@@ -71,7 +72,6 @@ static VALUE rb_fast_jsonparser_parse(VALUE self, VALUE arg, VALUE symbolize_key
7172
{
7273
Check_Type(arg, T_STRING);
7374

74-
dom::parser parser;
7575
auto [doc, error] = parser.parse(RSTRING_PTR(arg), RSTRING_LEN(arg));
7676
if (error != SUCCESS)
7777
{
@@ -84,7 +84,6 @@ static VALUE rb_fast_jsonparser_load(VALUE self, VALUE arg, VALUE symbolize_keys
8484
{
8585
Check_Type(arg, T_STRING);
8686

87-
dom::parser parser;
8887
auto [doc, error] = parser.load(RSTRING_PTR(arg));
8988
if (error != SUCCESS)
9089
{
@@ -98,9 +97,7 @@ static VALUE rb_fast_jsonparser_load_many(VALUE self, VALUE arg, VALUE symbolize
9897
Check_Type(arg, T_STRING);
9998
Check_Type(batch_size, T_FIXNUM);
10099

101-
try
102-
{
103-
dom::parser parser;
100+
try {
104101
auto [docs, error] = parser.load_many(RSTRING_PTR(arg), FIX2INT(batch_size));
105102
if (error != SUCCESS)
106103
{

0 commit comments

Comments
 (0)