Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kostya committed Jun 10, 2021
1 parent 50f0a5d commit 7a81729
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 38 deletions.
6 changes: 1 addition & 5 deletions bench/unpack.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ def test_unpack(name, count, klass, data, *, zero_copy = false)
print name
res = 0
count.times do |i|
obj = if zero_copy
klass.from_msgpack_zero_copy(slice)
else
klass.from_msgpack(slice)
end
obj = klass.from_msgpack(slice, zero_copy: zero_copy)
res += obj.is_a?(String) ? obj.bytesize : obj.size
end
Global.summary_unpacked += res
Expand Down
16 changes: 8 additions & 8 deletions spec/lexer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ private def it_lexes(description, expected_type, bytes, file = __FILE__, line =
string = Bytes.new(bytes.to_unsafe, bytes.size)

it "lexes #{description} from IO", file, line do
lexer = MessagePack::Lexer.new string
lexer = MessagePack::Lexer.new IO::Memory.new(string)
token = lexer.read_token
token.class.should eq(expected_type)
end
Expand All @@ -14,7 +14,7 @@ private def it_lexes_int(description, int_value, bytes, file = __FILE__, line =
string = Bytes.new(bytes.to_unsafe, bytes.size)

it "lexes #{description} from IO", file, line do
lexer = MessagePack::Lexer.new string
lexer = MessagePack::Lexer.new IO::Memory.new(string)
token = lexer.read_token
case token
when MessagePack::Token::IntT
Expand All @@ -29,7 +29,7 @@ private def it_lexes_float(description, float_value, bytes, file = __FILE__, lin
string = Bytes.new(bytes.to_unsafe, bytes.size)

it "lexes #{description}", file, line do
lexer = MessagePack::Lexer.new string
lexer = MessagePack::Lexer.new IO::Memory.new(string)
token = lexer.read_token
case token
when MessagePack::Token::FloatT
Expand All @@ -44,7 +44,7 @@ private def it_lexes_string(description, string_value, bytes, file = __FILE__, l
string = Bytes.new(bytes.to_unsafe, bytes.size)

it "lexes #{description}", file, line do
lexer = MessagePack::Lexer.new string
lexer = MessagePack::Lexer.new IO::Memory.new(string)
token = lexer.read_token
case token
when MessagePack::Token::StringT
Expand All @@ -59,7 +59,7 @@ private def it_lexes_bytes(description, decoded, bytes, file = __FILE__, line =
string = Bytes.new(bytes.to_unsafe, bytes.size)

it "lexes #{description}", file, line do
lexer = MessagePack::Lexer.new string
lexer = MessagePack::Lexer.new IO::Memory.new(string)
token = lexer.read_token
case token
when MessagePack::Token::BytesT
Expand All @@ -74,7 +74,7 @@ private def it_lexes_arrays(description, size, bytes, file = __FILE__, line = __
string = Bytes.new(bytes.to_unsafe, bytes.size)

it "lexes #{description}", file, line do
lexer = MessagePack::Lexer.new string
lexer = MessagePack::Lexer.new IO::Memory.new(string)
token = lexer.read_token
case token
when MessagePack::Token::ArrayT
Expand All @@ -89,7 +89,7 @@ private def it_lexes_hashes(description, size, bytes, file = __FILE__, line = __
string = Bytes.new(bytes.to_unsafe, bytes.size)

it "lexes #{description}", file, line do
lexer = MessagePack::Lexer.new string
lexer = MessagePack::Lexer.new IO::Memory.new(string)
token = lexer.read_token
case token
when MessagePack::Token::HashT
Expand Down Expand Up @@ -148,7 +148,7 @@ describe MessagePack::Lexer do
it "only calls next byte before reading not after reading" do
bytes = UInt8[0xff, 0xff]
string = String.new(bytes.to_unsafe, bytes.size)
lexer = MessagePack::Lexer.new(string)
lexer = MessagePack::Lexer.new(IO::Memory.new(string))
lexer.read_token.should eq MessagePack::Token::IntT.new(0, -1)
lexer.read_token.should eq MessagePack::Token::IntT.new(1, -1)
expect_raises(MessagePack::EofError) do
Expand Down
38 changes: 18 additions & 20 deletions src/message_pack/from_msgpack.cr
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
def Object.from_msgpack(string_or_io)
parser = MessagePack::IOUnpacker.new(string_or_io)
def Object.from_msgpack(string_or_io, zero_copy = false)
parser = if zero_copy
MessagePack::IOUnpackerZeroCopy.new(string_or_io)
else
MessagePack::IOUnpacker.new(string_or_io)
end
new parser
end

def Hash.from_msgpack(string_or_io, default_value)
parser = MessagePack::IOUnpacker.new(string_or_io)
def Hash.from_msgpack(string_or_io, default_value, zero_copy = false)
parser = if zero_copy
MessagePack::IOUnpackerZeroCopy.new(string_or_io)
else
MessagePack::IOUnpacker.new(string_or_io)
end
new(parser, default_value)
end

def Hash.from_msgpack(string_or_io, &block : (Hash(K, V), K -> V))
parser = MessagePack::IOUnpacker.new(string_or_io)
new(parser, block)
end

def Object.from_msgpack_zero_copy(string_or_io)
parser = MessagePack::IOUnpackerZeroCopy.new(string_or_io)
new parser
end

def Hash.from_msgpack_zero_copy(string_or_io, default_value)
parser = MessagePack::IOUnpackerZeroCopy.new(string_or_io)
new(parser, default_value)
end
def Hash.from_msgpack(string_or_io, zero_copy = false, &block : (Hash(K, V), K -> V))
parser = if zero_copy
MessagePack::IOUnpackerZeroCopy.new(string_or_io)
else
MessagePack::IOUnpacker.new(string_or_io)
end

def Hash.from_msgpack_zero_copy(string_or_io, &block : (Hash(K, V), K -> V))
parser = MessagePack::IOUnpackerZeroCopy.new(string_or_io)
new(parser, block)
end

Expand Down
8 changes: 4 additions & 4 deletions src/message_pack/lexer.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class MessagePack::Lexer
@token : Token::T
@byte_number = 0
@current_byte_number = 0
@token_finished = true
@token = Token::NullT.new(0)

def initialize(@io : IO)
@byte_number = 0
@current_byte_number = 0
@token = Token::NullT.new(0)
@token_finished = true
end

@[AlwaysInline]
Expand Down
2 changes: 1 addition & 1 deletion src/message_pack/unpacker/io_unpacker.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class MessagePack::IOUnpacker < MessagePack::Unpacker

def self.new(bytes : Bytes | String)
io = IO::Memory.new(bytes)
new(io)
self.new(io)
end

def self.new(array : Array(UInt8))
Expand Down
11 changes: 11 additions & 0 deletions src/message_pack/unpacker/io_unpacker_zero_copy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@ class MessagePack::IOUnpackerZeroCopy < MessagePack::IOUnpacker
def initialize(io : IO)
@lexer = MessagePack::LexerZeroCopy.new(io)
end

def self.new(bytes : Bytes | String)
io = IO::Memory.new(bytes)
self.new(io)
end

def self.new(array : Array(UInt8))
bytes = Bytes.new(array.to_unsafe, array.size)
io = IO::Memory.new(bytes)
new(io)
end
end

0 comments on commit 7a81729

Please sign in to comment.