Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use predefined string when Lexer.debug_enabled? #1159

Merged
merged 2 commits into from
Jun 11, 2019

Conversation

ashmaroli
Copy link
Contributor

Based on the following code:

rouge/lib/rouge/lexer.rb

Lines 309 to 314 in f494b53

def initialize(opts={})
@options = {}
opts.each { |k, v| @options[k.to_s] = v }
@debug = Lexer.debug_enabled? && bool_option(:debug)
end

When Lexer.debug_enabled?, bool_option(:debug)will be called each time the Lexer is initialized, which in turn causes "debug" to be allocated for each instance. This can be avoided by using "debug" directly since that is the one globally supported option and other allocations can be reduced by caching into a local variable.

@jneen
Copy link
Member

jneen commented Jun 7, 2019

Can we not just use bool_option('debug') since we have frozen string literals?

@pyrmont pyrmont added the author-action The PR has been reviewed but action by the author is needed label Jun 7, 2019
@ashmaroli
Copy link
Contributor Author

Can we not just use bool_option('debug') since we have frozen string literals?

We'd still have to handle "other options" passed in as symbols.
IMO, we should just use :to_sym here instead like we decided for lexers' state-names..
(because unlike to_s that will create a new object for every symbol converted, to_sym is going to generate just one object for every string converted. Moreover since the globally accepted option is documented as a symbol, the chances of receiving string option keys are minimal)

@jneen
Copy link
Member

jneen commented Jun 8, 2019

Except when we render from CGI options, as is common from markdown. Also, lexer options are untrusted data, so we need to be a little careful about casting them to symbols.

@jneen
Copy link
Member

jneen commented Jun 8, 2019

or any use of Lexer.find_fancy, which also uses CGI options...

@jneen
Copy link
Member

jneen commented Jun 8, 2019

Also, is that string not garbage collected later? This seems like a very premature optimization to me, especially for such a short string, whose data should be kept in the shared buffer.

@ashmaroli
Copy link
Contributor Author

ashmaroli commented Jun 8, 2019

Garbage collection would be triggered more frequently if :debug.to_s was called multiple times.
I've updated the branch to use bool_option('debug') like you suggested, @jneen

@pyrmont pyrmont added needs-review The PR needs to be reviewed and removed author-action The PR has been reviewed but action by the author is needed labels Jun 10, 2019
@pyrmont pyrmont merged commit 6d62ba1 into rouge-ruby:master Jun 11, 2019
@pyrmont pyrmont removed the needs-review The PR needs to be reviewed label Jun 11, 2019
@ashmaroli ashmaroli deleted the bool-string branch June 19, 2019 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants