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

Optimizations #19

Merged
merged 26 commits into from
Sep 11, 2019
Merged

Optimizations #19

merged 26 commits into from
Sep 11, 2019

Commits on Sep 7, 2019

  1. Optimization: don't create new hash in Renderer#escape

    Renderer#escape was each time passing a new hash instance to gsub,
    making it allocate more memory than neccessary. Moving the hash to a
    constant improves performance a bit.
    
    For `Markd.to_html("hello world")`
    
    before 350.46k (  2.85µs) (± 6.31%)  4.09kB/op
    after  431.05k (  2.32µs) (± 1.43%)  3.07kB/op
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    48935f3 View commit details
    Browse the repository at this point in the history
  2. Optimization: avoid calling gsub when not needed

    For `Markd.to_html("hello world")`
    
    before  431.05k (  2.32µs) (± 1.43%)  3.07kB/op
    after   506.43k (  1.97µs) (± 2.50%)  2.72kB/op
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    66c6ea0 View commit details
    Browse the repository at this point in the history
  3. Optimization: avoid creating attributes Hash if not needed

    For `Markd.to_html("hello world")`
    
    before 506.43k (  1.97µs) (± 2.50%)  2.72kB/op
    after  554.18k (  1.80µs) (± 1.10%)  2.41kB/op
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    530de4c View commit details
    Browse the repository at this point in the history
  4. Optimization: avoid creating openers_bottom Hash if not needed

    For `Markd.to_html("hello world")`:
    
    before 554.18k (  1.80µs) (± 1.10%)  2.41kB/op
    after  640.45k (  1.56µs) (± 6.70%)  2.06kB/op
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    92f850e View commit details
    Browse the repository at this point in the history
  5. Optimization: make Node#data be lazily initialized

    This avoids initializing a Hash when not needed.
    
    For `Markd.to_html("hello world")`:
    
    before 640.45k (  1.56µs) (± 6.70%)  2.06kB/op
    after  727.39k (  1.37µs) (± 2.28%)  1.59kB/op
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    c096d59 View commit details
    Browse the repository at this point in the history
  6. Optimization: extract array literal to constant

    Array literals in tight loops allocate a lot of memory!
    
    Parsing markd's README.md:
    
    before 3.52k (283.87µs) (± 0.59%)  259kB/op
    after  3.66k (273.38µs) (± 0.54%)  244kB/op
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    795b0ff View commit details
    Browse the repository at this point in the history
  7. Optimization: avoid double regex match

    For `Markd.to_html("hello world")`:
    
    before 726.36k (  1.38µs) (± 1.86%)  1.59kB/op
    after  800.81k (  1.25µs) (± 0.58%)  1.58kB/op
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    4a8e485 View commit details
    Browse the repository at this point in the history
  8. Optimization: work at the byte level

    Markdown is an ASCII format: all control characters are ASCII so we
    can avoid decoding unicode characters when not needed. The old code was
    indexing bytes by unicode index position which is very slow because the
    string must be decoded each time. Here we change it to index by byte,
    and change all operations to do byte slices instead of unicode slices.
    
    For markd's README.md:
    
    before 3.86k (258.86µs) (± 0.52%)  242kB/op
    after  4.11k (243.25µs) (± 1.37%)  242kB/op
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    eb17b25 View commit details
    Browse the repository at this point in the history
  9. Optimization: perform MAIN rule by hand

    For `Markd.to_html("hello world")`:
    
    before 800.81k (  1.25µs) (± 0.58%)  1.58kB/op
    after  829.33k (  1.21µs) (±25.35%)  1.56kB/op
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    938ae0e View commit details
    Browse the repository at this point in the history
  10. Optimization: avoid allocating unnecessary arrays

    We can use an inline comparison or a tuple.
    
    For markd's README.md:
    
    before 4.57k (218.68µs) (± 0.64%)  198kB/op
    after  4.63k (215.84µs) (± 2.20%)  196kB/op
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    e3d247e View commit details
    Browse the repository at this point in the history
  11. Configuration menu
    Copy the full SHA
    e451a58 View commit details
    Browse the repository at this point in the history
  12. Optimization: use each_value instead of values.each

    `values.each` will create an intermediary array
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    ee76396 View commit details
    Browse the repository at this point in the history
  13. Configuration menu
    Copy the full SHA
    8540dda View commit details
    Browse the repository at this point in the history
  14. Configuration menu
    Copy the full SHA
    c0eb22f View commit details
    Browse the repository at this point in the history
  15. Optimization: there's no need to create an array of lines

    We can use `each_line` instead of `lines` and then iterating.
    
    For `Markd.to_html("hello world")`:
    
    before 882.84k (  1.13µs) (± 1.46%)  1.56kB/op
    after  929.11k (  1.08µs) (± 2.75%)  1.46kB/op
    
    For markd's README.md:
    
    before 4.85k (206.37µs) (± 2.24%)  187kB/op
    after  4.98k (200.82µs) (± 0.47%)  183kB/op
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    476ef2b View commit details
    Browse the repository at this point in the history
  16. Configuration menu
    Copy the full SHA
    86c700d View commit details
    Browse the repository at this point in the history
  17. Optimization: use String::Builder instead of IO::Memory

    `String::Builder` is slightly faster when used for a one-time shot.
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    fb8048d View commit details
    Browse the repository at this point in the history
  18. Optimization: for escape chars we can use bytes

    Because all escape chars are ASCII it's fine if we go byte per byte.
    
    For `Markd.to_html("hello world")`:
    
    before 942.28k (  1.06µs) (± 5.45%)  1.46kB/op
    after  959.11k (  1.04µs) (± 0.72%)  1.46kB/op
    
    For markd's README.md:
    
    before 4.97k (201.41µs) (± 0.74%)  176kB/op
    after  5.05k (197.95µs) (± 1.51%)  176kB/op
    asterite committed Sep 7, 2019
    Configuration menu
    Copy the full SHA
    408ab49 View commit details
    Browse the repository at this point in the history
  19. Configuration menu
    Copy the full SHA
    ba9d8c4 View commit details
    Browse the repository at this point in the history
  20. Configuration menu
    Copy the full SHA
    ab2a2d1 View commit details
    Browse the repository at this point in the history

Commits on Sep 8, 2019

  1. Configuration menu
    Copy the full SHA
    5e9d34c View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    1f83e56 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    80ae327 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    16ed2f6 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    8688ebb View commit details
    Browse the repository at this point in the history
  6. Optimization: avoid gsub if not needed in decode_uri

    For markd's README.md:
    
    before 5.14k (194.65µs) (± 1.25%)  174kB/op
    after  5.23k (191.14µs) (± 0.55%)  173kB/op
    asterite committed Sep 8, 2019
    Configuration menu
    Copy the full SHA
    50ee837 View commit details
    Browse the repository at this point in the history