$30 off During Our Annual Pro Sale. View Details »

💀 Symbols

💀 Symbols

⚡️ Talk based on a Hacker News comment (https://news.ycombinator.com/item?id=9197412). Presented at RubyConf India and the Ruby User Group Berlin.

Erik Berlin

April 07, 2015
Tweet

More Decks by Erik Berlin

Other Decks in Programming

Transcript

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

    View Slide

  2. 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.

    View Slide

  3. "string".freeze.object_id

    ==

    "string".freeze.object_id

    # false on Ruby < 2.1
    # true on Ruby >= 2.1
    Symbols are ❄ Strings

    View Slide

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

    View Slide

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

    View Slide

  6. string: 0.126956
    freeze: 0.056473
    symbol: 0.056416
    Results

    View Slide

  7. 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

    View Slide

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

    View Slide

  9. Ruby makes me happy.

    View Slide

  10. Symbols make me sad.

    View Slide

  11. View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. View Slide

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

    View Slide