ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:118365] [Ruby master Feature#20589] Resize array in `rb_ary_freeze` and use `rb_ary_freeze` internally for arrays
@ 2024-06-20 16:42 eileencodes (Eileen Uchitelle) via ruby-core
  2024-07-03 14:31 ` [ruby-core:118429] " byroot (Jean Boussier) via ruby-core
  0 siblings, 1 reply; 2+ messages in thread
From: eileencodes (Eileen Uchitelle) via ruby-core @ 2024-06-20 16:42 UTC (permalink / raw)
  To: ruby-core; +Cc: eileencodes (Eileen Uchitelle)

Issue #20589 has been reported by eileencodes (Eileen Uchitelle).

----------------------------------------
Feature #20589: Resize array in `rb_ary_freeze` and use `rb_ary_freeze` internally for arrays
https://bugs.ruby-lang.org/issues/20589

* Author: eileencodes (Eileen Uchitelle)
* Status: Open
----------------------------------------
GitHub PR https://github.com/ruby/ruby/pull/11030

This is a redo of https://github.com/ruby/ruby/pull/2640 and a new issue for the array portion of https://bugs.ruby-lang.org/issues/16291 because both are stale.

This change proposes the following:

1) Call `ary_shrink_capa` from `rb_ary_freeze` to resize arrays before freezing (if they are not embedded, not shared, and not a shared root).
2) Update callers to use `rb_ary_freeze` instead of `rb_obj_freeze` internally in CRuby/
3) Add an assertion to `ary_heap_realloc` that ensures frozen arrays are not being reallocated.\

The orignal issue implemented this for performance reasons, which are still valid. However, additionally this ensures that frozen arrays are not being passed to `ary_heap_realloc` and also ensures the capacity is set with `ARY_SET_CAPA` in `ary_shrink_capa`. Previously, because `ARY_SET_CAPA` was not called, `ary_heap_realloc` would get called after the array was frozen (when that is unnecessary).

Array memsize before and after this change:

Before:

```ruby
$ require 'objspace'
=> false
$ a = (1..5).to_a
=> [1, 2, 3, 4, 5]
$ ObjectSpace.memsize_of(a)
=> 200
$ a.freeze
=> [1, 2, 3, 4, 5]
$ ObjectSpace.memsize_of(a)
=> 200
```

After:

```ruby
$ require 'objspace'
=> false
$ a = (1..5).to_a
=> [1, 2, 3, 4, 5]
$ ObjectSpace.memsize_of(a)
=> 200
$ a.freeze
=> [1, 2, 3, 4, 5]
$ ObjectSpace.memsize_of(a)
=> 80
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-core:118429] [Ruby master Feature#20589] Resize array in `rb_ary_freeze` and use `rb_ary_freeze` internally for arrays
  2024-06-20 16:42 [ruby-core:118365] [Ruby master Feature#20589] Resize array in `rb_ary_freeze` and use `rb_ary_freeze` internally for arrays eileencodes (Eileen Uchitelle) via ruby-core
@ 2024-07-03 14:31 ` byroot (Jean Boussier) via ruby-core
  0 siblings, 0 replies; 2+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2024-07-03 14:31 UTC (permalink / raw)
  To: ruby-core; +Cc: byroot (Jean Boussier)

Issue #20589 has been updated by byroot (Jean Boussier).

Status changed from Open to Closed

Closing given https://github.com/ruby/ruby/pull/11030 was merged yesterday.

However it caused some assertions failures in corner cases, so I opened a followup: https://github.com/ruby/ruby/pull/11092

----------------------------------------
Feature #20589: Resize array in `rb_ary_freeze` and use `rb_ary_freeze` internally for arrays
https://bugs.ruby-lang.org/issues/20589#change-108941

* Author: eileencodes (Eileen Uchitelle)
* Status: Closed
----------------------------------------
GitHub PR https://github.com/ruby/ruby/pull/11030

This is a redo of https://github.com/ruby/ruby/pull/2640 and a new issue for the array portion of https://bugs.ruby-lang.org/issues/16291 because both are stale.

This change proposes the following:

1) Call `ary_shrink_capa` from `rb_ary_freeze` to resize arrays before freezing (if they are not embedded, not shared, and not a shared root).
2) Update callers to use `rb_ary_freeze` instead of `rb_obj_freeze` internally in CRuby/
3) Add an assertion to `ary_heap_realloc` that ensures frozen arrays are not being reallocated.\

The orignal issue implemented this for performance reasons, which are still valid. However, additionally this ensures that frozen arrays are not being passed to `ary_heap_realloc` and also ensures the capacity is set with `ARY_SET_CAPA` in `ary_shrink_capa`. Previously, because `ARY_SET_CAPA` was not called, `ary_heap_realloc` would get called after the array was frozen (when that is unnecessary).

Array memsize before and after this change:

Before:

```ruby
$ require 'objspace'
=> false
$ a = (1..5).to_a
=> [1, 2, 3, 4, 5]
$ ObjectSpace.memsize_of(a)
=> 200
$ a.freeze
=> [1, 2, 3, 4, 5]
$ ObjectSpace.memsize_of(a)
=> 200
```

After:

```ruby
$ require 'objspace'
=> false
$ a = (1..5).to_a
=> [1, 2, 3, 4, 5]
$ ObjectSpace.memsize_of(a)
=> 200
$ a.freeze
=> [1, 2, 3, 4, 5]
$ ObjectSpace.memsize_of(a)
=> 80
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-07-03 14:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-20 16:42 [ruby-core:118365] [Ruby master Feature#20589] Resize array in `rb_ary_freeze` and use `rb_ary_freeze` internally for arrays eileencodes (Eileen Uchitelle) via ruby-core
2024-07-03 14:31 ` [ruby-core:118429] " byroot (Jean Boussier) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).