Граф коммитов

446 Коммитов

Автор SHA1 Сообщение Дата
Jean Boussier 7c12169230 Eliminate internal uses of `Data_Wrap_Struct`
Ref: https://github.com/ruby/ruby/pull/10872

These should be the last internal uses of the old `Data` API
inside Ruby itself. Some use remain in a couple default gems.
2024-06-02 13:59:11 +02:00
Nobuyoshi Nakada 8b9b150512
[Bug #20481] Check for unmarshaling ivar
Prohibit setting instance variables of existing classes and modules
via link.
2024-05-12 13:57:15 +09:00
Jean Boussier d4f3dcf4df Refactor VM root modules
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-06 15:33:43 -05:00
Jean Boussier b4a69351ec Move FL_SINGLETON to FL_USER1
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-03-06 13:11:41 -05:00
Peter Zhu e8e2415bb3 Use RB_SPECIAL_CONST_P instead of rb_special_const_p
rb_special_const_p returns a VALUE (Qtrue or Qfalse), so we shouldn't
assume that Qfalse is 0. We should instead use RB_SPECIAL_CONST_P.
2024-02-27 21:11:11 -05:00
Yusuke Endoh 25d74b9527 Do not include a backtick in error messages and backtraces
[Feature #16495]
2024-02-15 18:42:31 +09:00
Peter Zhu d0b774cfb8 Remove null checks for xfree
xfree can handle null values, so we don't need to check it.
2024-01-19 10:25:02 -05:00
Adam Hess 6816e8efcf Free everything at shutdown
when the RUBY_FREE_ON_SHUTDOWN environment variable is set, manually free memory at shutdown.

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Co-authored-by: Peter Zhu <peter@peterzhu.ca>
2023-12-07 15:52:35 -05:00
John Hawthorn 0c3593b657 Use free with ruby_dtoa
In ae0ceafb0c0d05cc80623b525070255e3abb34ef ruby_dtoa was switched to
use malloc instead of xmalloc, which means that consumers should be
using free instead of xfree. Otherwise we will artificially shrink
oldmalloc_increase_bytes.
2023-12-07 09:23:02 -08: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
Jean Boussier 5278742bf0 marshal.c: embed load and dump argument objects
This avoid a bit of needless malloc churn.
2023-11-20 17:43:01 +01:00
Nobuyoshi Nakada f0923164d8
Align the type of `num_entries` to `st_table` 2023-08-20 20:12:44 +09:00
Nobuyoshi Nakada 9ac070d0ff
Copy instance variables to regexp from marshaled data
`rb_ivar_foreach` disallows `ST_DELETE`.
2023-06-20 14:02:40 +09:00
Jean Boussier 6339cb70c3 marshal.c: shallow freeze user objects
When `freeze: true` argument is passed.

[Bug #19427]
2023-04-26 16:54:25 +02:00
Jean Boussier d2520b7b76 Marshal.load: restore instance variables on Regexp
[Bug #19439]

The instance variables were restore on the Regexp source,
not the regexp itself.

Unfortunately we have a bit of a chicken and egg problem.

The source holds the encoding, and the encoding need to be set on
the source to be able to instantiate the Regexp.

So the instance variables have to be read on the `source`.
To correct this we transfert the instance variables after
instantiating the Regexp.

The only way to avoid this would be to read the instance variable
twice and rewind.
2023-02-21 13:57:04 +01:00
Jean Boussier 1a4b4cd7f8 Move `attached_object` into `rb_classext_struct`
Given that signleton classes don't have an allocator,
we can re-use these bytes to store the attached object
in `rb_classext_struct` without making it larger.
2023-02-16 08:14:44 +01:00
Jean Boussier 7ddcee5928 Marshal.load: also freeze extended objects
[Bug #19427]

The `proc` wouldn't be called either, that fixes both.
2023-02-13 17:08:21 +01:00
Aaron Patterson edc7af48ac Stop transitioning to UNDEF when undefining an instance variable
Cases like this:

```ruby
obj = Object.new
loop do
  obj.instance_variable_set(:@foo, 1)
  obj.remove_instance_variable(:@foo)
end
```

can cause us to use many more shapes than we want (and even run out).
This commit changes the code such that when an instance variable is
removed, we'll walk up the shape tree, find the shape, then rebuild any
child nodes that happened to be below the "targetted for removal" IV.

This also requires moving any instance variables so that indexes derived
from the shape tree will work correctly.

Co-Authored-By: Jemma Issroff <jemmaissroff@gmail.com>
Co-authored-by: John Hawthorn <jhawthorn@github.com>
2022-12-07 09:57:11 -08:00
S-H-GAMELINKS 1f4f6c9832 Using UNDEF_P macro 2022-11-16 18:58:33 +09:00
John Hawthorn 02f1554224
Implement object shapes for T_CLASS and T_MODULE (#6637)
* Avoid RCLASS_IV_TBL in marshal.c
* Avoid RCLASS_IV_TBL for class names
* Avoid RCLASS_IV_TBL for autoload
* Avoid RCLASS_IV_TBL for class variables
* Avoid copying RCLASS_IV_TBL onto ICLASSes
* Use object shapes for Class and Module IVs
2022-10-31 14:05:37 -07:00
Jemma Issroff ad63b668e2
Revert "Revert "This commit implements the Object Shapes technique in CRuby.""
This reverts commit 9a6803c90b.
2022-10-11 08:40:56 -07:00
Aaron Patterson 9a6803c90b
Revert "This commit implements the Object Shapes technique in CRuby."
This reverts commit 68bc9e2e97d12f80df0d113e284864e225f771c2.
2022-09-30 16:01:50 -07:00
Jemma Issroff d594a5a8bd
This commit implements the Object Shapes technique in CRuby.
Object Shapes is used for accessing instance variables and representing the
"frozenness" of objects.  Object instances have a "shape" and the shape
represents some attributes of the object (currently which instance variables are
set and the "frozenness").  Shapes form a tree data structure, and when a new
instance variable is set on an object, that object "transitions" to a new shape
in the shape tree.  Each shape has an ID that is used for caching. The shape
structure is independent of class, so objects of different types can have the
same shape.

For example:

```ruby
class Foo
  def initialize
    # Starts with shape id 0
    @a = 1 # transitions to shape id 1
    @b = 1 # transitions to shape id 2
  end
end

class Bar
  def initialize
    # Starts with shape id 0
    @a = 1 # transitions to shape id 1
    @b = 1 # transitions to shape id 2
  end
end

foo = Foo.new # `foo` has shape id 2
bar = Bar.new # `bar` has shape id 2
```

Both `foo` and `bar` instances have the same shape because they both set
instance variables of the same name in the same order.

This technique can help to improve inline cache hits as well as generate more
efficient machine code in JIT compilers.

This commit also adds some methods for debugging shapes on objects.  See
`RubyVM::Shape` for more details.

For more context on Object Shapes, see [Feature: #18776]

Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org>
Co-Authored-By: Eileen M. Uchitelle <eileencodes@gmail.com>
Co-Authored-By: John Hawthorn <john@hawthorn.email>
2022-09-28 08:26:21 -07:00
Aaron Patterson 06abfa5be6
Revert this until we can figure out WB issues or remove shapes from GC
Revert "* expand tabs. [ci skip]"

This reverts commit 830b5b5c35.

Revert "This commit implements the Object Shapes technique in CRuby."

This reverts commit 9ddfd2ca00.
2022-09-26 16:10:11 -07:00
Jemma Issroff 9ddfd2ca00 This commit implements the Object Shapes technique in CRuby.
Object Shapes is used for accessing instance variables and representing the
"frozenness" of objects.  Object instances have a "shape" and the shape
represents some attributes of the object (currently which instance variables are
set and the "frozenness").  Shapes form a tree data structure, and when a new
instance variable is set on an object, that object "transitions" to a new shape
in the shape tree.  Each shape has an ID that is used for caching. The shape
structure is independent of class, so objects of different types can have the
same shape.

For example:

```ruby
class Foo
  def initialize
    # Starts with shape id 0
    @a = 1 # transitions to shape id 1
    @b = 1 # transitions to shape id 2
  end
end

class Bar
  def initialize
    # Starts with shape id 0
    @a = 1 # transitions to shape id 1
    @b = 1 # transitions to shape id 2
  end
end

foo = Foo.new # `foo` has shape id 2
bar = Bar.new # `bar` has shape id 2
```

Both `foo` and `bar` instances have the same shape because they both set
instance variables of the same name in the same order.

This technique can help to improve inline cache hits as well as generate more
efficient machine code in JIT compilers.

This commit also adds some methods for debugging shapes on objects.  See
`RubyVM::Shape` for more details.

For more context on Object Shapes, see [Feature: #18776]

Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org>
Co-Authored-By: Eileen M. Uchitelle <eileencodes@gmail.com>
Co-Authored-By: John Hawthorn <john@hawthorn.email>
2022-09-26 09:21:30 -07:00
S-H-GAMELINKS 79f50b9d02 Using is_broken_string function 2022-09-10 09:32:51 +09:00
Nobuyoshi Nakada 5044832fec
`w_bigfixnum` is used only for large FIXNUM 2022-09-02 22:11:12 +09:00
Nobuyoshi Nakada 92d2476208
Adjust styles [ci skip] 2022-09-02 14:49:42 +09:00
John Hawthorn 0608a9a086
Optimize Marshal dump/load for large (> 31-bit) FIXNUM (#6229)
* Optimize Marshal dump of large fixnum

Marshal's FIXNUM type only supports 31-bit fixnums, so on 64-bit
platforms the 63-bit fixnums need to be represented in Marshal's
BIGNUM.

Previously this was done by converting to a bugnum and serializing the
bignum object.

This commit avoids allocating the intermediate bignum object, instead
outputting the T_FIXNUM directly to a Marshal bignum. This maintains the
same representation as the previous implementation, including not using
LINKs for these large fixnums (an artifact of the previous
implementation always allocating a new BIGNUM).

This commit also avoids unnecessary st_lookups on immediate values,
which we know will not be in that table.

* Fastpath for loading FIXNUM from Marshal bignum

* Run update-deps
2022-08-15 16:14:12 -07:00
Nobuyoshi Nakada 58c8b6e862
Adjust styles [ci skip] 2022-08-06 10:13:20 +09:00
Peter Zhu efb91ff19b Rename rb_ary_tmp_new to rb_ary_hidden_new
rb_ary_tmp_new suggests that the array is temporary in some way, but
that's not true, it just creates an array that's hidden and not on the
transient heap. This commit renames it to rb_ary_hidden_new.
2022-07-26 09:12:09 -04:00
Takashi Kokubun 5b21e94beb Expand tabs [ci skip]
[Misc #18891]
2022-07-21 09:42:04 -07:00
S-H-GAMELINKS 420f3ced4d Using is_ascii_string to check encoding 2022-06-17 12:02:50 +09:00
Kazuhiro NISHIYAMA 04f07713d1
Fix typos [ci skip] 2021-12-25 10:33:49 +09:00
Jean Boussier afcbb501ac marshal.c Marshal.load accepts a freeze: true option.
Fixes [Feature #18148]

When set, all the loaded objects are returned as frozen.

If a proc is provided, it is called with the objects already frozen.
2021-10-05 18:34:56 +02:00
S.H dc9112cf10
Using NIL_P macro instead of `== Qnil` 2021-10-03 22:34:45 +09:00
Nobuyoshi Nakada d087214658 Restore Hash#compare_by_identity mode [Bug #18171] 2021-10-02 11:43:35 +09:00
Jean byroot Boussier 529fc204af
marshal.c: don't call the proc with partially initialized objects. (#4866)
For cyclic objects, it requires to keep a st_table of the partially
initialized objects.
2021-09-30 16:50:31 +02:00
Nobuyoshi Nakada 8226c33bb5
Add symname_equal_lit for comparison with a string literal 2021-09-23 22:07:52 +09:00
Nobuyoshi Nakada 49af9012a2
Prohibit invalid encoding symbols [Bug #18184] 2021-09-23 16:02:44 +09:00
Nobuyoshi Nakada 7cec727612
Check instance variable count overflow 2021-09-23 14:01:08 +09:00
Nobuyoshi Nakada 64bdad5991
Extract ruby2_keywords predicate and setter 2021-09-23 13:59:45 +09:00
Nobuyoshi Nakada 842a4cb915
Turned to_be_skipped_id to an inline function 2021-09-23 10:55:49 +09:00
Nobuyoshi Nakada 552728a23a
Check the encoding of `ruby2_keywords_flag` [Bug #18184] 2021-09-23 00:07:17 +09:00
Nobuyoshi Nakada 7c0230b05d
Check the entire name as `ruby2_keywords_flag` [Bug #18184] 2021-09-22 19:04:55 +09:00
Jean Boussier 89242279e6 Marshal.load: do not call the proc until strings have their encoding
Ref: https://bugs.ruby-lang.org/issues/18141
2021-09-15 08:00:18 +09:00
S.H 71ba09632b
Remove unneeded declarations 2021-03-20 21:00:29 +09:00
Stefan Stüben 8c2e5bbf58 Don't redefine #rb_intern over and over again 2020-10-21 12:45:18 +09:00
Nobuyoshi Nakada 5c49bb5486
Removed useless casts 2020-09-05 17:34:12 +09:00
卜部昌平 ff30358d13 RARRAY_AREF: convert into an inline function
RARRAY_AREF has been a macro for reasons.  We might not be able to
change that for public APIs, but why not relax the situation internally
to make it an inline function.
2020-08-15 12:09:26 +09:00