Skip to content

Cache the detector layout file in MetisLMSSpectralTraceList to avoid reading the file N*28 times for every OpticalTrain usage - #987

Merged
astronomyk merged 1 commit into
mainfrom
perf/cache-lms-layout-read
Aug 24, 2026
Merged

Cache the detector layout file in MetisLMSSpectralTraceList to avoid reading the file N*28 times for every OpticalTrain usage#987
astronomyk merged 1 commit into
mainfrom
perf/cache-lms-layout-read

Conversation

@astronomyk

@astronomyk astronomyk commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

TL;DR - tiny efficiency speed up to avoid excessive disk reads of the DetectorLayout file
@teutoburg requesting your review for this one as it's more about ScopeSim mehcanics, rather than METIS mechanics


What

MetisLMSSpectralTrace.fov_grid needs the detector limits and read + parsed the layout file (!DET.layout.file_name) from disk on every call. Building an LMS trace list calls it several times for each of the 28 slices (trace construction, update_meta, volume setup), so the same small file was read dozens of times per OpticalTrain — more during observe().

The read is wrapped in a small lru_cache'd module function (maxsize=8, keyed on the filename), so each layout file is read once per session.

Testing

  • New test TestDetectorLayoutCache::test_layout_file_is_read_only_once counts the actual ascii.read calls for repeated lookups.
  • A notebook counting the file reads while building the real METIS LMS trace list (lookups vs actual reads) is attached below.

MetisLMSSpectralTrace.fov_grid re-read and re-parsed the detector
layout file from disk on every call. During construction of an LMS
trace list it is called several times for each of the 28 slices (once
per trace at construction, again through update_meta), so the same
small file was read dozens of times.

Wrap the read in a small lru_cache'd module function.

Adds a test that the file is read only once for repeated calls.
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 76.46%. Comparing base (3cdfc14) to head (74b4108).

Files with missing lines Patch % Lines
scopesim/effects/metis_lms_trace_list.py 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #987      +/-   ##
==========================================
+ Coverage   76.45%   76.46%   +0.01%     
==========================================
  Files          69       69              
  Lines        9021     9025       +4     
==========================================
+ Hits         6897     6901       +4     
  Misses       2124     2124              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@astronomyk astronomyk changed the title Cache the detector layout file read in MetisLMS fov_grid Cache the detector layout file in MetisLMSSpectralTraceList to avoid reading the file N*28 times for every OpticalTrain usage Aug 24, 2026
@astronomyk
astronomyk requested a review from teutoburg August 24, 2026 14:09
@astronomyk
astronomyk marked this pull request as ready for review August 24, 2026 14:10
@teutoburg teutoburg moved this to 👀 Awaiting Review in ScopeSim-development Aug 24, 2026
@teutoburg teutoburg added the performance Execution speed or memory consumtion label Aug 24, 2026

@teutoburg teutoburg 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.

This shouldn't hurt I guess. Now stop playing with the toaster and go back to being a manager 😜

@astronomyk
astronomyk merged commit beb191d into main Aug 24, 2026
54 of 56 checks passed
@astronomyk
astronomyk deleted the perf/cache-lms-layout-read branch August 24, 2026 14:40
@github-project-automation github-project-automation Bot moved this from 👀 Awaiting Review to ✅ Done in ScopeSim-development Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Execution speed or memory consumtion

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

2 participants