This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: Speeding Up Builder 2.0
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
This week I’m trying to break the skids off of Markaby. A recent Camping audit has lit up Markaby as the slowpoke. And I happened to root up a bit of slowness in Builder::XChar. The Range.include? is killing us because the Range gets cast to an Array every time!
My friends, in your lives, in your valuable time, do not, in any case, use Range#include? Use Range#===. (0x0000..0xFFFF) === 0xBABE you know it’s true. I have been careless, too.
Here’s a replacement:
module XChar
VALID = [
0x9, 0xA, 0xD,
(0x20..0xD7FF),
(0xE000..0xFFFD),
(0x10000..0x10FFFF)
]
end
class Fixnum
# XML escaped version of chr
def xchr
n = XChar::CP1252[self] || self
case n when *XChar::VALID
XChar::PREDEFINED[n] or (n<128 ? n.chr : "&##{n};")
else
'*'
end
end
end
Which gave me a 30% speed up on the tests Atrus was running. The UTF-8 unpack is also taking some time. I wonder.