Skip to content

New LazyImport implementation based on lazy_object_proxy #22793

@embray

Description

@embray

This adds lazy_object_proxy as a dependency (see #22792), and rewrites the sage.misc.lazy_import.LazyImport class on top of it. This doesn't change anything else about how the lazy_import mechanism is implemented and is mostly independent of any such changes (e.g. #22752).

Pros

  • In the spirit of tickets like Use psutil instead of various hacks #21805, removes more code than it adds, replacing Sage-specific code with robust solutions from the Python community...

  • ...meaning less code for us to maintain and keep forward-compatible with future Python versions (in principle, but see also the first "con").

  • This solution is probably faster, being written purely in C and with no Cython-related overhead. The old implementation in Sage required a function call for every indirection to the proxied object (albeit a cpdef function). The lazy_object_proxy implementation in principle also has a plain C function call (Proxy__ensure_wrapped), but with typical compiler optimizations this is actually eliminated entirely for the common case where the proxied object has been initialized. I haven't done any benchmarks though, but could if requested.

  • Maintainer is an active member of the Python community and not likely to abandon the project or disappear (hard to quantify of course, but he's someone whose work I'm familiar with).

Cons

  • Effectively same amount of code to maintain if we ever need upstream changes for Sage--upstream can be more difficult to deal with than fixing code in Sage.

That said, the implementation of lazy_object_proxy is very generic and not likely to require deep changes for Sage. The old LazyImport has never required deep structural changes either.

Depends on #22792

Component: misc

Author: Erik Bray

Branch/Commit: u/embray/lazy-import-proxy @ e58d61d

Issue created by migration from https://trac.sagemath.org/ticket/22793

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions