From 54e6650338e97657e9611320bd0c092428592125 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 17 Apr 2014 19:08:26 +0200 Subject: [PATCH] Don't use define_method in the lexer. Profiling showed that calls to methods defined using `define_method` are really, really slow. Before this commit the lexer would process 3000-4000 lines per second. With this commit that has been increased to around 10 000 lines per second. Thanks to @headius for mentioning the (potential) overhead of define_method. --- lib/oga/xml/lexer.rl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/oga/xml/lexer.rl b/lib/oga/xml/lexer.rl index 8502fb1..3f08282 100644 --- a/lib/oga/xml/lexer.rl +++ b/lib/oga/xml/lexer.rl @@ -47,16 +47,6 @@ module Oga 'wbr' ]) - # Lazy way of forwarding instance method calls used internally by Ragel - # to their corresponding class methods. - private_methods.grep(/^_lexer_/).each do |name| - define_method(name) do - return self.class.send(name) - end - - private(name) - end - ## # @param [String] data The data to lex. # @@ -141,6 +131,16 @@ module Oga def advance(&block) @block = block + _lexer_eof_trans = self.class.send(:_lexer_eof_trans) + _lexer_from_state_actions = self.class.send(:_lexer_from_state_actions) + _lexer_index_offsets = self.class.send(:_lexer_index_offsets) + _lexer_indicies = self.class.send(:_lexer_indicies) + _lexer_key_spans = self.class.send(:_lexer_key_spans) + _lexer_to_state_actions = self.class.send(:_lexer_to_state_actions) + _lexer_trans_actions = self.class.send(:_lexer_trans_actions) + _lexer_trans_keys = self.class.send(:_lexer_trans_keys) + _lexer_trans_targs = self.class.send(:_lexer_trans_targs) + %% write exec; # % fix highlight