premailer / css_parser

Ruby CSS Parser
Other
279 stars 110 forks source link

Reduce Allocations to reduce memory usage #102

Closed stevendaniels closed 5 years ago

stevendaniels commented 5 years ago

While using the premailer gem with a background worker, I noticed that our excessive GC was slowing our mail jobs down. The main culprit, is this gem. I took 3 approaches to reducing allocations and memory usage.

  1. reduce dynamic string creation for border properties. (This can also be done for expand_dimensions_shorthand
  2. Reduced hash allocations
  3. Reduces string allocations when parsing a complex document.

Overall, allocated memory was reduced by 25% and object allocation was reduced by 27%.

Before

Total allocated: 6873180 bytes (96310 objects)
Total retained:  5585 bytes (58 objects)

allocated memory by gem
-----------------------------------
   6861148  css_parser/lib
     10776  racc
      1256  other

allocated objects by class
-----------------------------------
     57903  String
     20809  Array
     14161  Hash
      3243  MatchData
       179  CssParser::RuleSet
        15  Racc::CparseParams

After

Total allocated: 5172596 bytes (70638 objects)
Total retained:  5585 bytes (58 objects)

allocated memory by gem
-----------------------------------
   5160564  css_parser/lib
     10776  racc
      1256  other

allocated objects by class
-----------------------------------
     43867  String
     14531  Array
      8803  Hash
      3243  MatchData
       179  CssParser::RuleSet
        15  Racc::CparseParams
stevendaniels commented 5 years ago

I've made your suggested improvements.

grosser commented 5 years ago

want to also do expand_dimensions_shorthand so things look more uniform ?

stevendaniels commented 5 years ago

want to also do expand_dimensions_shorthand so things look more uniform ?

Done!

grosser commented 5 years ago

1.7.0 🎉

also added a tiny cleanup https://github.com/premailer/css_parser/commit/cffac203d3d602e464f66f5512ae2824b2069af3