Commit graph

1165 commits

Author SHA1 Message Date
nagachika
a2fe4b75e4 merge revision(s) 217df51f0e: [Backport #18232]
Dump outer variables tables when dumping an iseq to binary

	This commit dumps the outer variables table when dumping an iseq to
	binary.  This fixes a case where Ractors aren't able to tell what outer
	variables belong to a lambda after the lambda is loaded via ISeq.load_from_binary

	[Bug #18232] [ruby-core:105504]
	---
	 compile.c              | 55 +++++++++++++++++++++++++++++++++++++++++++++++++-
	 test/ruby/test_iseq.rb | 10 +++++++++
	 2 files changed, 64 insertions(+), 1 deletion(-)
2021-10-16 13:26:47 +09:00
nagachika
31816356ee merge revision(s) 2bc293e899c9d32dcd794a73de8925c49ecf8f15,d0e6c6e682b9ba2b0309a5177933a0628e8ef316,cc0dc67bbbe1951ff90004bc987f78545625d772,e1eff837cf12a8e813de9d4ff2db50c9b68b86b5,0ab0b86c8491d639b9ff1335ddf35e341ecd867e,6911b4bc82889860ff7da4ecf975504cdc3e5314: [Backport #17854]
cdhash_cmp: can take rational literals

	Rational literals are those integers suffixed with `r`.  They tend to
	be a part of more complex expressions like `123/456r`, but in theory
	they can live alone.  When such "bare" rational literals are passed to
	case-when branch, we have to take care of them.  Fixes [Bug #17854]
	---
	 common.mk                  |  1 +
	 compile.c                  |  7 +++++++
	 internal/rational.h        |  1 +
	 rational.c                 | 13 ++++++++++---
	 test/ruby/test_rational.rb |  7 +++++++
	 5 files changed, 26 insertions(+), 3 deletions(-)

	cdhash_cmp: rational literals with fractions

	Nobu kindly pointed out that rational literals can have fractions.
	---
	 compile.c                  | 5 +++--
	 test/ruby/test_rational.rb | 4 ++++
	 2 files changed, 7 insertions(+), 2 deletions(-)

	cdhash_cmp: can also take complex

	There are complex literals `123i`, which can also be a case condition.
	---
	 compile.c                  | 15 +++++++++++----
	 complex.c                  | 12 +++++++++---
	 internal/complex.h         |  1 +
	 test/ruby/test_rational.rb |  4 ++++
	 4 files changed, 25 insertions(+), 7 deletions(-)

	cdhash_cmp: recursively apply

	For instance a rational's numerator can be a bignum.  Comparison using
	C's == can be insufficient.
	---
	 compile.c | 4 ++--
	 1 file changed, 2 insertions(+), 2 deletions(-)

	cdhash_cmp: should use ||

	cf: https://github.com/ruby/ruby/pull/4469#discussion_r628386707
	---
	 compile.c | 4 ++--
	 1 file changed, 2 insertions(+), 2 deletions(-)

	test_cdhash: refactor change class

	It is now strange to test Complex in a class named Rational_Test.
	---
	 test/ruby/test_rational.rb | 15 ---------------
	 test/ruby/test_syntax.rb   | 15 +++++++++++++++
	 2 files changed, 15 insertions(+), 15 deletions(-)
2021-05-22 17:09:16 +09:00
nagachika
ddd720f8dc merge revision(s) 5026f9a5d5: [Backport #17868]
compile.c: stop the jump-jump optimization if the second has any
	 event

	Fixes [Bug #17868]
	---
	 compile.c                      |  3 ++-
	 test/ruby/test_settracefunc.rb | 15 +++++++++++++++
	 2 files changed, 17 insertions(+), 1 deletion(-)
2021-05-22 15:51:37 +09:00
NARUSE, Yui
2dc39e2fd4 merge revision(s) 1b89b99941: [Backport #17534]
Mark pattern labels as unremoveable

	Peephole optimization doesn't play well with find pattern at
	least. The only case when a pattern matching could have
	unreachable patterns is when we have lasgn/dasgn node, which
	shouldn't happen in real-life.

	Fixes https://bugs.ruby-lang.org/issues/17534
	---
	 compile.c                          |  2 +-
	 test/ruby/test_pattern_matching.rb | 10 ++++++++++
	 2 files changed, 11 insertions(+), 1 deletion(-)
2021-02-02 17:50:00 +09:00
NARUSE, Yui
74f90b46a9 merge revision(s) efcdf68e6443ab70fbff1703b9dabbfc5090df31,5e26619660f20272a53c7f839dde36cce034bb35: [Backport #17539]
Guard callinfo

	Callinfo was being written in to an array and the GC would not see the
	reference on the stack.  `new_insn_send` creates a new callinfo object,
	then it calls `new_insn_core`.  `new_insn_core` allocates a new INSN
	linked list item, which can end up calling `xmalloc` which will trigger
	a GC:

	  70cd351c7c/compile.c (L968-L969)

	Since the callinfo object isn't on the stack, the GC won't see it, and
	it can get collected.  This patch just refactors `new_insn_send` to keep
	the object on the stack

	Co-authored-by: John Hawthorn <john@hawthorn.email>
	---
	 compile.c            |  7 +++++--
	 test/ruby/test_gc.rb | 10 ++++++++++
	 2 files changed, 15 insertions(+), 2 deletions(-)

	Fix WB for callinfo

	The WB for callinfo needs to be executed *after* the reference is
	written.  Otherwise we get a WB miss.
	---
	 compile.c | 1 +
	 1 file changed, 1 insertion(+)
2021-02-01 21:42:11 +09:00
Koichi Sasada
b93e16dc0f enable constant cache on ractors
constant cache `IC` is accessed by non-atomic manner and there are
thread-safety issues, so Ruby 3.0 disables to use const cache on
non-main ractors.

This patch enables it by introducing `imemo_constcache` and allocates
it by every re-fill of const cache like `imemo_callcache`.
[Bug #17510]

Now `IC` only has one entry `IC::entry` and it points to
`iseq_inline_constant_cache_entry`, managed by T_IMEMO object.

`IC` is atomic data structure so `rb_mjit_before_vm_ic_update()` and
`rb_mjit_after_vm_ic_update()` is not needed.
2021-01-13 17:06:16 +09:00
Nobuyoshi Nakada
d143b75f8e
Adjusted indents [ci skip] 2020-12-25 00:56:17 +09:00
John Hawthorn
40b7358e93 Skip defined check in NODE_OP_ASGN_OR with ivar
Previously we would add code to check if an ivar was defined when using
`@foo ||= 123`, which was slower than `@foo || (@foo = 123)` when `@foo`
was already defined.

Recently 01b7d5acc7 made it so that
accessing an undefined variable no longer generates a warning, making
the defined check unnecessary and both statements exactly equal.

This commit avoids emitting the defined instruction when compiling
NODE_OP_ASGN_OR with a NODE_IVAR.

Before:

    $ ruby --dump=insn -e '@foo ||= 123'
    == disasm: #<ISeq:<main>@-e:1 (1,0)-(1,12)> (catch: FALSE)
    0000 putnil                                                           (   1)[Li]
    0001 defined                      instance-variable, :@foo, false
    0005 branchunless                 14
    0007 getinstancevariable          :@foo, <is:0>
    0010 dup
    0011 branchif                     20
    0013 pop
    0014 putobject                    123
    0016 dup
    0017 setinstancevariable          :@foo, <is:0>
    0020 leave

After:

    $ ./ruby --dump=insn -e '@foo ||= 123'
    == disasm: #<ISeq:<main>@-e:1 (1,0)-(1,12)> (catch: FALSE)
    0000 getinstancevariable                    :@foo, <is:0>             (   1)[Li]
    0003 dup
    0004 branchif                               13
    0006 pop
    0007 putobject                              123
    0009 dup
    0010 setinstancevariable                    :@foo, <is:0>
    0013 leave

This seems to be about 50% faster in this benchmark:

    require "benchmark/ips"

    class Foo
      def initialize
        @foo = nil
      end

      def test1
        @foo ||= 123
      end

      def test2
        @foo || (@foo = 123)
      end
    end

    FOO = Foo.new

    Benchmark.ips do |x|
      x.report("test1", "FOO.test1")
      x.report("test2", "FOO.test2")
    end

Before:

    $ ruby benchmark_ivar.rb
    Warming up --------------------------------------
                   test1     1.957M i/100ms
                   test2     3.125M i/100ms
    Calculating -------------------------------------
                   test1     20.030M (± 1.7%) i/s -    101.780M in   5.083040s
                   test2     31.227M (± 4.5%) i/s -    156.262M in   5.015936s

After:

    $ ./ruby benchmark_ivar.rb
    Warming up --------------------------------------
                   test1     3.205M i/100ms
                   test2     3.197M i/100ms
    Calculating -------------------------------------
                   test1     32.066M (± 1.1%) i/s -    163.440M in   5.097581s
                   test2     31.438M (± 4.9%) i/s -    159.860M in   5.098961s
2020-12-14 19:38:59 -08:00
Nobuyoshi Nakada
555bd83a8e
Raise when loading unprovided builtin function [Bug #17192] 2020-11-30 15:19:49 +09:00
Aaron Patterson
67b2c21c32
Add GC.auto_compact= true/false and GC.auto_compact
* `GC.auto_compact=`, `GC.auto_compact` can be used to control when
  compaction runs.  Setting `auto_compact=` to true will cause
  compaction to occurr duing major collections.  At the moment,
  compaction adds significant overhead to major collections, so please
  test first!

[Feature #17176]
2020-11-02 14:42:48 -08:00
wanabe
4f8d9b0db8 Revert "Use adjusted sp on iseq_set_sequence()" and "Delay remove_unreachable_chunk() after iseq_set_sequence()"
This reverts commit 3685ed7303 and 5dc107b03f.
Because of some CI failures https://github.com/ruby/ruby/pull/3404#issuecomment-719868313.
2020-10-31 11:56:41 +09:00
Nobuyoshi Nakada
dd2f99d94a
Removed unused variable 2020-10-31 10:51:57 +09:00
wanabe
3685ed7303 Use adjusted sp on iseq_set_sequence() 2020-10-31 09:18:37 +09:00
wanabe
5dc107b03f Delay remove_unreachable_chunk() after iseq_set_sequence() 2020-10-31 09:18:37 +09:00
Nobuyoshi Nakada
799253dc46
strip trailing spaces [ci skip] 2020-10-30 12:26:59 +09:00
Koichi Sasada
07c03bc309 check isolated Proc more strictly
Isolated Proc prohibit to access outer local variables, but it was
violated by binding and so on, so they should be error.
2020-10-29 23:42:55 +09:00
Kenta Murata
fb3c711df3
compile.c: separate compile_builtin_function_call (#3711) 2020-10-28 10:22:28 +09:00
Stefan Stüben
8c2e5bbf58 Don't redefine #rb_intern over and over again 2020-10-21 12:45:18 +09:00
Koichi Sasada
f6661f5085 sync RClass::ext::iv_index_tbl
iv_index_tbl manages instance variable indexes (ID -> index).
This data structure should be synchronized with other ractors
so introduce some VM locks.

This patch also introduced atomic ivar cache used by
set/getinlinecache instructions. To make updating ivar cache (IVC),
we changed iv_index_tbl data structure to manage (ID -> entry)
and an entry points serial and index. IVC points to this entry so
that cache update becomes atomically.
2020-10-17 08:18:04 +09:00
wanabe
65ae7f347a Adjust sp for if true or .../if false and ... 2020-10-16 08:37:04 +09:00
wanabe
ce7a053475 Adjust sp for x = false; y = (return until x unless x) [Bug #16695] 2020-10-16 08:37:04 +09:00
Aaron Patterson
d528254095
Add missing WB for iseq
The write barrier wasn't being called for this object, so add the
missing WB.  Automatic compaction moved the reference because it didn't
know about the relationship (that's how I found the missing WB).
2020-10-07 17:01:14 -07:00
Nobuyoshi Nakada
fced98f464
Added the room for builtin inline prefix 2020-10-03 12:19:56 +09:00
Nobuyoshi Nakada
5a665f6ce7
Check builtin inline function index overflow 2020-10-03 10:47:24 +09:00
Koichi Sasada
8d76b729a1 Put same frozen Range literal if possible
Range literal is now frozen so we can reuse same Range object if
the begin and the last are Numeric (frozen), such as `(1..2)`.
2020-10-02 09:22:17 +09:00
Nobuyoshi Nakada
7b2bea42a2
Unfreeze string-literal-only interpolated string-literal
[Feature #17104]
2020-09-30 22:15:28 +09:00
Benoit Daloze
9b535f3ff7 Interpolated strings are no longer frozen with frozen-string-literal: true
* Remove freezestring instruction since this was the only usage for it.
* [Feature #17104]
2020-09-15 21:32:35 +02:00
Koichi Sasada
79df14c04b Introduce Ractor mechanism for parallel execution
This commit introduces Ractor mechanism to run Ruby program in
parallel. See doc/ractor.md for more details about Ractor.
See ticket [Feature #17100] to see the implementation details
and discussions.

[Feature #17100]

This commit does not complete the implementation. You can find
many bugs on using Ractor. Also the specification will be changed
so that this feature is experimental. You will see a warning when
you make the first Ractor with `Ractor.new`.

I hope this feature can help programmers from thread-safety issues.
2020-09-03 21:11:06 +09:00
Nobuyoshi Nakada
a90f29ebb2
procnames-start-lines [ci skip] 2020-08-17 14:27:34 +09:00
Nobuyoshi Nakada
352e923242
Revisit "Refactor to reduce "swap" instruction of pattern matching"
Just moved "case base" after allocating cache space.
2020-08-17 14:25:09 +09:00
Kazuhiro NISHIYAMA
5849309c5a
Revert "Refactor to reduce "swap" instruction of pattern matching"
This reverts commit 3a4be429b5.

To fix following warning:

```
compiling ../compile.c
../compile.c:6336:20: warning: variable 'line' is uninitialized when used here [-Wuninitialized]
    ADD_INSN(head, line, putnil); /* allocate stack for cached #deconstruct value */
                   ^~~~
../compile.c:220:57: note: expanded from macro 'ADD_INSN'
  ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (line), BIN(insn), 0))
                                                        ^~~~
../compile.c:6327:13: note: initialize the variable 'line' to silence this warning
    int line;
            ^
             = 0
1 warning generated.
```
2020-08-17 09:28:15 +09:00
wanabe
3a4be429b5 Refactor to reduce "swap" instruction of pattern matching 2020-08-16 18:53:39 +09:00
wanabe
5c40c88a3e Adjust sp for case ... in a: 0 ... end 2020-08-16 18:39:08 +09:00
wanabe
691f10dd89 Adjust sp for case ... in *, a, * end 2020-08-16 18:39:08 +09:00
wanabe
6c407b3668 Adjust sp for case ... in *v end/case ... in v1, v2 end 2020-08-16 18:39:08 +09:00
wanabe
c866d6563f Adjust sp for case ... in v1 ... in v2 end 2020-08-16 18:39:08 +09:00
wanabe
d594078426 Adjust sp for case ... in v1, v2 ... end 2020-08-16 18:39:08 +09:00
wanabe
2bbb7c3d1f Adjust sp for case ... in pat => var ... end 2020-08-16 18:39:08 +09:00
wanabe
6bc0c6c18b Adjust sp for case ... in pat1 | pat2 ... end 2020-08-16 18:39:08 +09:00
wanabe
0759862458 Adjust sp for pattern matching implicit/explicit "else" 2020-08-16 18:39:08 +09:00
wanabe
a7bd0ec570 Warn sp overwriting on compile time 2020-08-16 08:43:29 +09:00
wanabe
ac399c2c7a Show hidden object and TS_BUILTIN for halfbaked insn data 2020-08-16 08:43:29 +09:00
Koichi Sasada
a0f12a0258
Use ID instead of GENTRY for gvars. (#3278)
Use ID instead of GENTRY for gvars.

Global variables are compiled into GENTRY (a pointer to struct
rb_global_entry). This patch replace this GENTRY to ID and
make the code simple.

We need to search GENTRY from ID every time (st_lookup), so
additional overhead will be introduced.
However, the performance of accessing global variables is not
important now a day and this simplicity helps Ractor development.
2020-07-03 16:56:44 +09:00
卜部昌平
bacd03ebdf compile_redo: fix wrong condition 2020-06-29 11:05:41 +09:00
卜部昌平
9c92dcf366 ibf_dump_object_object: do not goto into a branch
I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.
2020-06-29 11:05:41 +09:00
卜部昌平
a8d992ac00 compile_call: do not goto into a branch
I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.
2020-06-29 11:05:41 +09:00
卜部昌平
aa2cb7f722 compile_redo: do not goto into a branch
I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.
2020-06-29 11:05:41 +09:00
卜部昌平
cf29de7e6e compile_next: do not goto into a branch
I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.
2020-06-29 11:05:41 +09:00
卜部昌平
cc1e9b8e11 compile_break: do not goto into a branch
I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.
2020-06-29 11:05:41 +09:00
卜部昌平
1f90690a1d compile_branch_condition: do not goto into a branch
I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.
2020-06-29 11:05:41 +09:00