Skip to content

Instantly share code, notes, and snippets.

@elia
Created April 11, 2017 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elia/66341dae29adf153c912f672e0eff354 to your computer and use it in GitHub Desktop.
Save elia/66341dae29adf153c912f672e0eff354 to your computer and use it in GitHub Desktop.
Opal: constants system rewrite (in two parts) #1653

from: opal/opal#1653

master

commit 447e1b4cb313de4bd416f1184b27fcee8cd5edd8
Author: Elia Schito <elia@schito.me>
Date:   Wed Apr 12 00:00:35 2017 +0200
    Establish a constant lookup IPS benchmark


bundle exec opal -ropal/platform -gbenchmark-ips -rbenchmark/ips -A /Users/elia/Code/opal/benchmark-ips/bm_constants_lookup.rb
Warming up --------------------------------------
              Kernel    26.286k i/100ms
            ::Kernel    26.547k i/100ms
                   B    24.406k i/100ms
                B::C    23.660k i/100ms
Calculating -------------------------------------
              Kernel      2.566M (± 7.4%) i/s -     12.749M in   4.998786s
            ::Kernel      3.589M (± 8.2%) i/s -     17.786M in   4.996683s
                   B      1.507M (± 6.6%) i/s -      7.517M in   5.012634s
                B::C      1.401M (± 6.2%) i/s -      6.980M in   5.003246s

Comparison:
            ::Kernel:  3588532.5 i/s
              Kernel:  2566471.4 i/s - 1.40x  slower
                   B:  1506741.2 i/s - 2.38x  slower
                B::C:  1400891.8 i/s - 2.56x  slower


after making it correct

commit 547ddd43bf7fdd11505defc1c4c621c2a424c101
Author: Elia Schito <elia@schito.me>
Date:   Tue Apr 11 09:05:15 2017 +0000

    Rewrite the constants system
    
    Still needs some fixes for proper autoloading checks and to go back to
    acceptable speed.
    
    Special thanks to:
    
    - The Rails autoloading guide (http://guides.rubyonrails.org/v5.0/autoloading_and_reloading_constants.html)
    - @ ConradIrwin's 2012 post on “Everything you ever wanted to know about constant lookup in Ruby” (http://cirw.in/blog/constant-lookup.html)


bundle exec opal -ropal/platform -gbenchmark-ips -rbenchmark/ips -A /Users/elia/Code/opal/benchmark-ips/bm_constants_lookup.rb
Warming up --------------------------------------
              Kernel     5.589k i/100ms
            ::Kernel     6.661k i/100ms
                   B     6.581k i/100ms
                B::C     6.604k i/100ms
Calculating -------------------------------------
              Kernel    348.624k (± 6.6%) i/s -      1.738M in   5.009213s
            ::Kernel      6.576M (±10.4%) i/s -     31.926M in   4.923825s
                   B      3.900M (± 9.9%) i/s -     19.072M in   4.954285s
                B::C      3.306M (±10.0%) i/s -     16.186M in   4.960673s
Comparison:
            ::Kernel:  6576348.2 i/s
                   B:  3900073.1 i/s - 1.69x  slower
                B::C:  3305787.1 i/s - 1.99x  slower
              Kernel:   348624.0 i/s - 18.86x  slower


after making it fast

commit effc86f757846bd177fa2e5e943ee7e879f8d7f8
Author: Elia Schito <elia@schito.me>
Date:   Tue Apr 11 21:58:09 2017 +0000

    Use a weak constant cache
    
    - It will expire every time a constant is defined, removed or set.
    - For relative lookups the cache will be held on the current nesting.
    - For qualified lookups the cache will be held on the current cref.
    - Only the referenced constants will be cached.
    
    There's a slight risk of memory leak if constants are set dynamically
    and their unreferenced, but seems a good overall compromise that can
    only be resolved by a WeakRef JS implementation.

bundle exec opal -ropal/platform -gbenchmark-ips -rbenchmark/ips -A /Users/elia/Code/opal/benchmark-ips/bm_constants_lookup.rb
Warming up --------------------------------------
              Kernel   100.685k i/100ms
            ::Kernel    98.575k i/100ms
                   B    99.922k i/100ms
                B::C    91.044k i/100ms
Calculating -------------------------------------
              Kernel      6.930M (± 7.5%) i/s -     34.434M in   5.004191s
            ::Kernel      6.611M (± 5.3%) i/s -     33.023M in   5.010747s
                   B      7.259M (± 6.0%) i/s -     36.172M in   5.002918s
                B::C      5.212M (± 6.5%) i/s -     25.948M in   5.001865s

Comparison:
                   B:  7259023.0 i/s
              Kernel:  6929763.2 i/s - same-ish: difference falls within error
            ::Kernel:  6611188.0 i/s - same-ish: difference falls within error
                B::C:  5211583.8 i/s - 1.39x  slower



it's a 50% to almost 80% improvement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment