Project

General

Profile

Actions

Misc #16160

open

Lazy init thread local storage

Added by methodmissing (Lourens Naudé) over 4 years ago. Updated over 4 years ago.

Status:
Open
Assignee:
-
[ruby-core:94876]

Description

References PR https://github.com/ruby/ruby/pull/2295

Why?

The local_storage member of execution context is lazy initialized and drives the Thread#[] and Thread#[]= APIs, which are Fiber local and not Thread local storage. I think the same lazy init pattern should be applied to the APIs below as well - reduces one Hash alloc per thread created that does not use thread locals.

Lazy allocates thread local storage for the following APIs

  • Thread#thread_variable_get - early returns nil on locals Hash not initialised
  • Thread#thread_variable_set - forces allocation of the locals Hash if not initilalised
  • Thread#thread_variables - early returns the empty array AND saves on Hash iteration if locals Hash not initialised
  • Thread#thread_variable? - early returns false on locals Hash not initialised

Other notes

  • Moved initial implementation from internal.h to thread.c local to call sites.
  • Preferred defs/id.def for the locals ID (seeing this pattern used more often, but not sure if that is preferred to inline rb_intern yet. Either way there's quite a few different conventions around IDs in the codebase at the moment and happy to help converging to a standard instead.
  • Maybe a flag is overkill and NIL_P on locals ivar could also work ...

Thoughts?

Actions

Also available in: Atom PDF

Like0
Like0Like0