Skip to content

Instantly share code, notes, and snippets.

@casperisfine
Created March 30, 2021 22:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save casperisfine/e4c2fb3357b727882e625490ece87bd2 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
require 'active_support/all'
class CVarBase
mattr_accessor :legacy_connection_handling, instance_writer: false, default: true
def self.connection_handlers
unless legacy_connection_handling
raise NotImplementedError, "The new connection handling does not support accessing multiple connection handlers."
end
@@connection_handlers ||= {}
end
def self.connection_handlers=(handlers)
unless legacy_connection_handling
raise NotImplementedError, "The new connection handling does not setting support multiple connection handlers."
end
@@connection_handlers = handlers
end
end
class CVarChild < CVarBase
end
class RefactorBase
module Config
extend self
attr_accessor :legacy_connection_handling
@legacy_connection_handling = true
attr_accessor :connection_handlers
@connection_handlers = {}
end
def self.legacy_connection_handling
Config.legacy_connection_handling
end
def self.legacy_connection_handling=(enabled)
if (Config.legacy_connection_handling = enabled)
Config.connection_handlers ||= {}
else
Config.connection_handlers = false
end
end
def self.connection_handlers
Config.connection_handlers or raise NotImplementedError, "The new connection handling does not setting support multiple connection handlers."
end
def self.connection_handlers=(handlers)
unless legacy_connection_handling
raise NotImplementedError, "The new connection handling does not setting support multiple connection handlers."
end
Config.connection_handlers = handlers
end
end
class RefactorChild < RefactorBase
end
Benchmark.ips do |x|
x.report('cvar') { CVarBase.connection_handlers }
x.report('const_ivar') { RefactorBase.connection_handlers }
x.compare!
end
Warming up --------------------------------------
cvar 538.684k i/100ms
const_ivar 1.463M i/100ms
Calculating -------------------------------------
cvar 5.298M (± 1.6%) i/s - 26.934M in 5.085454s
const_ivar 14.427M (± 2.3%) i/s - 73.140M in 5.072354s
Comparison:
const_ivar: 14427114.5 i/s
cvar: 5297607.2 i/s - 2.72x (± 0.00) slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment