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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 12/11/06, Jeremy Evans <jeremyevans0-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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: >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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 11, 2006, at 1:53 PM, Jeremy Evans wrote:> 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. > > <snip> > > # 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.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 ;) Thanks- -- Ezra Zygmuntowicz -- Lead Rails Evangelist -- ez-NLltGlunAUd/unjJdyJNww@public.gmane.org -- Engine Yard, Serious Rails Hosting -- (866) 518-YARD (9273) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 12/11/06, Jeremy Evans <jeremyevans0-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 12/12/06, Jeremy Evans <jeremyevans0-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 12/11/06, Jeremy Evans <jeremyevans0-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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. > > > > To use: > > > > # For example, in environment.rb > > require ''speed_up_html_escape'' > > I got enough requests for this that I''ve packaged it as a gem and > applied for a RubyForge project.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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---