Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. franckverrot created this gist May 26, 2016.
    42 changes: 42 additions & 0 deletions 0001-parse.y-Replace-squotes-with-dquotes.patch
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    From 739265f2f2c54d8be56ad58d7c863193f0ce472d Mon Sep 17 00:00:00 2001
    From: Franck Verrot <franck@verrot.fr>
    Date: Thu, 26 May 2016 10:12:29 -0700
    Subject: [PATCH] * parse.y : Replace squotes with dquotes

    ---
    parse.y | 6 +++---
    1 file changed, 3 insertions(+), 3 deletions(-)

    diff --git a/parse.y b/parse.y
    index 005e926..1752d3b 100644
    --- a/parse.y
    +++ b/parse.y
    @@ -6560,7 +6560,7 @@ parser_heredoc_identifier(struct parser_params *parser)
    }
    switch (c) {
    case '\'':
    - func |= str_squote; goto quoted;
    + func |= str_dquote; goto quoted;
    case '"':
    func |= str_dquote; goto quoted;
    case '`':
    @@ -7739,7 +7739,7 @@ parse_percent(struct parser_params *parser, const int space_seen, const enum lex
    return tSTRING_BEG;

    case 'q':
    - lex_strterm = NEW_STRTERM(str_squote, term, paren);
    + lex_strterm = NEW_STRTERM(str_dquote, term, paren);
    return tSTRING_BEG;

    case 'W':
    @@ -8411,7 +8411,7 @@ parser_yylex(struct parser_params *parser)

    case '\'':
    label = (IS_LABEL_POSSIBLE() ? str_label : 0);
    - lex_strterm = NEW_STRTERM(str_squote | label, '\'', 0);
    + lex_strterm = NEW_STRTERM(str_dquote | label, '\'', 0);
    return tSTRING_BEG;

    case '?':
    --
    2.7.3
    35 changes: 35 additions & 0 deletions concatenation.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # courtesy of https://github.com/JuanitoFatas/fast-ruby/blob/master/code/string/concatenation.rb
    require 'benchmark/ips'

    # 2 + 1 = 3 object
    def slow_plus
    'foo' + 'bar'
    end

    # 2 + 1 = 3 object
    def slow_concat
    'foo'.concat 'bar'
    end

    # 2 + 1 = 3 object
    def slow_append
    'foo' << 'bar'
    end

    # 1 object
    def fast
    'foo' 'bar'
    end

    def fast_interpolation
    "#{'foo'}#{'bar'}"
    end

    Benchmark.ips do |x|
    x.report('String#+') { slow_plus }
    x.report('String#concat') { slow_concat }
    x.report('String#append') { slow_append }
    x.report('"foo" "bar"') { fast }
    x.report('"#{\'foo\'}#{\'bar\'}"') { fast_interpolation }
    x.compare!
    end
    20 changes: 20 additions & 0 deletions with-patch.log
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    λ ruby concatenation.rb
    Warming up --------------------------------------
    String#+ 58.167k i/100ms
    String#concat 59.549k i/100ms
    String#append 60.170k i/100ms
    "foo" "bar" 79.179k i/100ms
    "#{'foo'}#{'bar'}" 81.362k i/100ms
    Calculating -------------------------------------
    String#+ 999.051k (± 3.8%) i/s - 5.002M in 5.014765s
    String#concat 1.082M (± 4.8%) i/s - 5.419M in 5.022983s
    String#append 1.140M (± 4.3%) i/s - 5.716M in 5.022578s
    "foo" "bar" 1.826M (± 4.8%) i/s - 9.106M in 4.999979s
    "#{'foo'}#{'bar'}" 1.789M (± 7.7%) i/s - 8.950M in 5.038771s

    Comparison:
    "foo" "bar": 1825544.4 i/s
    "#{'foo'}#{'bar'}": 1788974.3 i/s - same-ish: difference falls within error
    String#append: 1140399.8 i/s - 1.60x slower
    String#concat: 1081619.8 i/s - 1.69x slower
    String#+: 999051.2 i/s - 1.83x slower
    20 changes: 20 additions & 0 deletions without-patch.log
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    ~ [○] λ ruby /tmp/concatenation.rb
    Warming up --------------------------------------
    String#+ 56.174k i/100ms
    String#concat 59.173k i/100ms
    String#append 62.512k i/100ms
    "foo" "bar" 81.058k i/100ms
    "#{'foo'}#{'bar'}" 81.225k i/100ms
    Calculating -------------------------------------
    String#+ 964.524k (± 4.5%) i/s - 4.831M in 5.019469s
    String#concat 1.060M (± 4.6%) i/s - 5.326M in 5.036661s
    String#append 1.129M (± 4.1%) i/s - 5.689M in 5.048866s
    "foo" "bar" 1.825M (± 4.5%) i/s - 9.160M in 5.030873s
    "#{'foo'}#{'bar'}" 1.818M (± 4.9%) i/s - 9.097M in 5.018021s

    Comparison:
    "foo" "bar": 1824600.9 i/s
    "#{'foo'}#{'bar'}": 1817629.8 i/s - same-ish: difference falls within error
    String#append: 1128802.6 i/s - 1.62x slower
    String#concat: 1059964.0 i/s - 1.72x slower
    String#+: 964524.3 i/s - 1.89x slower