develooper Front page | perl.perl5.porters | Postings from January 2005

Faster Perl_keyword

Thread Next
From:
Nicholas Clark
Date:
January 25, 2005 09:31
Subject:
Faster Perl_keyword
Message ID:
20050125173137.GG12352@plum.flirble.org
As of change 23876 Perl_keyword, called by the tokeniser, should be about 20%
faster. (Not that I expect that this will show up measurably by timing perl as
a whole). ( http://public.activestate.com/cgi-bin/perlbrowse?patch=23876 )

I hacked Perl_keyword to record everything it ever called:

I32
Perl_keyword(pTHX_ register char *d, I32 len)
{
  if (len < 256)
  {
    int fd = open (INSTALL_PREFIX_EXP "/keywordlog", O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
    if (fd) {
      unsigned char buffer[257];
      *buffer = len;
      memcpy(buffer+1,d,len);
      buffer[len+1] = '\0';
      write(fd, buffer, len+2);
      close(fd);
    }
  }
  else
  {
    int fd = open (INSTALL_PREFIX_EXP "/longkeywordlog", O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
    if (fd) {
      write(fd, d, len+1);
      close(fd);
    }
  }

then used this data to feed into a subclass of ExtUtils::Constant to generate
a new lookup table weighted to match the more common keywords earlier. (see
http://public.activestate.com/cgi-bin/perlbrowse?file=perl_keyword.pl&blame=1 )

Benchmarking was done with the attached program linked against a file
containing each Perl_keyword (with the warning call at 'elseif' converted to
a call to the dummy function stuff() to stop it being optimised away).

So now you can log your own preferred set of perl keywords, and regenerate
toke.c to be fractionally faster for whatever perl you used. :-)
Although I doubt that this will make a difference, as I'm assuming that my
135M of input data consisting of a lot of CPAN was typical enough.

Nicholas Clark

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About