Created
March 29, 2023 11:57
-
-
Save casperisfine/872f0a486b5ccdf90d9feb830c76d9ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
begin | |
require "bundler/inline" | |
rescue LoadError => e | |
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler" | |
raise e | |
end | |
gemfile(true) do | |
source "https://rubygems.org" | |
gem "activemodel", path: "/Users/byroot/src/github.com/Shopify/rails" | |
gem "benchmark-ips" | |
end | |
require 'active_model' | |
class InscribingType | |
def changed_in_place?(raw_old_value, new_value) | |
false | |
end | |
def cast(value) | |
value | |
end | |
def serialize(value) | |
value | |
end | |
def deserialize(value) | |
value | |
end | |
end | |
type = InscribingType.new | |
ActiveModel::Attribute.optimization_enabled = !!ENV["ENABLE_OPT"] | |
Benchmark.ips do |x| | |
x.report("#value (#{ActiveModel::Attribute.optimization_enabled ? 'opt' : 'orig'})") do | |
attribute = ActiveModel::Attribute.from_database(nil, "a value", type) | |
# We mix calls here to cause shape transitions, hence cache misses | |
attribute.value | |
attribute.value | |
attribute.value_for_database | |
attribute.value | |
attribute.value_for_database | |
end | |
x.save!(RubyVM::YJIT.enabled? ? "/tmp/attr-bench-yjit.dump" : "/tmp/attr-bench.dump") | |
x.compare!(order: :baseline) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ENABLE_OPT=1 ruby -v /tmp/attribute-bench.rb | |
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [arm64-darwin22] | |
Warming up -------------------------------------- | |
#value (opt) 205.105k i/100ms | |
Calculating ------------------------------------- | |
#value (opt) 2.045M (± 0.8%) i/s - 10.255M in 5.014536s | |
Comparison: | |
#value (orig): 1811240.2 i/s | |
#value (opt): 2045238.9 i/s - 1.13x faster |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ENABLE_OPT=1 ruby --yjit -v /tmp/attribute-bench.rb | |
ruby 3.2.1 (2023-02-08 revision 31819e82c8) +YJIT [arm64-darwin22] | |
Warming up -------------------------------------- | |
#value (opt) 508.880k i/100ms | |
Calculating ------------------------------------- | |
#value (opt) 6.347M (± 1.4%) i/s - 32.059M in 5.051888s | |
Comparison: | |
#value (orig): 4379180.3 i/s | |
#value (opt): 6347280.7 i/s - 1.45x faster |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment