Commit graph

637 commits

Author SHA1 Message Date
Nobuyoshi Nakada
644424941a [Bug #20457] [Prism] Remove redundant return flag 2024-07-17 14:06:11 -04:00
Kevin Newton
7993b88eee [PRISM] Use StringValuePtr for fgets for Prism stream parsing 2024-07-17 13:58:58 -04:00
Kevin Newton
b0a99d0da9 [PRISM] Properly compile branch conditions in their own sequence 2024-07-16 14:40:20 -04:00
Kevin Newton
90e945a7b7 [PRISM] Fix up ensure+loop+break 2024-07-16 14:40:20 -04:00
Kevin Newton
2911578ed7
[PRISM] Add missing rescue tracepoint for rescue modifier 2024-07-15 15:03:54 -04:00
Kevin Newton
8080de04be [PRISM] Optimize inner static literal hashes 2024-07-15 14:04:25 -04:00
Kevin Newton
c1e5358448 [PRISM] Optimize pushing large hash literals 2024-07-15 14:04:25 -04:00
Kevin Newton
b38493c572 [PRISM] Chunk sub-arrays of static literals in array literals
Co-authored-by: Adam Hess <HParker@github.com>
2024-07-15 14:04:25 -04:00
Kevin Newton
fb6d54143d [PRISM] Optimizations for compiling large arrays 2024-07-15 14:04:25 -04:00
Kevin Newton
1f6aeadc82 [PRISM] Fix Windows 2015 segfault 2024-07-11 14:25:54 -04:00
Kevin Newton
ac093f5a06 [PRISM] Fix up shareable constant casting 2024-07-11 14:25:54 -04:00
Kevin Newton
c1df15c3e6 [PRISM] Use node ids for error highlight 2024-07-11 14:25:54 -04:00
eileencodes
d25b74b32c Resize arrays in rb_ary_freeze and use it for freezing arrays
While working on a separate issue we found that in some cases
`ary_heap_realloc` was being called on frozen arrays. To fix this, this
change does the following:

1) Updates `rb_ary_freeze` to assert the type is an array, return if
already frozen, and shrink the capacity if it is not embedded, shared
or a shared root.
2) Replaces `rb_obj_freeze` with `rb_ary_freeze` when the object is
always an array.
3) In `ary_heap_realloc`, ensure the new capa is set with
`ARY_SET_CAPA`. Previously the change in capa was not set.
4) Adds an assertion to `ary_heap_realloc` that the array is not frozen.

Some of this work was originally done in
https://github.com/ruby/ruby/pull/2640, referencing this issue
https://bugs.ruby-lang.org/issues/16291. There didn't appear to be any
objections to this PR, it appears to have simply lost traction.

The original PR made changes to arrays and strings at the same time,
this PR only does arrays. Also it was old enough that rather than revive
that branch I've made a new one. I added Lourens as co-author in addtion
to Aaron who helped me with this patch.

The original PR made this change for performance reasons, and while
that's still true for this PR, the goal of this PR is to avoid
calling `ary_heap_realloc` on frozen arrays. The capacity should be
shrunk _before_ the array is frozen, not after.

Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org>
Co-Authored-By: methodmissing <lourens@methodmissing.com>
2024-07-02 10:34:23 -07:00
Kevin Newton
9f420e2ba5 [PRISM] Modules should also emit the CLASS event 2024-06-25 09:33:39 -04:00
Jeremy Evans
ae0c7faa79
Handle hash and splat nodes in defined?
This supports the nodes in both in the parse.y and prism compilers.

