Skip to content

Implement Phase 1 Native PEP 0810 Lazy Loading#17591

Draft
hebaalazzeh wants to merge 2 commits into
mainfrom
prototype-phase1-pep810
Draft

Implement Phase 1 Native PEP 0810 Lazy Loading#17591
hebaalazzeh wants to merge 2 commits into
mainfrom
prototype-phase1-pep810

Conversation

@hebaalazzeh

Copy link
Copy Markdown
Contributor

Overview

This PR represents Phase 1 of our initiative to resolve severe initialization bottlenecks (~10s-13s) in generated client libraries by adopting Native Python 3.15 Explicit Lazy Imports (PEP 0810).
To avoid introducing maintenance burdens or subtle breaking changes via custom import hooks, this PR focuses exclusively on utilizing native interpreter standards.

Implementation Architecture

We modify the upstream GAPIC Generator to emit a native __lazy_modules__ set at the top of the __init__.py files, immediately followed by the standard eager imports.

  • Python 3.15+ (Native Acceleration): The Python 3.15 runtime natively intercepts the standard imports referenced in the __lazy_modules__ set and treats them as fast C-level lazy proxies. Startup times drop from ~13s to ~200ms, and peak RAM consumption drops by up to 85%.
  • Python 3.14 and Older (Zero Blast Radius): Older Python environments safely ignore the __lazy_modules__ variable and process the standard eager imports exactly as they do today. This guarantees 100% backwards compatibility with zero risk.
  • Perfect Static Typing: Because standard imports are still present in the file, IDEs (IntelliSense) and static analyzers (MyPy) maintain zero-friction support.
    Example Structure:
import sys
# 1. Native C-Level Lazy Proxying for Python 3.15+
if sys.version_info >= (3, 15):
    __lazy_modules__ = {
       f"{__name__}.services.accelerator_types",
       f"{__name__}.services.addresses",
       f"{__name__}.types.compute",
    }
# 2. Standard eager imports
# Evaluated instantly by older Python versions. Intercepted natively by Python 3.15.
from .services.accelerator_types import AcceleratorTypesClient
from .services.addresses import AddressesClient
from .types.compute import Address
__all__ = (
    "AcceleratorTypesClient",
    "AddressesClient",
    "Address",
)

Related Links:

Design Doc: go/sdk-performance-design
Towards googleapis/python-aiplatform#4749

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request adds a conditional check for Python 3.15+ to define __lazy_modules__ in the initialization file of google-cloud-compute. However, the sys module is not imported, which will cause a NameError at runtime. Since this is a generated file, the generator or templates should be updated to include the necessary import rather than applying manual fixes.

Comment on lines +26 to +27

if sys.version_info >= (3, 15):

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.

critical

The sys module is not imported in this file, which will cause a NameError at runtime on all Python versions when this package is imported. Since this is a generated file, please update the generator or templates to import sys at the module level to prevent manual changes from being overwritten.

References
  1. Do not suggest manual code modifications, optimizations, or style fixes for generated files, as any changes should be implemented in the generator or templates to prevent them from being overwritten.

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