summaryrefslogtreecommitdiff
path: root/proc.c
AgeCommit message (Collapse)Author
17 hours* adjust indentNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/13634
11 daysproc.c: saves Binding#clone on copying ivars twiceJean Boussier
Notes: Merged: https://github.com/ruby/ruby/pull/13541
2025-05-21Fix one-by-one error of numbered parameter IDRichard Böhme
Notes: Merged: https://github.com/ruby/ruby/pull/13395
2025-05-21Shrink `sym_proc_cache` by halfJean Boussier
There is no need to store the symbol and the proc given the proc has a reference to the symbol. This makes the cache half as small, now fitting in an object slot, but also make it easier to allow that cache to be used by ractors, assuming we'd make `Symbol#to_proc` return a shareable proc. Notes: Merged: https://github.com/ruby/ruby/pull/13390
2025-05-21Fix Symbol#to_proc (rb_sym_to_proc) to be ractor safeLuke Gruber
In non-main ractors, don't use `sym_proc_cache`. It is not thread-safe to add to this array without a lock and also it leaks procs from one ractor to another. Instead, we create a new proc each time. If this results in poor performance we can come up with a solution later. Fixes [Bug #21354] Notes: Merged: https://github.com/ruby/ruby/pull/13380
2025-05-20[DOC] Describe new return value of source_locationDaisuke Aritomo
Proc#source_location, Method#source_location and UnboundMethod#source_location carry more information since 073c4e1cc712064e626914fa4a5a8061f903a637. https://bugs.ruby-lang.org/issues/6012 https://github.com/ruby/ruby/pull/12539 Notes: Merged: https://github.com/ruby/ruby/pull/13370
2025-05-11Prevent a compile error of clang's -Wformat-pedanticYusuke Endoh
``` /github/workspace/src/proc.c:2023:65: error: format specifies type 'void *' but the argument has type 'const rb_namespace_t *' (aka 'const struct rb_namespace_struct *') [-Werror,-Wformat-pedantic] 2023 | rb_bug("Unexpected namespace on the method definition: %p", ns); | ~~ ^~ ```
2025-05-11namespace on readSatoshi Tagomori
2025-04-03Remove no longer used unionNobuyoshi Nakada
IMEMO_NEW takes just one memo value, min/max argc are assigned directly and packed argc is not used.
2025-03-16Fix enums in comparisonsNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/12941
2025-02-20Revert "refactor: make get_local_variable_ptr accept "rb_env_t *""Yusuke Endoh
This reverts commit 6d75599a1aade9f8081d0691a9da1e62a5428e95. Updating env was actually needed in local_variable_set. Alan Wu pointed this out to me. Notes: Merged: https://github.com/ruby/ruby/pull/12783
2025-02-18reject numbered parameters from Binding#local_variablesYusuke Endoh
Also, Binding#local_variable_get and #local_variable_set rejects an access to numbered parameters. [Bug #20965] [Bug #21049] Notes: Merged: https://github.com/ruby/ruby/pull/12746
2025-02-18refactor: make get_local_variable_ptr accept "rb_env_t *"Yusuke Endoh
... instead of "rb_env_t **" because no one uses the updated env. Notes: Merged: https://github.com/ruby/ruby/pull/12746
2025-02-01[Bug #21103] Fix local variable index calculation with forwardingNobuyoshi Nakada
Forwarding argument is optimized not to packed when no other arguments and an internal object refers values before it. This size is decided at called time, calculate the local variable index from the fixed end point. Notes: Merged: https://github.com/ruby/ruby/pull/12686
2025-01-20Remove dead function rb_func_proc_newPeter Zhu
Notes: Merged: https://github.com/ruby/ruby/pull/12566
2025-01-13Proc#parameters: Show anonymous optionals as `[:opt]`Alan Wu
Have this for lead parameters as well as parameters after rest ("post"). [Bug #20974] Notes: Merged: https://github.com/ruby/ruby/pull/12547
2025-01-09[Feature #6012] Extend `source_location` for end position and columnsNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/12539
2025-01-05Use a single quote instead of a backtick for error messagesJunichi Ito
Fix https://bugs.ruby-lang.org/issues/20977 Notes: Merged: https://github.com/ruby/ruby/pull/12424
2025-01-02[DOC] Exclude 'Method' from RDoc's autolinkingNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/12496
2024-12-18Document 'it' and update numbered parameters docs (#12375)Victor Shepelev
Notes: Merged-By: zverok <[email protected]>
2024-12-13Fix use-after-free in ep in Proc#dup for ifunc procsPeter Zhu
[Bug #20950] ifunc proc has the ep allocated in the cfunc_proc_t which is the data of the TypedData object. If an ifunc proc is duplicated, the ep points to the ep of the source object. If the source object is freed, then the ep of the duplicated object now points to a freed memory region. If we try to use the ep we could crash. For example, the following script crashes: p = { a: 1 }.to_proc 100.times do p = p.dup GC.start p.call rescue ArgumentError end This commit changes ifunc proc to also duplicate the ep when it is duplicated. Notes: Merged: https://github.com/ruby/ruby/pull/12319
2024-11-01[Bug #20853] Fix Proc#hash to not change after compactionPeter Zhu
The hash value of a Proc must remain constant after a compaction, otherwise it may not work as the key in a hash table. Notes: Merged: https://github.com/ruby/ruby/pull/11966
2024-10-03Make Object#singleton_method return methods in modules included in or ↵Jeremy Evans
prepended to singleton class To simplify the implementation, this makes Object#singleton_method call the same method called by Object#method (rb_obj_method), then check that the returned Method is defined before the superclass of the object's singleton class. To keep the same error messages, it rescues exceptions raised by rb_obj_method, and then raises its own exception. Fixes [Bug #20620] Notes: Merged: https://github.com/ruby/ruby/pull/11605
2024-09-27s/erorr/error/ydah
Notes: Merged: https://github.com/ruby/ruby/pull/11697
2024-07-10Add rb_block_call2, a flexible variant of rb_block_callYusuke Endoh
This function accepts flags: RB_NO_KEYWORDS, RB_PASS_KEYWORDS, RB_PASS_CALLED_KEYWORDS: Works as the same as rb_block_call_kw. RB_BLOCK_NO_USE_PACKED_ARGS: The given block ("bl_proc") does not use "yielded_arg" of rb_block_call_func_t. Instead, the block accesses the yielded arguments via "argc" and "argv". This flag allows the called method to yield arguments without allocating an Array.
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-04-26[Universal parser] Decouple IMEMO from rb_ast_tHASUMI Hitoshi
This patch removes the `VALUE flags` member from the `rb_ast_t` structure making `rb_ast_t` no longer an IMEMO object. ## Background We are trying to make the Ruby parser generated from parse.y a universal parser that can be used by other implementations such as mruby. To achieve this, it is necessary to exclude VALUE and IMEMO from parse.y, AST, and NODE. ## Summary (file by file) - `rubyparser.h` - Remove the `VALUE flags` member from `rb_ast_t` - `ruby_parser.c` and `internal/ruby_parser.h` - Use TypedData_Make_Struct VALUE which wraps `rb_ast_t` `in ast_alloc()` so that GC can manage it - You can retrieve `rb_ast_t` from the VALUE by `rb_ruby_ast_data_get()` - Change the return type of `rb_parser_compile_XXXX()` functions from `rb_ast_t *` to `VALUE` - rb_ruby_ast_new() which internally `calls ast_alloc()` is to create VALUE vast outside ruby_parser.c - `iseq.c` and `vm_core.h` - Amend the first parameter of `rb_iseq_new_XXXX()` functions from `rb_ast_body_t *` to `VALUE` - This keeps the VALUE of AST on the machine stack to prevent being removed by GC - `ast.c` - Almost all change is replacement `rb_ast_t *ast` with `VALUE vast` (sorry for the big diff) - Fix `node_memsize()` - Now it includes `rb_ast_local_table_link`, `tokens` and script_lines - `compile.c`, `load.c`, `node.c`, `parse.y`, `proc.c`, `ruby.c`, `template/prelude.c.tmpl`, `vm.c` and `vm_eval.c` - Follow-up due to the above changes - `imemo.{c|h}` - If an object with `imemo_ast` appears, considers it a bug Co-authored-by: Nobuyoshi Nakada <[email protected]>
2024-04-05[Bug #20342] Consider wrapped load in `main` methodsNobuyoshi Nakada
2024-03-06Refactor VM root modulesJean Boussier
This `st_table` is used to both mark and pin classes defined from the C API. But `vm->mark_object_ary` already does both much more efficiently. Currently a Ruby process starts with 252 rooted classes, which uses `7224B` in an `st_table` or `2016B` in an `RArray`. So a baseline of 5kB saved, but since `mark_object_ary` is preallocated with `1024` slots but only use `405` of them, it's a net `7kB` save. `vm->mark_object_ary` is also being refactored. Prior to this changes, `mark_object_ary` was a regular `RArray`, but since this allows for references to be moved, it was marked a second time from `rb_vm_mark()` to pin these objects. This has the detrimental effect of marking these references on every minors even though it's a mostly append only list. But using a custom TypedData we can save from having to mark all the references on minor GC runs. Addtionally, immediate values are now ignored and not appended to `vm->mark_object_ary` as it's just wasted space.
2024-03-06Move FL_SINGLETON to FL_USER1Jean Boussier
This frees FL_USER0 on both T_MODULE and T_CLASS. Note: prior to this, FL_SINGLETON was never set on T_MODULE, so checking for `FL_SINGLETON` without first checking that `FL_TYPE` was `T_CLASS` was valid. That's no longer the case.
2024-02-21Add IMEMO_NEWPeter Zhu
Rather than exposing that an imemo has a flag and four fields, this changes the implementation to only expose one field (the klass) and fills the rest with 0. The type will have to fill in the values themselves.
2024-02-15Do not include a backtick in error messages and backtracesYusuke Endoh
[Feature #16495]
2024-02-12proc.c: get rid of `CLONESETUP`Jean Boussier
[Bug #20253] All the way down to Ruby 1.9, `Proc`, `Method`, `UnboundMethod` and `Binding` always had their own specific clone and dup routine. This caused various discrepancies with how other objects behave on `dup` and `clone. [Bug #20250], [Bug #20253]. This commit get rid of `CLONESETUP` and use the the same codepath as all other types, so ensure consistency. NB: It's still not accepting the `freeze` keyword argument on `clone`. Co-Authored-By: Étienne Barrié <[email protected]>
2024-01-30Use `UNDEF_P`Nobuyoshi Nakada
2023-12-12Remove unused macrosAlan Wu
2023-12-05Make env_clone compaction safePeter Zhu
The original order of events is: 1. Allocate new_body. 2. Peform memcpy into new_body. 3. Create new_env using new_body. However, if GC compaction runs during step 3, then new_env would not have yet been created and objects on new_body could move but it would not be reference updated. This commit changes the order of the last two events.
2023-11-21Get rid of useless dsize functionsJean Boussier
If we always return 0, we might as well not define the function at all.
2023-11-20proc.c: Make Method and UnboundMethod embdedJean Boussier
Avoid some needless malloc churn ``` compare-ruby: ruby 3.3.0dev (2023-11-20T02:02:55Z master 701b0650de) [arm64-darwin22] last_commit=[ruby/prism] feat: add encoding for IBM865 (https://github.com/ruby/prism/pull/1884) built-ruby: ruby 3.3.0dev (2023-11-20T16:23:07Z embedded-methods e35284bfaa) [arm64-darwin22] warming up.. | |compare-ruby|built-ruby| |:------------------------|-----------:|---------:| |allocate_method | 8.413M| 12.333M| | | -| 1.47x| |allocate_unbound_method | 8.083M| 11.607M| | | -| 1.44x| ``` ``` prelude: | class SomeClass def foo end end some_object = SomeClass.new benchmark: allocate_method: some_object.method(:foo) allocate_unbound_method: SomeClass.instance_method(:foo) ```
2023-10-26[Feature #19362] Call `#initialize_dup` hook at `Proc#dup`Nobuyoshi Nakada
2023-09-12Make Kernel#lambda raise when given non-literal blockAlan Wu
Previously, Kernel#lambda returned a non-lambda proc when given a non-literal block and issued a warning under the `:deprecated` category. With this change, Kernel#lambda will always return a lambda proc, if it returns without raising. Due to interactions with block passing optimizations, we previously had two separate code paths for detecting whether Kernel#lambda got a literal block. This change allows us to remove one path, the hack done with rb_control_frame_t::block_code introduced in 85a337f for supporting situations where Kernel#lambda returned a non-lambda proc. [Feature #19777] Co-authored-by: Takashi Kokubun <[email protected]> Notes: Merged: https://github.com/ruby/ruby/pull/8405
2023-07-26Make {Nil,True,False}Class#singleton_method always raise NameErrorJeremy Evans
{Nil,True,False}Class#singleton_methods always returns [] indicating that there are no singleton methods defined, so #singleton_method should be consistent with that. Fixes [Bug #11064] Notes: Merged: https://github.com/ruby/ruby/pull/7973
2023-07-20proc.c: Remove unused parameter [ci skip]Alan Wu
2023-07-13Store object age in a bitmapMatt Valentine-House
Closes [Feature #19729] Previously 2 bits of the flags on each RVALUE are reserved to store the number of GC cycles that each object has survived. This commit introduces a new bit array on the heap page, called age_bits, to store that information instead. This patch still reserves one of the age bits in the flags (the old FL_PROMOTED0 bit, now renamed FL_PROMOTED). This is set to 0 for young objects and 1 for old objects, and is used as a performance optimisation for the write barrier. Fetching the age_bits from the heap page and doing the required math to calculate if the object was old or not would slow down the write barrier. So we keep this bit synced in the flags for fast access. Notes: Merged: https://github.com/ruby/ruby/pull/7938
2023-03-06Stop exporting symbols for MJITTakashi Kokubun
Notes: Merged: https://github.com/ruby/ruby/pull/7459
2023-02-19[DOC] Improve Kernel#binding docszverok
* Add links to Binding class * Make examples practical * Extend possible usages description Notes: Merged: https://github.com/ruby/ruby/pull/7316
2023-02-15Encapsulate RCLASS_ATTACHED_OBJECTJean Boussier
Right now the attached object is stored as an instance variable and all the call sites that either get or set it have to know how it's stored. It's preferable to hide this implementation detail behind accessors so that it is easier to change how it's stored. Notes: Merged: https://github.com/ruby/ruby/pull/7308
2023-02-10use correct svar even if env is escapedKoichi Sasada
This patch is follo-up of 0a82bfe. Without this patch, if env is escaped (Proc'ed), strange svar can be touched. This patch tracks escaped env and use it. Notes: Merged: https://github.com/ruby/ruby/pull/7282
2023-02-09Merge gc.h and internal/gc.hMatt Valentine-House
[Feature #19425] Notes: Merged: https://github.com/ruby/ruby/pull/7273
2023-02-07Use rb_gc_mark_and_move for method objectsPeter Zhu
Notes: Merged: https://github.com/ruby/ruby/pull/7245
2023-02-07Use rb_gc_mark_and_move for proc and bindingPeter Zhu
Also makes VM_ENV_ENVVAL movable. Notes: Merged: https://github.com/ruby/ruby/pull/7245