FasterHTMLEscape, an h replacement

h is a function commonly used in .rhtml templates to escape HTML data (e.g. "<%=h html %>"). It is defined in ERB::Util#html_escape (h is an alias). It's implementation is relatively unoptimized, requiring a separate gsub for each of <, >, &, and ". Ruby's implementation of String#gsub (str_gsub, written in C), converts all string arguments to regular expressions before scanning the input, further slowing things down.

I have rewritten html_escape in C, speeding it up by a factor of 4-20 depending on the input. Input with no HTML characters is about 4 times faster, input of exclusively HTML characters is about 20 times faster.

Attached is a tarball, which includes:

extconf.rb - for creating the makefile to compile the extension faster_html_escape.c - The C version of html_escape (which is created inside the FasterHTMLEscape module) speed_up_html_escape.rb - This replaces the ERB::Util version of html_escape and h, as well as CGI.escapeHTML, with the C version test_faster_html_escape.rb - Small test script for checking the validity of the C function as well as benchmarking it test_faster_html_escape.sh - Uses test_faster_html_escape.rb to check and benchmark the C function with various inputs

To install (assuming Unix conventions):

  tar zxf faster_html_escape.tar.gz   cd faster_html_escape   ruby extconf.rb   make   ./test_faster_html_escape.sh   sudo make install

To use:

  # For example, in environment.rb   require 'speed_up_html_escape'

If there is interest in this, I can look into packaging it as a gem.

Please try it out and give me feedback.

Thanks, Jeremy

faster_html_escape.tar.gz (2.44 KB)

Great idea Jeremy! Considering that this is used so often in rendering html, this is a simple way to improve rendering performance especially for files that have large amounts of html_escaped content.

Have you considered creating a gem for this?

Ideally I’d like to see this replace the built-in ruby html_escape once adequate testing and analysis has been done on the module, but in the meantime it would be great to have it as a gem to lower the barrier of entry for users (especially windoze users which often don’t have compilers).

Blessings,

Jeff

Anything that can reliably improve speed should be greeted with much enthusiasm. So, I definitely think there will be interest in this and the gem idea is a good one.

thanks, andy

Hey Jeremy-

  This is definitely cool. I would definitely use this as a gem if you make it into one. If you don't I might vendor it in merb :wink:

Thanks-

-- Ezra Zygmuntowicz-- Lead Rails Evangelist -- ez@engineyard.com -- Engine Yard, Serious Rails Hosting -- (866) 518-YARD (9273)

I got enough requests for this that I've packaged it as a gem and applied for a RubyForge project. I'll post again after the project has been approved and the gem is available via gem install. I don't anticipate having a Win32 version, but if you running Rails on Win32, you probably don't care too much about performance.

Thanks, Jeremy

I got enough requests for this that I've packaged it as a gem and applied for a RubyForge project. I'll post again after the project has been approved and the gem is available via gem install.

Very nice

  I don't anticipate having a Win32 version, but if you running Rails on Win32, you probably don't care too much about performance.   

lol

Thanks,

matthi

The gem is now available via "gem install faster_html_escape". After installing it, add "require 'speed_up_html_escape'" to environment.rb to speed your h up.

If you want to use it outside of Rails:

  require 'rubygems'   require_gem 'faster_html_escape'   require 'speed_up_html_escape'

Please direct any questions/comments to the RubyForge site: http://rubyforge.org/projects/fasterh/.

Thanks, Jeremy