Skip to content

Store Rust stdlib modules on VirtualMachine and lazily-load them - #107

Merged
windelbouwman merged 2 commits into
RustPython:masterfrom
OddBloke:stdlib
Aug 31, 2018
Merged

windelbouwman merged 2 commits into
RustPython:masterfrom
OddBloke:stdlib

Conversation

@OddBloke

Copy link
Copy Markdown
Collaborator

This means we only have to load the modules once at startup.

Fixes #90.

This means we only have to load the modules once at startup.

Fixes RustPython#90.
@OddBloke OddBloke changed the title Store Rust stdlib modules on VirtualMachine Store Rust stdlib modules on VirtualMachine and lazily-load them Aug 30, 2018
Comment thread vm/src/stdlib/mod.rs
pub fn get_modules(vm: &VirtualMachine) -> HashMap<String, PyObjectRef> {
pub type StdlibInitFunc = fn(&PyContext) -> PyObjectRef;

pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect here HashMap<String, PyObjectRef> where the object refers to a python module instance.

Comment thread vm/src/vm.rs
frames: Vec<Frame>,
builtins: PyObjectRef,
pub sys_module: PyObjectRef,
pub stdlib_inits: HashMap<String, stdlib::StdlibInitFunc>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can make the rust parts of the stdlib part of context, so when we have more than 1 vm, the stdlib is shared. Does this make sense?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no reason why this couldn't live on PyContext instead of VirtualMachine.

Comment thread vm/src/import.rs
// Check for Rust-native modules
if let Some(module) = stdlib::get_modules(vm).get(module) {
return Ok(module.clone());
if let Some(module) = vm.stdlib_inits.get(module) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is a good idea to implement some sort of function which get_or_create's a module based on a name. This function then will check a cache HashMap<String, PyObjectRef> where the object ref is a module ref, and if not present, calls an init function on the module, so it is loaded lazily. On the other hand, we might preload all builtin modules, so that the initialization order is deterministic at all times.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that cache just sys.modules?

(We don't currently put anything in there, see #109.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha! I totally forgot about that, you are right!

@windelbouwman
windelbouwman merged commit c687db0 into RustPython:master Aug 31, 2018
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.

2 participants