Wednesday, March 16, 2011

Speeding up Perl Functions in XS

For the longest time I was scared to mess around with XS, but I started messing around with XS in my free time the past couple of days. I've been using Term::VT102 as my sandbox. I am using this for two reasons: (a) I use it pretty heavily in some of my software and would enjoy some speedups, and (b) the code looks like it can be ported to XS without much resistance once I have the tuits.

One of my current goals is to make an entire port of Term::VT102 in XS (Term::VT102::XS). It is progressing very slowly. However, in an act of impatience, I took a current copy of Term::VT102 and converted one of its functions to XS to see how much faster it was. I replaced



...with...



and ran a test script through Devel::NYTProf.

The pure-perl version ran in this much time (9.44s is the time in line):


The XS version ran in this much time:


A speedup that reduced the amount of time in that sub to nearly one ninth the time! Believe it or not, this function was one of the more expensive calls in my project code. I know I cheated by requiring $self for the XS function (rendering it completely incompatible but I don't know how to overload it quite yet), but I hope that doesn't make too much of a difference, speed-wise. If it speeds up a single function to 12% of the time (inclusive), imagine how much faster it will be with the entire thing ported to XS.

No comments:

Post a Comment