Fixes [Bug #20043]

Co-authored-by: Kevin Newton <kddnewton@gmail.com>
2024-06-24 11:32:58 -07:00
Aaron Patterson
cc97a27008 Add two new instructions for forwarding calls
This commit adds `sendforward` and `invokesuperforward` for forwarding
parameters to calls

Co-authored-by: Matt Valentine-House <matt@eightbitraptor.com>
2024-06-18 09:28:25 -07:00
Aaron Patterson
cdf33ed5f3 Optimized forwarding callers and callees
This patch optimizes forwarding callers and callees. It only optimizes methods that only take `...` as their parameter, and then pass `...` to other calls.

Calls it optimizes look like this:

```ruby
def bar(a) = a
def foo(...) = bar(...) # optimized
foo(123)
```

```ruby
def bar(a) = a
def foo(...) = bar(1, 2, ...) # optimized
foo(123)
```

```ruby
def bar(*a) = a

def foo(...)
  list = [1, 2]
  bar(*list, ...) # optimized
end
foo(123)
```

All variants of the above but using `super` are also optimized, including a bare super like this:

```ruby
def foo(...)
  super
end
```

This patch eliminates intermediate allocations made when calling methods that accept `...`.
We can observe allocation elimination like this:

```ruby
def m
  x = GC.stat(:total_allocated_objects)
  yield
  GC.stat(:total_allocated_objects) - x
end

def bar(a) = a
def foo(...) = bar(...)

def test
  m { foo(123) }
end

test
p test # allocates 1 object on master, but 0 objects with this patch
```

```ruby
def bar(a, b:) = a + b
def foo(...) = bar(...)

def test
  m { foo(1, b: 2) }
end

test
p test # allocates 2 objects on master, but 0 objects with this patch
```

How does it work?
-----------------

This patch works by using a dynamic stack size when passing forwarded parameters to callees.
The caller's info object (known as the "CI") contains the stack size of the
parameters, so we pass the CI object itself as a parameter to the callee.
When forwarding parameters, the forwarding ISeq uses the caller's CI to determine how much stack to copy, then copies the caller's stack before calling the callee.
The CI at the forwarded call site is adjusted using information from the caller's CI.

I think this description is kind of confusing, so let's walk through an example with code.

```ruby
def delegatee(a, b) = a + b

def delegator(...)
  delegatee(...)  # CI2 (FORWARDING)
end

def caller
  delegator(1, 2) # CI1 (argc: 2)
end
```

Before we call the delegator method, the stack looks like this:

```
Executing Line | Code                                  | Stack
---------------+---------------------------------------+--------
              1| def delegatee(a, b) = a + b           | self
              2|                                       | 1
              3| def delegator(...)                    | 2
              4|   #                                   |
              5|   delegatee(...)  # CI2 (FORWARDING)  |
              6| end                                   |
              7|                                       |
              8| def caller                            |
          ->  9|   delegator(1, 2) # CI1 (argc: 2)     |
             10| end                                   |
```

The ISeq for `delegator` is tagged as "forwardable", so when `caller` calls in
to `delegator`, it writes `CI1` on to the stack as a local variable for the
`delegator` method.  The `delegator` method has a special local called `...`
that holds the caller's CI object.

Here is the ISeq disasm fo `delegator`:

```
== disasm: #<ISeq:delegator@-e:1 (1,0)-(1,39)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] "..."@0
0000 putself                                                          (   1)[LiCa]
0001 getlocal_WC_0                          "..."@0
0003 send                                   <calldata!mid:delegatee, argc:0, FCALL|FORWARDING>, nil
0006 leave                                  [Re]
```

The local called `...` will contain the caller's CI: CI1.

Here is the stack when we enter `delegator`:

```
Executing Line | Code                                  | Stack
---------------+---------------------------------------+--------
              1| def delegatee(a, b) = a + b           | self
              2|                                       | 1
              3| def delegator(...)                    | 2
           -> 4|   #                                   | CI1 (argc: 2)
              5|   delegatee(...)  # CI2 (FORWARDING)  | cref_or_me
              6| end                                   | specval
              7|                                       | type
              8| def caller                            |
              9|   delegator(1, 2) # CI1 (argc: 2)     |
             10| end                                   |
```

The CI at `delegatee` on line 5 is tagged as "FORWARDING", so it knows to
memcopy the caller's stack before calling `delegatee`.  In this case, it will
memcopy self, 1, and 2 to the stack before calling `delegatee`.  It knows how much
memory to copy from the caller because `CI1` contains stack size information
(argc: 2).

Before executing the `send` instruction, we push `...` on the stack.  The
`send` instruction pops `...`, and because it is tagged with `FORWARDING`, it
knows to memcopy (using the information in the CI it just popped):

```
== disasm: #<ISeq:delegator@-e:1 (1,0)-(1,39)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] "..."@0
0000 putself                                                          (   1)[LiCa]
0001 getlocal_WC_0                          "..."@0
0003 send                                   <calldata!mid:delegatee, argc:0, FCALL|FORWARDING>, nil
0006 leave                                  [Re]
```

Instruction 001 puts the caller's CI on the stack.  `send` is tagged with
FORWARDING, so it reads the CI and _copies_ the callers stack to this stack:

```
Executing Line | Code                                  | Stack
---------------+---------------------------------------+--------
              1| def delegatee(a, b) = a + b           | self
              2|                                       | 1
              3| def delegator(...)                    | 2
              4|   #                                   | CI1 (argc: 2)
           -> 5|   delegatee(...)  # CI2 (FORWARDING)  | cref_or_me
              6| end                                   | specval
              7|                                       | type
              8| def caller                            | self
              9|   delegator(1, 2) # CI1 (argc: 2)     | 1
             10| end                                   | 2
```

The "FORWARDING" call site combines information from CI1 with CI2 in order
to support passing other values in addition to the `...` value, as well as
perfectly forward splat args, kwargs, etc.

Since we're able to copy the stack from `caller` in to `delegator`'s stack, we
can avoid allocating objects.

I want to do this to eliminate object allocations for delegate methods.
My long term goal is to implement `Class#new` in Ruby and it uses `...`.

I was able to implement `Class#new` in Ruby
[here](https://github.com/ruby/ruby/pull/9289).
If we adopt the technique in this patch, then we can optimize allocating
objects that take keyword parameters for `initialize`.

For example, this code will allocate 2 objects: one for `SomeObject`, and one
for the kwargs:

```ruby
SomeObject.new(foo: 1)
```

If we combine this technique, plus implement `Class#new` in Ruby, then we can
reduce allocations for this common operation.

Co-Authored-By: John Hawthorn <john@hawthorn.email>
Co-Authored-By: Alan Wu <XrXr@users.noreply.github.com>
2024-06-18 09:28:25 -07:00
Aaron Patterson
8f7d08137e remove unused variable 2024-06-06 15:31:21 -07:00
Aaron Patterson
f789b81652 remove debug output 2024-06-06 14:37:59 -07:00
Nobuyoshi Nakada
a720a1c447
Suppress -Wmaybe-uninitialized warnings with LTO 2024-06-01 16:22:31 +09:00
Kevin Newton
4e36abbab3 [PRISM] Support for compiling builtins 2024-05-30 15:38:02 -04:00
Kevin Newton
bc3199bb29 [PRISM] Enable TestAssignmentGen#test_assignment 2024-05-28 15:30:32 -04:00
Kevin Newton
9efb8825a3 [PRISM] Update BEGIN node line number based on empty statements and rescue 2024-05-28 14:24:18 -04:00
Kevin Newton
22536148e2 [PRISM] Use PUSH_SYNTHETIC_PUTNIL for all optional statement bodies 2024-05-28 14:24:18 -04:00
Jean Boussier
9e9f1d9301 Precompute embedded string literals hash code
With embedded strings we often have some space left in the slot, which
we can use to store the string Hash code.

It's probably only worth it for string literals, as they are the ones
likely to be used as hash keys.

We chose to store the Hash code right after the string terminator as to
make it easy/fast to compute, and not require one more union in RString.

```
compare-ruby: ruby 3.4.0dev (2024-04-22T06:32:21Z main f77618c1fa) [arm64-darwin23]
built-ruby: ruby 3.4.0dev (2024-04-22T10:13:03Z interned-string-ha.. 8a1a32331b) [arm64-darwin23]
last_commit=Precompute embedded string literals hash code

|            |compare-ruby|built-ruby|
|:-----------|-----------:|---------:|
|symbol      |     39.275M|   39.753M|
|            |           -|     1.01x|
|dyn_symbol  |     37.348M|   37.704M|
|            |           -|     1.01x|
|small_lit   |     29.514M|   33.948M|
|            |           -|     1.15x|
|frozen_lit  |     27.180M|   33.056M|
|            |           -|     1.22x|
|iseq_lit    |     27.391M|   32.242M|
|            |           -|     1.18x|
```

Co-Authored-By: Étienne Barrié <etienne.barrie@gmail.com>
2024-05-28 07:32:41 +02:00
Kevin Newton
47b723f890
[PRISM] Use only bundled error formatting 2024-05-24 13:05:35 -04:00
Kevin Newton
ba336027be [PRISM] Move error formatting into Ruby 2024-05-24 12:58:44 -04:00
Kevin Newton
56a51fcd33 [PRISM] Fix up some masgn topn calculations 2024-05-23 13:22:22 -04:00
Kevin Newton
6d81ae3f01 [PRISM] Properly support 'it' 2024-05-22 16:34:04 -04:00
Kevin Newton
7a8f797cd4 [PRISM] Use new rational layout 2024-05-21 14:27:46 -04:00
Kevin Newton
42930d28a4 [PRISM] Handle safe navigation in call target nodes 2024-05-21 14:27:37 -04:00
Kevin Newton
a708b6aa65 [PRISM] Respect eval coverage setting 2024-05-20 12:28:47 -04:00
Kevin Newton
ca5b458044 [PRISM] Match CRuby line semantics for evstr 2024-05-20 11:28:53 -04:00
Nobuyoshi Nakada
8c0b57d3ee
rb_enc_compile_warn and rb_enc_compile_warning are printf format 2024-05-19 22:15:59 +09:00
Nobuyoshi Nakada
b47533f67b
Remove rb_bug after COMPILE_ERROR
Fix test failures since 7afc16aa48.
Why crash after reported compile error properly.
2024-05-19 20:45:47 +09:00
Kevin Newton
cce7c25a42 [PRISM] Enable TestRequire 2024-05-17 16:42:14 -04:00
Kevin Newton
c60cdbdc98 [PRISM] Emit END event for modules 2024-05-17 11:23:23 -04:00
Kevin Newton
4ba0579da6 [PRISM] Enable TestSyntax#test_error_message_encoding 2024-05-17 10:33:42 -04:00
Kevin Newton
77b6c980b2 [PRISM] Handle operator->binary_operator rename 2024-05-10 11:47:48 -04:00
Kevin Newton
ba062a6231 [PRISM] Use correct warning encoding 2024-05-08 08:12:17 -04:00
Kevin Newton
481e16d58b [PRISM] Support for compiling ractor constant path writes 2024-05-06 19:06:13 -04:00
Kevin Newton
eeba158109 [PRISM] Support for compiling ractor constant writes 2024-05-06 19:06:13 -04:00
Kevin Newton
0948b6a592 [PRISM] Use new constant path structure 2024-05-03 11:11:57 -04:00
Kevin Newton
5409661fe6 Mark the first string element of a regexp as binary if US-ASCII 2024-05-02 22:46:09 -04:00
Kevin Newton
e34c131ce8 [PRISM] Disallow redundant returns from being line events 2024-05-02 15:16:15 -04:00
Kevin Newton
398453c3c0 [PRISM] Fix param names for repeated splats 2024-05-02 11:27:05 -04:00
jinroq
a3726c028d Fixed missing support for d75bbba255. 2024-05-02 05:36:14 -04:00
Kevin Newton
0fa09c5729 [PRISM] Correct encoding for interpolated string literals in regexp 2024-05-01 12:34:29 -04:00
Kevin Newton
b6fa18fbe9 [PRISM] Properly precheck regexp for encoding issues 2024-05-01 12:34:29 -04:00
Kevin Newton
1b8650964b [PRISM] Support interpolated regexp with encoding modifiers 2024-05-01 12:34:29 -04:00