Project

General

Profile

Actions

Feature #20589

open

Resize array in `rb_ary_freeze` and use `rb_ary_freeze` internally for arrays

Added by eileencodes (Eileen Uchitelle) 8 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:118365]

Description

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:

$ 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:

$ 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

Related issues 1 (1 open0 closed)

Related to Ruby master - Feature #16291: Introduce support for resize in rb_ary_freeze and prefer internal use of rb_ary_freeze and rb_str_freeze for String and Array typesAssignednobu (Nobuyoshi Nakada)Actions
Actions #1

Updated by byroot (Jean Boussier) 8 days ago

  • Related to Feature #16291: Introduce support for resize in rb_ary_freeze and prefer internal use of rb_ary_freeze and rb_str_freeze for String and Array types added
Actions

Also available in: Atom PDF

Like0
Like0