Slide 1

Slide 1 text

Erik Michaels-Ober (@sferik) RUG::B, 7 May 2015 Symbols

Slide 2

Slide 2 text

Symbols are Useless 1. Calling Symbol#to_s on every symbol in your program would not change its semantics. 2. Symbols are an archaic optimization. They were useful 20 years ago, but not today. 3. Symbols and strings have converged into one type.

Slide 3

Slide 3 text

"string".freeze.object_id
 ==
 "string".freeze.object_id
 # false on Ruby < 2.1 # true on Ruby >= 2.1 Symbols are ❄ Strings

Slide 4

Slide 4 text

def measure init = Time.now yield if block_given? return Time.now - init end Benchmark

Slide 5

Slide 5 text

N = 1_000_000 measure { N.times { "string" } } measure { N.times { "string".freeze } } measure { N.times { :symbol } } Benchmark

Slide 6

Slide 6 text

string: 0.126956 freeze: 0.056473 symbol: 0.056416 Results

Slide 7

Slide 7 text

1. Symbols and frozen strings perform identically. 2. Allocating a million strings takes about twice as long as allocating one string, putting it in into a hash table, and looking it up a million times. 3. You can allocate a million short strings in about a tenth of a second on a typical 2015 laptop computer. Takeaways

Slide 8

Slide 8 text

PowerBook 190 33 MHz CPU 8 MB RAM 500 MB HDD $1850 August 1995

Slide 9

Slide 9 text

Ruby makes me happy.

Slide 10

Slide 10 text

Symbols make me sad.

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

:foo == "foo" # true Proposal !"#$%