[ruby-core:80290] [Ruby trunk Feature#13355] [PATCH] compile.c: optimize literal String range in case/when dispatch

From: normalperson@...
Date: 2017-03-23 08:28:28 UTC
List: ruby-core #80290
Issue #13355 has been reported by normalperson (Eric Wong).

----------------------------------------
Feature #13355:  [PATCH] compile.c: optimize literal String range in case/when dispatch
https://bugs.ruby-lang.org/issues/13355

* Author: normalperson (Eric Wong)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
This seems obvious, and manages to pass existing tests.
Earlier, I found myself writing code like:

    case RUBY_VERSION
    when '2.0.0',
         '2.1.0'..'2.1.9',
         '2.2.0'..'2.2.6',
         '2.3.0'..'2.3.3',
         '2.4.0'
      # install workaround
    else
      puts "good!"
    end

And figured other people might write similar code somewhere.



This is similar in spirit to opt_case_dispatch as the literal
Range here is guaranteed to be immutable when used for
checkmatch.

Normal range literals with non-frozen strings are actually
mutable, as Range#begin and Range#end exposes the strings to
modification.  So those Range objects cannot be frozen without
breaking compatibility, but Ranges in case/when dispatch can be
frozen at compile time.

* compile.c (iseq_peephole_optimize): persistent Range creation
  when String literals are used as beginning and end of range
  when used for case/when dispatch.


---Files--------------------------------
0001-compile.c-optimize-literal-String-range-in-case-when.patch (2.91 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next