From 58f5db971663fca777db57cb2bcaab1a2746f477 Mon Sep 17 00:00:00 2001 From: blackhedd Date: Sat, 2 Sep 2006 04:43:35 +0000 Subject: [PATCH] performance improvement in BERParser#read_ber, replaced a lot of calls to Symbol#===. --- lib/net/ber.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/net/ber.rb b/lib/net/ber.rb index fabcb0f..5bd6f0f 100644 --- a/lib/net/ber.rb +++ b/lib/net/ber.rb @@ -94,6 +94,8 @@ module Net end } +=begin + Replaced this case with if/else because Symbol#=== profiled surprisingly hot. obj = case objtype when :boolean newobj != "\000" @@ -116,6 +118,29 @@ module Net else raise BerError.new( "unsupported object type: class=#{tagclass}, encoding=#{encoding}, tag=#{tag}" ) end +=end + + obj = if objtype == :boolean + newobj != "\000" + elsif objtype == :string + (newobj || "").dup + elsif objtype == :integer + j = 0 + newobj.each_byte {|b| j = (j << 8) + b} + j + elsif objtype == :array + seq = [] + sio = StringIO.new( newobj || "" ) + # Interpret the subobject, but note how the loop + # is built: nil ends the loop, but false (a valid + # BER value) does not! + while (e = sio.read_ber(syntax)) != nil + seq << e + end + seq + else + raise BerError.new( "unsupported object type: class=#{tagclass}, encoding=#{encoding}, tag=#{tag}" ) + end # Add the identifier bits into the object if it's a String or an Array. # We can't add extra stuff to Fixnums and booleans, not that it makes much sense anyway.