Skip to content

Refactor import caching to per-interpreter model#48

Open
benoitc wants to merge 3 commits intomainfrom
feature/per-interpreter-import-registry
Open

Refactor import caching to per-interpreter model#48
benoitc wants to merge 3 commits intomainfrom
feature/per-interpreter-import-registry

Conversation

@benoitc
Copy link
Owner

@benoitc benoitc commented Mar 22, 2026

Summary

  • Replace per-process import caching with global ETS registry
  • Imports are now applied to sys.modules when interpreters are created
  • Remove deprecated loop import NIFs (loop_import_module, etc.)
  • Add interp_apply_imports/2 and interp_flush_imports/1 NIFs
  • Contexts and loops sharing the same interpreter share sys.modules

API Changes

New functions:

  • py:init_import_registry/0 - called on application startup
  • py:get_imports/0 - list all registered imports
  • py:del_import/1,2 - remove imports from registry
  • py:clear_imports/0 - clear all registered imports
  • py_context:flush_imports/1 - flush imports from a context
  • py_context_router:flush_all_imports/0 - flush imports from all contexts

Removed exports:

  • py_event_loop_pool:import/1,2
  • py_event_loop_pool:flush_imports/0
  • py_event_loop_pool:import_stats/0
  • py_event_loop_pool:import_list/0

@benoitc benoitc force-pushed the feature/per-interpreter-import-registry branch 2 times, most recently from 94728a1 to 379c81c Compare March 22, 2026 22:18
- Replace per-process import caching with global ETS registry
- Imports are applied to sys.modules when interpreters are created
- Remove deprecated loop import NIFs (loop_import_module, etc.)
- Add interp_apply_imports/2 and interp_flush_imports/2 NIFs
- flush_imports now removes modules from sys.modules in all interpreters
- Contexts and loops sharing the same interpreter share sys.modules
- Add py:init_import_registry/0 called on application startup
- Add py:get_imports/0, py:del_import/1,2, py:clear_imports/0
- Add py_context:flush_imports/2 and py_context_router:flush_all_imports/1
- Add del_import_reimport_test verifying module removal from sys.modules
@benoitc benoitc force-pushed the feature/per-interpreter-import-registry branch from 379c81c to bc357f5 Compare March 22, 2026 22:29
benoitc added 2 commits March 23, 2026 00:21
Replace del_import with generation-based pool slot recycling.
When flush_imports is called, all pool slots are marked stale.
When a stale slot's usage count drops to 0, the subinterpreter
is destroyed and reinitialized with a fresh one.

Changes:
- Add usage_count, marked_stale, generation to subinterp_slot_t
- Add subinterp_pool_acquire/release/flush_generation functions
- Remove del_import/1,2 from py.erl (no longer needed)
- Update flush_imports to use generation-based approach
- Remove del_import tests from test suite
flush_imports now works correctly for all context types:
- Subinterpreter pool: slots marked stale and replaced
- OWN_GIL: each has own subinterpreter destroyed with context
- Main interpreter: cache generation checked on module access

Added unconditional import_cache_get_generation() and
import_cache_flush_generation() functions that work regardless
of subinterpreter support.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant