Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow recycling fibers by GC if not referenced directly #6253

Merged
merged 2 commits into from Apr 25, 2024

Commits on Apr 23, 2024

  1. Don't mrb_realloc_simple() call mrb_full_gc() in the sweep phase

    `mrb_env_unshare()` calls `mrb_realloc_simple()` and follows `mrb_full_gc()` to avoid an infinite loop where `mrb_env_unshare()` is called again.
    This does not occur at this time, but may occur in subsequent patches.
    dearblue committed Apr 23, 2024
    Configuration menu
    Copy the full SHA
    c8c7d1a View commit details
    Browse the repository at this point in the history
  2. Allow recycling fibers by GC if not referenced directly

    The patch assumes that `struct REnv::cxt` only performs checks with the `OP_BREAK` and `OP_RETURN_BLK` instructions, and does not reference the entity.
    Therefore, by changing to a weak reference, it is possible to collect fibers that are no longer directly referenced while in the suspended state.
    
    However, we need to detach the living env objects that remain in the call stack of the fiber.
    So, in effect, it involves a revert of following commits.
      - commit a3365d8
      - commit 57ffa1c
    
    Examples of the effects of change are shown below.
    Note that it was built with `rake MRUBY_CONFIG=host-debug`.
    
    ```ruby
    f = Fiber.new { (x, y, z) = "X", "Y", "Z"; Fiber.yield -> { [x, y, z] } }
    g = f.resume
    GC.start
    p ObjectSpace.memsize_of_all
    # => 59532
    g.call
    # => ["X", "Y", "Z"]
    f = nil
    GC.start
    ObjectSpace.memsize_of_all
    # BEFORE => 59532
    # AFTER  => 58044
    g.call
    # => ["X", "Y", "Z"]
    ```
    dearblue committed Apr 23, 2024
    Configuration menu
    Copy the full SHA
    f1c9260 View commit details
    Browse the repository at this point in the history