summaryrefslogtreecommitdiff
path: root/prism_compile.c
AgeCommit message (Collapse)Author
2024-07-23[PRISM] Fix up ensure compilation, match compile.cKevin Newton
Notes: Merged: https://github.com/ruby/ruby/pull/11228
2024-07-22[PRISM] Use xcalloc for constants instead of callocPeter Zhu
Notes: Merged: https://github.com/ruby/ruby/pull/11222
2024-07-22[PRISM] Fix memory leak in constantsPeter Zhu
For example, the following code leaks: code = 1000.times.map { |i| "var#{i} = 1" }.join("\n") 10.times do 1000.times do RubyVM::InstructionSequence.compile_prism(code) end puts `ps -o rss= -p #{$$}` end Before: 70384 88032 103856 115712 125584 132768 144784 152624 165296 180608 After: 62368 78784 74512 87712 85072 77728 69424 74992 71264 81440 Notes: Merged: https://github.com/ruby/ruby/pull/11222
2024-07-19[PRISM] Fix compiler warning for min_tmp_array_sizePeter Zhu
prism_compile.c:5770:40: warning: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare] 5770 | if (tmp_array_size >= min_tmp_array_size) { | ^~
2024-07-19[PRISM] Fix compiler warning for min_tmp_hash_lengthPeter Zhu
prism_compile.c:1406:27: warning: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare] 1406 | if (count >= min_tmp_hash_length) { | ^~
2024-07-18Fix interpolated sybmol node instructionseileencodes
If the symbol node is interpolated like this `:"#{foo}"` the instruction sequence should be `putstring` followed by `intern`. In this case it was a `putobject` causing the `test_yjit` tests to fail. Note that yjit is not required to reproduce - the instructions are `putstring` and `intern` for yjit and non-yjit with the original parser. To fix I moved `pm_interpolated_node_compile` out of the else, and entirely removed the conditional. `pm_interpolated_node_compile` knows how / when to use `putstring` over `putobject` already. The `intern` is then added by removing the conditional. Before: ``` == disasm: #<ISeq:<main>@test2.rb:1 (1,0)-(1,11)> 0000 putobject :foo ( 1)[Li] 0002 leave ``` After: ``` == disasm: #<ISeq:<main>@test2.rb:1 (1,0)-(1,11)> 0000 putstring "foo" ( 1)[Li] 0002 intern 0003 leave ``` Fixes the test `TestYJIT#test_compile_dynamic_symbol`. Related to ruby/prism#2935 Notes: Merged: https://github.com/ruby/ruby/pull/11191
2024-07-18[PRISM] Use KW_SPLAT_MUT when possible for method callsKevin Newton
Notes: Merged: https://github.com/ruby/ruby/pull/11200
2024-07-18[PRISM] Use concattoarray instead of splatarray+concatarrayKevin Newton
Notes: Merged: https://github.com/ruby/ruby/pull/11200
2024-07-18Fix empty hash instructioneileencodes
When we have an empty hash the iseq should have a `newhash` but instead had a `duphash`. To fix, check if the node's elements are equal to `0`. If so we want a `newhash`, otherwise use the original `duphash` instructions. Before: ``` == disasm: #<ISeq:<main>@test2.rb:1 (1,0)-(1,2)> 0000 duphash {} ( 1)[Li] 0002 leave ``` After: ``` == disasm: #<ISeq:<main>@test2.rb:1 (1,0)-(1,2)> 0000 newhash 0 ( 1)[Li] 0002 leave ``` Fixes the test `TestYJIT#test_compile_newhash`. Related to ruby/prism#2935 Notes: Merged: https://github.com/ruby/ruby/pull/11190
2024-07-17[PRISM] Use RSTRING_PTR for Ruby parsing with fgetsKevin Newton
2024-07-17[PRISM] Use RSTRING_LEN for Prism stream parsingKevin Newton
2024-07-17[Bug #20457] [Prism] Remove redundant return flagNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/11163
2024-07-17[PRISM] Use StringValuePtr for fgets for Prism stream parsingKevin Newton
Notes: Merged: https://github.com/ruby/ruby/pull/11188
2024-07-16[PRISM] Properly compile branch conditions in their own sequenceKevin Newton
Notes: Merged: https://github.com/ruby/ruby/pull/11177
2024-07-16[PRISM] Fix up ensure+loop+breakKevin Newton
Notes: Merged: https://github.com/ruby/ruby/pull/11177
2024-07-15[PRISM] Add missing rescue tracepoint for rescue modifierKevin Newton
2024-07-15[PRISM] Optimize inner static literal hashesKevin Newton
2024-07-15[PRISM] Optimize pushing large hash literalsKevin Newton
2024-07-15[PRISM] Chunk sub-arrays of static literals in array literalsKevin Newton
Co-authored-by: Adam Hess <[email protected]>
2024-07-15[PRISM] Optimizations for compiling large arraysKevin Newton
2024-07-11[PRISM] Fix Windows 2015 segfaultKevin Newton
2024-07-11[PRISM] Fix up shareable constant castingKevin Newton
2024-07-11[PRISM] Use node ids for error highlightKevin Newton
2024-07-02Resize arrays in `rb_ary_freeze` and use it for freezing arrayseileencodes
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 <[email protected]> Co-Authored-By: methodmissing <[email protected]>
2024-06-25[PRISM] Modules should also emit the CLASS eventKevin Newton
2024-06-24Handle hash and splat nodes in defined?Jeremy Evans
This supports the nodes in both in the parse.y and prism compilers. Fixes [Bug #20043] Co-authored-by: Kevin Newton <[email protected]>
2024-06-18Add two new instructions for forwarding callsAaron Patterson
This commit adds `sendforward` and `invokesuperforward` for forwarding parameters to calls Co-authored-by: Matt Valentine-House <[email protected]>
2024-06-18Optimized forwarding callers and calleesAaron Patterson
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 <[email protected]> Co-Authored-By: Alan Wu <[email protected]>
2024-06-06remove unused variableAaron Patterson
2024-06-06remove debug outputAaron Patterson
2024-06-01Suppress -Wmaybe-uninitialized warnings with LTONobuyoshi Nakada
2024-05-30[PRISM] Support for compiling builtinsKevin Newton
2024-05-28[PRISM] Enable TestAssignmentGen#test_assignmentKevin Newton
2024-05-28[PRISM] Update BEGIN node line number based on empty statements and rescueKevin Newton
2024-05-28[PRISM] Use PUSH_SYNTHETIC_PUTNIL for all optional statement bodiesKevin Newton
2024-05-28Precompute embedded string literals hash codeJean Boussier
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é <[email protected]>
2024-05-24[PRISM] Use only bundled error formattingKevin Newton
2024-05-24[PRISM] Move error formatting into RubyKevin Newton
2024-05-23[PRISM] Fix up some masgn topn calculationsKevin Newton
2024-05-22[PRISM] Properly support 'it'Kevin Newton
2024-05-21[PRISM] Use new rational layoutKevin Newton
2024-05-21[PRISM] Handle safe navigation in call target nodesKevin Newton
2024-05-20[PRISM] Respect eval coverage settingKevin Newton
2024-05-20[PRISM] Match CRuby line semantics for evstrKevin Newton
2024-05-19`rb_enc_compile_warn` and `rb_enc_compile_warning` are printf formatNobuyoshi Nakada
2024-05-19Remove `rb_bug` after COMPILE_ERRORNobuyoshi Nakada
Fix test failures since 7afc16aa48beb093b06eb978bc430f90dd771690. Why crash after reported compile error properly.
2024-05-17[PRISM] Enable TestRequireKevin Newton
2024-05-17[PRISM] Emit END event for modulesKevin Newton
2024-05-17[PRISM] Enable TestSyntax#test_error_message_encodingKevin Newton
2024-05-10[PRISM] Handle operator->binary_operator renameKevin Newton