Skip to content

Commit 58f5db9

Browse files
author
blackhedd
committed
performance improvement in BERParser#read_ber,
replaced a lot of calls to Symbol#===.
1 parent 91d5fff commit 58f5db9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

lib/net/ber.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def read_ber syntax=nil
9494
end
9595
}
9696

97+
=begin
98+
Replaced this case with if/else because Symbol#=== profiled surprisingly hot.
9799
obj = case objtype
98100
when :boolean
99101
newobj != "\000"
@@ -116,6 +118,29 @@ def read_ber syntax=nil
116118
else
117119
raise BerError.new( "unsupported object type: class=#{tagclass}, encoding=#{encoding}, tag=#{tag}" )
118120
end
121+
=end
122+
123+
obj = if objtype == :boolean
124+
newobj != "\000"
125+
elsif objtype == :string
126+
(newobj || "").dup
127+
elsif objtype == :integer
128+
j = 0
129+
newobj.each_byte {|b| j = (j << 8) + b}
130+
j
131+
elsif objtype == :array
132+
seq = []
133+
sio = StringIO.new( newobj || "" )
134+
# Interpret the subobject, but note how the loop
135+
# is built: nil ends the loop, but false (a valid
136+
# BER value) does not!
137+
while (e = sio.read_ber(syntax)) != nil
138+
seq << e
139+
end
140+
seq
141+
else
142+
raise BerError.new( "unsupported object type: class=#{tagclass}, encoding=#{encoding}, tag=#{tag}" )
143+
end
119144

120145
# Add the identifier bits into the object if it's a String or an Array.
121146
# We can't add extra stuff to Fixnums and booleans, not that it makes much sense anyway.

0 commit comments

Comments
 (0)