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

Speed up setting instance variables #10

Closed
wants to merge 1 commit into from
Closed

Commits on May 14, 2020

  1. Speed up setting instance variables

    This patch speeds up setting instance variables somewhat.  Here is the
    benchmark:
    
    ```ruby
    class IVSetter
      def init
        @A = @b = @c = @d = nil
      end
    end
    
    obj = IVSetter.new
    
    require "benchmark/ips"
    
    Benchmark.ips do |x|
      x.report("init") do
        obj.init
      end
    end
    ```
    
    Before this patch:
    
    ```
    [aaron@tc-lan-adapter ~/g/ruby (master)]$ ./ruby -v ../y.rb
    ruby 2.8.0dev (2020-05-13T15:51:04Z master 65c5a39) [x86_64-darwin19]
    Warming up --------------------------------------
                    init     1.324M i/100ms
    Calculating -------------------------------------
                    init     13.034M (± 1.6%) i/s -     66.210M in   5.080905s
    ```
    
    After this patch:
    
    ```
    [aaron@tc-lan-adapter ~/g/ruby (faster-ivar-set)]$ ./ruby -v ../y.rb
    ruby 2.8.0dev (2020-05-14T19:33:13Z faster-ivar-set 60cfbb9d7e) [x86_64-darwin19]
    Warming up --------------------------------------
                    init     1.456M i/100ms
    Calculating -------------------------------------
                    init     14.599M (± 1.5%) i/s -     74.270M in   5.088467s
    ```
    
    This speeds up ivar sets because we can reduce redundant checks.
    `ROBJECT_NUMIV` and `ROBJECT_IVPTR` both check that the object is a
    `T_OBJECT`, but the caller (`vm_setivar`) already knows this is a
    `T_OBJECT` (otherwise this branch wouldn't be executing).
    
    Secondly, both `ROBJECT_IVPTR` and `ROBJECT_NUMIV` check flags for
    `ROBJECT_EMBED`.  We can reduce the check to just once.
    
    I thought the C compiler would be smart enough to eliminate these
    redundant checks, but after reading the assembly code it's clear that
    the checks are being performed multiple times.
    tenderlove committed May 14, 2020
    Configuration menu
    Copy the full SHA
    43b6fe9 View commit details
    Browse the repository at this point in the history