Talk at DevConTLV about how to make your dynamic language implementation faster. What are some basic techniques used and how do they work.
The myth of dynamiclanguage performanceDevConTLV
View Slide
Dirkjan Bussink@dbussink
RubiniusA Ruby implementation
“PHP too slow for Facebook,builds its own faster version”“Everything you heard about Ruby being slowis not true. It’s about twice as slow as that”“How JavaScript is Slowing Down the Web”2006
Static vs. Dynamic
Weak vs. Strong
C PHPJava RubyStatic DynamicWeakStrong
The lure of the hash table
class MethodTabledef initialize@methods = {}enddef store_method(name, code)@methods[name] = codeenddef find_method(name)@methods[name]endend
class Address {private String street;private Integer number;private String city;public Address(String street, Integer number, String city) {this.street = street;this.number = number;this.city = city;}}
class Address {private HashTable instance_variables;public Address(String street, Integer number, String city) {instance_variables = new HashTable();instance_variables.put("street", street);instance_variables.put("number", number);instance_variables.put("city", city);}}
Hash tables everywhere!
Inline caching
p = Person.newp.name
Specific object layout
class Addressattr_reader :streetattr_reader :numberattr_reader :cityendAddress.instance_variable_get("@seen_ivars")=> [:@street, :@number, :@city]
0001: push_ivar :@numberself[1]Removes hashtable lookup
Just in time compilation
def method11 + method2enddef method22 + 1end100000.times domethod1end...0x110ed90e7 mov $0x9, %eax0x110ed90ec jmp 0x119 ; 0x110ed9129...0x110ed9129 addq $0xa0, %rsp0x110ed9130 pop %rbp0x110ed9131 ret1 + 2 + 1 = 4FIXNUM(4) = 0x9
What about youas a developer?
Be nice
Write type stable code
Small and simple methods
Benchmark!
Recap
Fast dynamic languagesare harder, not impossible
Writing simple and wellfactored code is ofteneasy to optimize for a VM
?