aboutsummaryrefslogtreecommitdiffstats
path: root/vm_core.h
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2023-11-30 14:58:42 -0800
committerJeremy Evans <code@jeremyevans.net>2024-01-24 18:25:55 -0800
commit0f90a24a816bec438edb272fb83f334484dfc285 (patch)
treec753fc56c52aa777488c69da7f1b21175d331832 /vm_core.h
parentb8516d6d0174a10579817f4bcf5a94c8ef03dd7a (diff)
downloadruby-0f90a24a816bec438edb272fb83f334484dfc285.tar.gz
Introduce Allocationless Anonymous Splat Forwarding
Ruby makes it easy to delegate all arguments from one method to another: ```ruby def f(*args, **kw) g(*args, **kw) end ``` Unfortunately, this indirection decreases performance. One reason it decreases performance is that this allocates an array and a hash per call to `f`, even if `args` and `kw` are not modified. Due to Ruby's ability to modify almost anything at runtime, it's difficult to avoid the array allocation in the general case. For example, it's not safe to avoid the allocation in a case like this: ```ruby def f(*args, **kw) foo(bar) g(*args, **kw) end ``` Because `foo` may be `eval` and `bar` may be a string referencing `args` or `kw`. To fix this correctly, you need to perform something similar to escape analysis on the variables. However, there is a case where you can avoid the allocation without doing escape analysis, and that is when the splat variables are anonymous: ```ruby def f(*, **) g(*, **) end ``` When splat variables are anonymous, it is not possible to reference them directly, it is only possible to use them as splats to other methods. Since that is the case, if `f` is called with a regular splat and a keyword splat, it can pass the arguments directly to `g` without copying them, avoiding allocation. For example: ```ruby def g(a, b:) a + b end def f(*, **) g(*, **) end a = [1] kw = {b: 2} f(*a, **kw) ``` I call this technique: Allocationless Anonymous Splat Forwarding. This is implemented using a couple additional iseq param flags, anon_rest and anon_kwrest. If anon_rest is set, and an array splat is passed when calling the method when the array splat can be used without modification, `setup_parameters_complex` does not duplicate it. Similarly, if anon_kwest is set, and a keyword splat is passed when calling the method, `setup_parameters_complex` does not duplicate it.
Diffstat (limited to 'vm_core.h')
-rw-r--r--vm_core.h2
1 files changed, 2 insertions, 0 deletions
diff --git a/vm_core.h b/vm_core.h
index 6532c6b038..c1508736bd 100644
--- a/vm_core.h
+++ b/vm_core.h
@@ -416,6 +416,8 @@ struct rb_iseq_constant_body {
unsigned int ambiguous_param0 : 1; /* {|a|} */
unsigned int accepts_no_kwarg : 1;
unsigned int ruby2_keywords: 1;
+ unsigned int anon_rest: 1;
+ unsigned int anon_kwrest: 1;
} flags;
unsigned int size;