Project

General

Profile

Actions

Feature #22613

open

iseq freeing after require/load without reference

Feature #22613: iseq freeing after require/load without reference

Added by fuzzyrichie-gusto (Richard Desmarais) 1 day ago. Updated 1 day ago.

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

Description

When Ruby loads a file, it compiles the code into bytecode. The code at the top of each file and inside class bodies only ever runs once - while the file loads.

Today Ruby keeps that one-time bytecode in memory forever, because a single block created inside a class (like a sig { } or a scope -> { }) keeps the whole file's compiled code alive.

The change frees that bytecode right after each file finishes loading, keeping only the small bits that surviving blocks still need, which cuts memory for compiled code by about 40% in our app.

This would be a default change.

https://github.com/ruby/ruby/pull/20059 is the PR.

Updated by alanwu (Alan Wu) 1 day ago 1Actions #3 [ruby-core:127649]

Good idea to save memory.
Though, freeing at a certain program point by taking a ractor barrier is incorrect. That does not prevent you from freeing code that some other ractor is still running. Like trying to free JIT code, only the GC can prove that an iseq is truly unreachable. It's much cleaner to reorganize the object graph rather than trying to do it by guessing a program point with sprinkles of adhoc checks like iseq_run_once_releasable_p().

It should be robust enough to not require adding a feature flag.

Updated by fuzzyrichie-gusto (Richard Desmarais) 1 day ago Actions #4 [ruby-core:127654]

Fair point - I went and did the GC-approach and made it default, so hopefully this works much better for the reusability part of things.

Updated by fuzzyrichie-gusto (Richard Desmarais) 1 day ago Actions #5

  • Description updated (diff)
Actions

Also available in: PDF Atom