742 Commits

Author SHA1 Message Date
Samuel Williams
7e05e9ff94
Fix incorrect "nested_fake_name" documentation. (#9135) 2023-12-06 06:48:55 +00:00
Alan Wu
0346cbbc14 Fix parameter types for rb_ivar_foreach() callbacks
For this public API, the callback is declared to take
`(ID, VALUE, st_data_t)`, but it so happens that using
`(st_data_t, st_data_t, st_data_t)` also
type checks, because the underlying type is identical.
Use it as declared and get rid of some casts.
2023-12-05 18:19:42 -05:00
Peter Zhu
f40727f4aa Make rb_obj_copy_ivs_to_hash_table_i static 2023-12-04 09:58:41 -05:00
Peter Zhu
43dc8e9012 Fix indentation in ivar_set [ci skip] 2023-11-28 08:12:35 -05:00
Peter Zhu
269c705f93 Fix compaction for generic ivars
When generic instance variable has a shape, it is marked movable. If it
it transitions to too complex, it needs to update references otherwise
it may have incorrect references.
2023-11-24 13:29:04 -05:00
Peter Zhu
7f7613c2c7 Fix compacting during evacuation of generic ivars
When evacuating generic instance variables, the instance variables exist
in both the array and the ST table. We need to ensure it has switched
to the ST table before performing any operations that can trigger GC
compaction.
2023-11-23 09:11:24 -05:00
Aaron Patterson
209a0253f5 Use count macros for counting instance variables
We don't need to check for Qundef because the shape tells us the number
if IVs that are stored on the object
2023-11-21 09:46:50 -08:00
Peter Zhu
7e7e2dde24 Fix memory leak when evacuating generic ivars
The lookup in the table is using the wrong key when converting generic
instance variables to too complex, which means that it never looks up
the entry which leaks memory when the entry is overwritten.
2023-11-21 10:27:02 -05:00
Aaron Patterson
6fce8c7980 Don't try compacting ivars on Classes that are "too complex"
Too complex classes use a hash table to store ivs, and should always pin
their IVs.  We shouldn't touch those classes in compaction.
2023-11-20 16:09:48 -08:00
Peter Zhu
f376163194 Fix crash when evacuating generic ivar
When transitioning generic instance variable objects to too complex, we
set the shape first before performing inserting the new gen_ivtbl. The
st_insert for the new gen_ivtbl could allocate and cause a GC. If that
happens, then it will crash because the object will have a too complex
shape but not yet be backed by a st_table.

This commit changes the order so that the insert happens first before
the new shape is set.

The following script reproduces the issue:

```
o = []
o.instance_variable_set(:@a, 1)

i = 0
o = Object.new
while RubyVM::Shape.shapes_available > 0
  o.instance_variable_set(:"@i#{i}", 1)
  i += 1
end

ary = 1_000.times.map { [] }

GC.stress = true
ary.each do |o|
  o.instance_variable_set(:@a, 1)
  o.instance_variable_set(:@b, 1)
end
```
2023-11-20 16:57:24 -05:00
Peter Zhu
9d51ab8b3d Fix indentation [ci skip] 2023-11-20 13:43:31 -05:00
Peter Zhu
83da4a7e62 Fix crash when iterating over generic ivars 2023-11-20 10:13:18 -05:00
Jean Boussier
94c9f16663 Refactor rb_obj_evacuate_ivs_to_hash_table
That function is a bit too low level to called from multiple
places. It's always used in tandem with `rb_shape_set_too_complex`
and both have to know how the object is laid out to update the
`iv_ptr`.

So instead we can provide two higher level function:

  - `rb_obj_copy_ivs_to_hash_table` to prepare a `st_table` from an
    arbitrary oject.
  - `rb_obj_convert_to_too_complex` to assign the new `st_table`
    to the old object, and safely free the old `iv_ptr`.

Unfortunately both can't be combined into one, because `rb_obj_copy_ivar`
need `rb_obj_copy_ivs_to_hash_table` to copy from one object
to another.
2023-11-17 09:19:21 +01:00
Jean Boussier
81b35fe729 rb_evict_ivars_to_hash: get rid of the sahpe paramater
It's only used to allocate the table with the right size,
but in some case we were passing `rb_shape_get_shape_by_id(SHAPE_OBJ_TOO_COMPLEX)`
which `next_iv_index` is a bit undefined.

So overall we're better to just allocate a table the size of the existing
object, it should be close enough in the vast majority of cases,
and that's already a de-optimizaton path anyway.
2023-11-16 17:49:59 +01:00
Peter Zhu
68869e9bd9 Revert "Revert "Remove SHAPE_CAPACITY_CHANGE shapes""
This reverts commit 5f3fb4f4e397735783743fe52a7899b614bece20.
2023-11-13 18:26:36 -05:00
Peter Zhu
7e6609e8f0 [ci skip] Fix indentation in rb_class_ivar_set 2023-11-10 12:43:06 -05:00
Peter Zhu
5f3fb4f4e3 Revert "Remove SHAPE_CAPACITY_CHANGE shapes"
This reverts commit f6910a61122931e4193bcc0fad18d839c319b720.

We're seeing crashes in the test suite of Shopify's core monolith after
this change.
2023-11-10 11:27:49 -05:00
Peter Zhu
f6910a6112 Remove SHAPE_CAPACITY_CHANGE shapes
We don't need to create a shape to transition capacity as we can
transition the capacity when the capacity of the SHAPE_IVAR changes.
2023-11-09 09:25:02 -05:00
Jean Boussier
d898e8d6f8 Refactor rb_shape_transition_shape_capa out
Right now the `rb_shape_get_next` shape caller need to
first check if there is capacity left, and if not call
`rb_shape_transition_shape_capa` before it can call `rb_shape_get_next`.

And on each of these it needs to checks if we got a TOO_COMPLEX
back.

All this logic is duplicated in the interpreter, YJIT and RJIT.

Instead we can have `rb_shape_get_next` do the capacity transition
when needed. The caller can compare the old and new shapes capacity
to know if resizing is needed. It also can check for TOO_COMPLEX
only once.
2023-11-08 11:02:55 +01:00
Peter Zhu
dc911a332b Remove rb_complex_ivar_set 2023-11-06 11:10:41 -05:00
Peter Zhu
e2ef957c23 Use general_ivar_set for generic ivars 2023-11-06 11:10:41 -05:00
Peter Zhu
18f675912e Use general_ivar_set for Class ivars 2023-11-06 11:10:41 -05:00
Peter Zhu
679e98dc27 Use general_ivar_set for Objects 2023-11-06 11:10:41 -05:00
Peter Zhu
c747c67533 Implement general_ivar_set 2023-11-06 11:10:41 -05:00
Jean Boussier
4a6bdbd6dc generic_ivar_set: properly check for TOO_COMPLEX on capacity transition 2023-11-06 12:39:52 +01:00
Peter Zhu
e010bf1674 Fix typo in variable.c 2023-11-03 11:04:25 -04:00
Peter Zhu
81882ca42f Use RB_OBJ_WRITE over RB_OBJ_WRITTEN in variable.c 2023-11-03 11:01:11 -04:00
Peter Zhu
1321df773b Use shape capacity transitions for generic ivars
This commit changes generic ivars to respect the capacity transition in
shapes rather than growing the capacity independently.
2023-11-03 10:15:32 -04:00
Jean Boussier
35da6f864a rb_ivar_defined: handle complex modules
It was assuming only objects can be complex.
2023-11-03 11:52:17 +01:00
Peter Zhu
4c3cc25ea2 Use shape capacity transition for class ivars
This commit changes class ivars to respect the capacity transition in
shapes rather than growing the capacity independently.
2023-11-02 13:42:11 -04:00
Peter Zhu
38ba040d8b Make every initial size pool shape a root shape
This commit makes every initial size pool shape a root shape and assigns
it a capacity of 0.
2023-11-02 13:42:11 -04:00
Peter Zhu
5f130e2111 Fix write barrier in rb_copy_generic_ivar 2023-11-02 09:23:14 -04:00
Peter Zhu
944e0ae698 Remove duplicated code in generic_ivar_set
There is a duplicated check for the object is too complex.
2023-11-02 08:46:54 -04:00
Peter Zhu
bdf8ce807f Fix remove_class_variable for too complex classes 2023-11-01 13:13:51 -04:00
Peter Zhu
e6059d0c84 Refactor rb_obj_remove_instance_variable 2023-11-01 11:37:13 -04:00
Peter Zhu
70e3e08881 Optimize for too complex objects 2023-11-01 11:05:46 -04:00
Jean Boussier
b77148ae9f remove_instance_variable: Handle running out of shapes
`remove_shape_recursive` wasn't considering that if we run out of
shapes, it might have to transition to SHAPE_TOO_COMPLEX.

When this happens, we now return with an error and the caller
initiates the evacuation.
2023-11-01 15:21:55 +01:00
Peter Zhu
9c6dd25093 Fix removing non-existent ivar for too complex 2023-11-01 08:25:09 -04:00
Peter Zhu
c3b7f27561 Fix remove_instance_variable for too complex generic ivar 2023-10-31 17:04:40 -04:00
Peter Zhu
8889992b75 Fix remove_instance_variable for too complex class 2023-10-31 16:38:05 -04:00
Peter Zhu
e2d950733e Add ST table to gen_ivtbl for complex shapes
On 32-bit systems, we must store the shape ID in the gen_ivtbl to not
lose the shape. If we directly store the ST table into the generic
ivar table, then we lose the shape. This makes it impossible to
determine the shape of the object and whether it is too complex or not.
2023-10-31 12:07:54 -04:00
Peter Zhu
1c45124c49 Create table for too complex generic variables 2023-10-31 12:07:54 -04:00
Aaron Patterson
6f5e378057 Fix "too complex" iv sets on generic ivar objects
We weren't taking in to account that objects with generic IV tables
could go "too complex" in the IV set code.  This commit takes that in to
account and also ensures FL_EXIVAR is set when a geniv object
transitions to "too complex"

Co-Authored-By: Jean Boussier <byroot@ruby-lang.org>
2023-10-31 12:07:54 -04:00
Jean Boussier
ac7f913ca3 Handle SHAPE_TOO_COMPLEX in generic_ivar_set 2023-10-31 12:07:54 -04:00
Aaron Patterson
afae8df373 get_next_shape_internal should always return a shape
If it runs out of shapes, or new variations aren't allowed, it will
return "too complex"
2023-10-24 14:23:17 -07:00
Aaron Patterson
a3f66e09f6 geniv objects can become too complex 2023-10-24 10:52:06 -07:00
Aaron Patterson
caf6a72348 remove IV limit / support complex shapes on classes 2023-10-24 10:52:06 -07:00
Jean Boussier
5cc44f48c5 Refactor rb_shape_transition_shape_capa to not accept capacity
This way the groth factor is encapsulated, which allows
rb_shape_transition_shape_capa to be smarter about ideal sizes.
2023-10-10 14:47:54 +02:00
Peter Zhu
196116e576 Refactor rb_ensure_iv_list_size
We don't really need obj_ivar_heap_alloc and obj_ivar_heap_realloc since
they're just one liners.
2023-08-21 09:13:36 -04:00
Peter Zhu
1e7b67f733 [Feature #19730] Remove transient heap 2023-07-13 09:27:33 -04:00