Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updating load order for benchmarks, slight optimization when encoding…

… to an IO
  • Loading branch information...
commit 5e8d3acfad42b35c6749f7f2b8628fd09ec0e668 1 parent 9772a94
@brianmario authored
View
6 benchmark/encode.rb
@@ -1,7 +1,11 @@
# encoding: UTF-8
require 'rubygems'
require 'benchmark'
-require 'yajl_ext'
+begin
+ require 'yajl_ext'
+rescue LoadError
+ require 'yajl/ffi'
+end
require 'stringio'
begin
require 'json'
View
6 benchmark/encode_json_and_marshal.rb
@@ -1,7 +1,11 @@
# encoding: UTF-8
require 'rubygems'
require 'benchmark'
-require 'yajl_ext'
+begin
+ require 'yajl_ext'
+rescue LoadError
+ require 'yajl/ffi'
+end
require 'stringio'
begin
require 'json'
View
6 benchmark/encode_json_and_yaml.rb
@@ -1,7 +1,11 @@
# encoding: UTF-8
require 'rubygems'
require 'benchmark'
-require 'yajl_ext'
+begin
+ require 'yajl_ext'
+rescue LoadError
+ require 'yajl/ffi'
+end
begin
require 'json'
rescue LoadError
View
8 benchmark/parse.rb
@@ -1,7 +1,11 @@
# encoding: UTF-8
require 'rubygems'
require 'benchmark'
-require 'yajl_ext'
+begin
+ require 'yajl_ext'
+rescue LoadError
+ require 'yajl/ffi'
+end
begin
require 'json'
rescue LoadError
@@ -54,4 +58,4 @@
}
end
}
-json.close
+json.close
View
6 benchmark/parse_ffi.rb
@@ -3,7 +3,11 @@
require 'rubygems'
require 'benchmark'
-require 'yajl/ffi'
+begin
+ require 'yajl_ext'
+rescue LoadError
+ require 'yajl/ffi'
+end
require 'json'
require 'activesupport'
View
6 benchmark/parse_json_and_marshal.rb
@@ -1,7 +1,11 @@
# encoding: UTF-8
require 'rubygems'
require 'benchmark'
-require 'yajl_ext'
+begin
+ require 'yajl_ext'
+rescue LoadError
+ require 'yajl/ffi'
+end
begin
require 'json'
rescue LoadError
View
6 benchmark/parse_json_and_yaml.rb
@@ -1,7 +1,11 @@
# encoding: UTF-8
require 'rubygems'
require 'benchmark'
-require 'yajl_ext'
+begin
+ require 'yajl_ext'
+rescue LoadError
+ require 'yajl/ffi'
+end
begin
require 'json'
rescue LoadError
View
6 benchmark/parse_stream.rb
@@ -1,7 +1,11 @@
# encoding: UTF-8
require 'rubygems'
require 'benchmark'
-require 'yajl_ext'
+begin
+ require 'yajl_ext'
+rescue LoadError
+ require 'yajl/ffi'
+end
begin
require 'json'
rescue LoadError
View
20 lib/yajl/ffi/encoder.rb
@@ -51,7 +51,7 @@ def encode(obj, io=nil, &block)
@callback = block if block_given?
encode_part(obj, io)
- outBuf = get_buffer
+ outBuf, len = get_buffer
yajl_gen_clear(@encoder)
if io
io.write(outBuf)
@@ -84,15 +84,20 @@ def on_progress=(proc)
INFINITY = "Infinity"
NEG_INFINITY = "-Infinity"
- def get_buffer
+ def get_buffer(include_str=true)
outBufPtr = FFI::MemoryPointer.new(:pointer)
outLen = FFI::MemoryPointer.new(:pointer)
status = yajl_gen_get_buf(@encoder, outBufPtr, outLen)
- if outLen.read_int > 0
- outBufPtr = outBufPtr.read_pointer
- outBufPtr.read_string
+ len = outLen.read_int
+ if include_str
+ if len > 0
+ outBufPtr = outBufPtr.read_pointer
+ [outBufPtr.read_string, len]
+ else
+ ["", 0]
+ end
else
- ""
+ len
end
end
@@ -101,8 +106,9 @@ def encode_part(obj, io=nil)
len = 0
if io or @callback
- outBuf = get_buffer
+ len = get_buffer(false)
if len > WRITE_BUFSIZE
+ outBuf, len = get_buffer
if io
io.write(outBuf)
elsif @callback
Please sign in to comment.
Something went wrong with that request. Please try again.