Trying to come up with a way to store state specific to a fiber that will get mopped up when the fiber completes. One thing I tried was to add ivars to Fiber
, but that may have performance implications since that makes every fiber take up additional heap memory, including fibers that would not store anything in them, and more bytes per allocation requires more time (this surprised me when I discovered it). In the grand scheme of things, it might not have a huge impact on either allocation/GC time or memory used, but I haven’t benchmarked in a real application yet.
Another idea was to maintain fiber-local state in a separate data structure and monkeypatch Fiber#finalize
to remove itself from it. The hard part about using this as a pattern is that other libraries may have to do it, too, so we would have to call previous_def
to invoke a previously defined monkeypatch for it.
In order to support this pattern, though, Fiber
would need to define a no-op finalize
method. This could also have performance implications since the GC would then register a finalizer for every fiber, regardless of whether it needs it. I don’t know how much performance that requires.
Are there other ways to do fiber-local state that don’t have the potential to slow down an application?