Skip to content

Make (gmeep) s-parameter caching more stable#679

Merged
joamatab merged 1 commit intogdsfactory:mainfrom
gronniger:fix_gmeep_caching
Feb 28, 2026
Merged

Make (gmeep) s-parameter caching more stable#679
joamatab merged 1 commit intogdsfactory:mainfrom
gronniger:fix_gmeep_caching

Conversation

@gronniger
Copy link
Contributor

@gronniger gronniger commented Feb 26, 2026

Currently, metadata generated by kfactory in the GDS file used to hash the simulated component can cause cache miss when changing gdsfactory or kfactory version or changing order of certain operations (which change neither component nor GDS shapes).
With this PR, metadata is not written to GDS for hashing, so caching should be more stable.
This is tested for gmeep, but should work the same for other simulation tools.

Summary by Sourcery

Stabilize s-parameter caching by ensuring component GDS exports used for hashing are written without extraneous metadata and consistently handled paths.

Enhancements:

  • Export component GDS files for hashing without metadata and empty cells to make s-parameter cache keys independent of non-geometric changes.
  • Standardize path handling in s-parameter path utilities by using the Path alias consistently.

@gronniger gronniger requested a review from joamatab as a code owner February 26, 2026 12:46
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Feb 26, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts how component GDS files are written and paths handled so that s-parameter caching ignores kfactory/gdsfactory metadata and uses more stable hashing, while also standardizing path handling via pathlib.Path imports.

Sequence diagram for stable sparameter caching via GDS hashing

sequenceDiagram
    actor User
    participant Caller as caller_module
    participant GetPath as _get_sparameters_path
    participant Gf as gf
    participant Comp as Component
    participant Hash as get_component_hash
    participant FS as filesystem

    User->>Caller: request_sparameters(component, **kwargs)
    Caller->>GetPath: _get_sparameters_path(component, dirpath, **kwargs)
    GetPath->>Gf: get_component(component)
    Gf-->>GetPath: Component instance
    GetPath->>Hash: get_component_hash(component)

    activate Hash
    Hash->>Comp: write_gds(no_empty_cells=True, with_metadata=False)
    Comp-->>Hash: gdspath
    Hash->>FS: read_bytes(gdspath)
    FS-->>Hash: gds_bytes
    Hash->>Hash: md5(gds_bytes)
    Hash->>FS: unlink(gdspath)
    Hash-->>GetPath: component_hash
    deactivate Hash

    GetPath->>GetPath: dirpath = Path(dirpath)
    GetPath->>GetPath: dirpath = dirpath / component.name
    GetPath-->>Caller: sparameters_path
    Caller-->>User: return cached_or_computed_sparameters
Loading

Flow diagram for get_component_hash using metadatafree GDS

flowchart TD
    A["get_component_hash(component)"] --> B["call component.write_gds(no_empty_cells=True, with_metadata=False)"]
    B --> C["receive gdspath string"]
    C --> D["convert to Path(gdspath)"]
    D --> E["read_bytes() from GDS file"]
    E --> F["compute md5 hash of bytes"]
    F --> G["unlink() temporary GDS file"]
    G --> H["return hash string"]
Loading

File-Level Changes

Change Details Files
Make component GDS hashing stable by excluding metadata and empty cells from the temporary GDS used for the hash.
  • Change temporary GDS write call to pass no_empty_cells=True so empty cells are omitted from the hash input GDS.
  • Disable metadata writing in the temporary GDS used for hashing by passing with_metadata=False.
  • Continue hashing the temporary GDS file via its MD5 and deleting it afterwards to avoid leaving artifacts.
gplugins/common/utils/get_sparameters_path.py
Standardize path handling in s-parameter path computation using direct Path imports instead of pathlib.Path.
  • Replace pathlib.Path calls with Path to match the existing Path import style.
  • Normalize dirpath handling by wrapping provided or default directory strings with Path consistently before use.
gplugins/common/utils/get_sparameters_path.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • You still convert dirpath to a Path twice in _get_sparameters_path; consider doing this once near the top of the function to avoid redundant work and keep the logic clearer.
  • In get_component_hash, you might consider using a context-managed temporary file (e.g., tempfile.NamedTemporaryFile) or an in-memory GDS export if available, to avoid relying on manual deletion and make the cleanup more robust.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- You still convert `dirpath` to a `Path` twice in `_get_sparameters_path`; consider doing this once near the top of the function to avoid redundant work and keep the logic clearer.
- In `get_component_hash`, you might consider using a context-managed temporary file (e.g., `tempfile.NamedTemporaryFile`) or an in-memory GDS export if available, to avoid relying on manual deletion and make the cleanup more robust.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@joamatab joamatab merged commit 0ef80f9 into gdsfactory:main Feb 28, 2026
11 of 17 checks passed
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