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

Optimize preparation of options for Rouge #655

Merged
merged 3 commits into from
Apr 30, 2020

Conversation

ashmaroli
Copy link
Contributor

@ashmaroli ashmaroli commented Apr 29, 2020

Summary

  • Replace array allocation [span_opts, block_opts] for every method call with serial calls to an extracted module method.
  • Replace use of Hash#update and a static {:wrap=>false} with a direct mutation via Hash#[]= to avoid allocating {:wrap=>false} on every method call.
  • Since hash.keys allocates an array even for empty hashes, add a guard-clause.

Benchmark

(on Ruby 2.4, Windows)

highlighter options:  {:default_lang=>"plaintext", :guess_lang=>true}
Warming up --------------------------------------
     master     6.134k i/100ms
   proposed     7.358k i/100ms
Calculating -------------------------------------
     master     70.786k (A± 4.1%) i/s -    355.772k in   5.034089s
   proposed     89.577k (A± 4.7%) i/s -    448.838k in   5.021782s

Comparison:
   proposed:    89577.2 i/s
     master:    70786.3 i/s - 1.27x  slower
highlighter options:  {:default_lang=>"plaintext", :guess_lang=>true, :span=>{"foo"=>"bar"}, :block=>{"lorem"=>"ipsum"}}
Warming up --------------------------------------
     master     3.521k i/100ms
   proposed     3.737k i/100ms
Calculating -------------------------------------
     master     40.721k (A± 5.2%) i/s -    204.218k in   5.028392s
   proposed     43.369k (A± 8.7%) i/s -    216.746k in   5.030135s

Comparison:
   proposed:    43369.5 i/s
     master:    40720.8 i/s - same-ish: difference falls within error

Memory profiles

highlighter options:  {:default_lang=>"plaintext", :guess_lang=>true}

memory-profile with master code through 1000 iterations

Total allocated: 1.24 MB (12000 objects)
Total retained:  576.00 B (3 objects)

memory-profile with proposed code through 1000 iterations

Total allocated: 848.00 kB (6000 objects)
Total retained:  576.00 B (3 objects)
highlighter options:  {:default_lang=>"plaintext", :guess_lang=>true, :span=>{"foo"=>"bar"}, :block=>{"lorem"=>"ipsum"}}

memory-profile with master code through 1000 iterations

Total allocated: 1.72 MB (14006 objects)
Total retained:  736.00 B (7 objects)

memory-profile with proposed code through 1000 iterations

Total allocated: 1.49 MB (12006 objects)
Total retained:  736.00 B (7 objects)

@gettalong gettalong merged commit a7f2f5f into gettalong:master Apr 30, 2020
@gettalong
Copy link
Owner

Thank you!

@ashmaroli ashmaroli deleted the prep-rouge-options branch April 30, 2020 07:26
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.

2 participants