• Jason Lee's avatar
    Avoid allocations in Ability class. · 03a40a7e
    Jason Lee authored
    It won't change anything after they are first invoke, so add method cache to avoid allocations and avoid GC.
    
    Benchmarks:
    
    ```
    Calculating -------------------------------------
    project_guest_rules without method cache
                            79.352k i/100ms
    project_guest_rules with method cache
                            93.634k i/100ms
    -------------------------------------------------
    project_guest_rules without method cache
                              2.865M (±32.5%) i/s -     11.982M
    project_guest_rules with method cache
                              4.419M (± 7.4%) i/s -     22.004M
    
    Comparison:
    project_guest_rules with method cache:  4418908.0 i/s
    project_guest_rules without method cache:  2864514.0 i/s - 1.54x slower
    
    Calculating -------------------------------------
    project_report_rules without method cache
                            53.126k i/100ms
    project_report_rules with method cache
                            97.473k i/100ms
    -------------------------------------------------
    project_report_rules without method cache
                              1.093M (±36.5%) i/s -      4.675M
    project_report_rules with method cache
                              4.420M (± 7.2%) i/s -     22.029M
    Comparison:
    project_report_rules with method cache:  4420054.3 i/s
    project_report_rules without method cache:  1092509.6 i/s - 4.05x slower
    ```
    
    https://gist.github.com/huacnlee/b04788ae6df42fe769e4
    03a40a7e
ability.rb 9.54 KB