Trust Unwrapped - A story of ethics, integrity and chocolate
Attributes Unwrapped: Lessons under the surface of active record
-
Upload
toster -
Category
Technology
-
view
1.006 -
download
1
description
Transcript of Attributes Unwrapped: Lessons under the surface of active record
![Page 1: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/1.jpg)
Attributes Unwrapped
![Page 2: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/2.jpg)
@jonleighton
![Page 3: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/3.jpg)
![Page 4: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/4.jpg)
I lied to you
![Page 5: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/5.jpg)
![Page 6: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/6.jpg)
Measure
![Page 7: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/7.jpg)
Measure
Refactor
![Page 8: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/8.jpg)
Measure
Refactor
Optimise
![Page 9: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/9.jpg)
LET’S DO SCIENCE
![Page 10: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/10.jpg)
gem 'rails', '3.2.0'
require 'active_record'
ActiveRecord::Base
.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:')
![Page 11: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/11.jpg)
ActiveRecord::Schema.define do
create_table :posts do |t|
t.string :title
end
end
class Post < ActiveRecord::Base
end
p = Post.create(:title => "lol")
![Page 12: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/12.jpg)
require 'benchmark'
n = 1_000_000
Benchmark.report(20) do |r|
r.report('attribute') do
n.times { p.title }
end
r.report('read_attribute') do
n.times { p[:title] }
end
end
![Page 13: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/13.jpg)
attribute
read_attribute
1.09 s
2.87 s
![Page 14: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/14.jpg)
gem install
benchmark_suite
![Page 15: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/15.jpg)
require 'benchmark/ips'
Benchmark.ips do |r|
r.report('attribute') do
p.title
end
r.report('read_attribute') do
p[:title]
end
end
![Page 16: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/16.jpg)
attribute
read_attribute
828,648
299,856
![Page 17: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/17.jpg)
Ruby 1.9
![Page 18: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/18.jpg)
Ruby 1.8
![Page 19: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/19.jpg)
Ruby 1.8
oops!
![Page 20: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/20.jpg)
define_method
![Page 21: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/21.jpg)
method compilation
![Page 22: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/22.jpg)
![Page 23: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/23.jpg)
![Page 24: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/24.jpg)
create_table :roflcopters do |t|
t.string " ROFL:ROFL:ROFL:ROFL"
t.string " _^____ "
t.string " L __/ []\ "
t.string "LOL===_ \ "
t.string " L \_________] "
t.string " I I "
t.string " --------/ "
end
![Page 25: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/25.jpg)
if compilable?
class_eval <<-STR
def #{attr_name}
...
end
STR
else
define_method attr_name do
...
end
end
![Page 26: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/26.jpg)
attr_name
=~
/\A[a-zA-Z_]\w*[!?=]?\z/
![Page 27: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/27.jpg)
:title
=~
/\A[a-zA-Z_]\w*[!?=]?\z/
![Page 28: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/28.jpg)
:title =~ /.../
# => true
Ruby 1.9
![Page 29: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/29.jpg)
Ruby 1.8
:title =~ /.../
# => false
![Page 30: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/30.jpg)
def __temp__
...
end
alias "@#>" :__temp__
undef_method :__temp__
![Page 31: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/31.jpg)
def __temp__
...
end
alias "@#>" :__temp__
undef_method :__temp__
DON'T USE THIS
![Page 32: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/32.jpg)
API Changes
![Page 33: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/33.jpg)
![Page 34: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/34.jpg)
def title
self[:title].upcase
end
![Page 35: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/35.jpg)
def title
super.upcase
end
![Page 36: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/36.jpg)
module A
def foo
"bar"
end
end
![Page 37: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/37.jpg)
class B
include A
def foo
super.upcase
end
end
![Page 38: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/38.jpg)
Don’t fight Ruby
<3 <3 <3
![Page 39: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/39.jpg)
#read_attribute
#[]
![Page 40: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/40.jpg)
def read_attribute(name)
name = "_#{name}"
if respond_to?(name)
send(name)
else
# other stuff
end
end
![Page 41: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/41.jpg)
Module.new
![Page 42: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/42.jpg)
def read_attribute(name)
mod = self.class.methods_module
if mod.respond_to?(name)
mod.send(name, @attributes)
else
# other stuff
end
end
![Page 43: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/43.jpg)
Module.new.respond_to?(:name)
# => true
![Page 44: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/44.jpg)
Module.new { extend self }
![Page 45: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/45.jpg)
Module.new { extend self }
mod.method_defined?(:name)
![Page 46: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/46.jpg)
Module.new { extend self }
INSANE HACKmod.method_defined?(:name)
![Page 47: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/47.jpg)
Still too slow☹
![Page 48: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/48.jpg)
gem install
perftools.rb
![Page 49: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/49.jpg)
![Page 50: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/50.jpg)
if attr_name == 'id'
attr_name =
self.class.primary_key
end
![Page 51: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/51.jpg)
No code is fasterthan no code
![Page 52: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/52.jpg)
def title
cast @attributes['title']
end
![Page 53: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/53.jpg)
def title
cast @attributes[:title]
end
![Page 54: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/54.jpg)
![Page 55: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/55.jpg)
class A
def initialize
@attributes = { :foo => 1 }
end
def foo
@attributes[:foo]
end
end
![Page 56: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/56.jpg)
class B
def initialize
@attributes = { 'foo' => 1 }
end
def foo
@attributes['foo']
end
end
![Page 57: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/57.jpg)
Benchmark.ips do |r|
r.report('symbol') { a.foo }
r.report('string') { b.foo }
end
![Page 58: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/58.jpg)
![Page 59: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/59.jpg)
code = "@attributes['foo']"
iseq =
RubyVM::InstructionSequence
.compile(code)
puts iseq.disassemble
![Page 60: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/60.jpg)
trace 1
getinstancevariable :@attributes
putstring "foo"
opt_aref <ic:2>
leave
![Page 61: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/61.jpg)
trace 1
getinstancevariable :@attributes
putobject :foo
opt_aref <ic:2>
leave
![Page 62: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/62.jpg)
DEFINE_INSN
putstring
(VALUE str)
()
(VALUE val)
{
val = rb_str_resurrect(str);
}
![Page 63: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/63.jpg)
DEFINE_INSN
putobject
(VALUE val)
()
(VALUE val)
{
/* */
}
![Page 64: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/64.jpg)
![Page 65: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/65.jpg)
code = "@attributes['foo']"
compiled =
Rubinius::Compiler
.compile_string(code)
puts compiled.decode
![Page 66: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/66.jpg)
push_ivar 0
push_literal "foo"
string_dup
send_stack :[], 1
pop
push_true
ret
![Page 67: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/67.jpg)
push_ivar 0
push_literal :foo
send_stack :[], 1
pop
push_true
ret
![Page 68: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/68.jpg)
![Page 69: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/69.jpg)
jruby --bytecode
-e "@attributes['foo']"
![Page 70: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/70.jpg)
RubyString
![Page 71: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/71.jpg)
RubyString
RubySymbol
![Page 72: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/72.jpg)
Performance problemsare a code smell
![Page 73: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/73.jpg)
Be a scientist
![Page 74: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/74.jpg)
But...
![Page 75: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/75.jpg)
Avoid roflscaling!
![Page 76: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/76.jpg)
Thanks!IT'S OVER!
![Page 77: Attributes Unwrapped: Lessons under the surface of active record](https://reader033.fdocuments.net/reader033/viewer/2022060109/5555a75ed8b42afe5d8b457e/html5/thumbnails/77.jpg)
Thanks!IT'S OVER!
(♥ @tenderlove ♥)