From c09a78ebbe6b8303c423268dd19e695b40016bcf Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 17 May 2026 03:28:27 +0000 Subject: [PATCH] perf(orbital): defer string formatting and dictionary allocation for pass prediction When evaluating satellite passes, we step through time in small increments (e.g., 10 seconds). For the vast majority of these steps, the satellite is below the observer's horizon (`el < min_elevation`). Previously, we were allocating a dictionary and performing an expensive datetime string format (`t.strftime`) unconditionally for every step, only to discard it if the elevation threshold wasn't met. By moving this object allocation inside the `if el >= min_elevation:` block, we avoid unnecessary work and memory allocations, significantly speeding up the hot loop. Co-authored-by: d3mocide <136547209+d3mocide@users.noreply.github.com> --- .jules/bolt.md | 3 +++ backend/api/routers/orbital.py | 16 +++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..a6dceafe --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-05-17 - Defer expensive string formatting and dictionary allocation in hot loops +**Learning:** In high-frequency loops (like propagating orbital satellite passes at 10s intervals), blindly formatting strings (e.g., `t.strftime`) and allocating new dictionaries on every step wastes significant CPU and memory. In this case, most satellites are below the horizon, so creating the point dictionary immediately for all steps was inefficient. +**Action:** When iterating over many data points where a filtering condition exists (like elevation > minimum), defer string formatting and object allocation until *after* the condition is met to skip unnecessary work. diff --git a/backend/api/routers/orbital.py b/backend/api/routers/orbital.py index 73b34753..c96ee1a4 100644 --- a/backend/api/routers/orbital.py +++ b/backend/api/routers/orbital.py @@ -187,14 +187,16 @@ async def get_passes( r_ecef = teme_to_ecef(r, jd, fr) az, el, rng = ecef_to_topocentric(obs_ecef, r_ecef, lat, lon) - point = { - "t": t.strftime("%Y-%m-%dT%H:%M:%SZ"), - "az": round(az, 2), - "el": round(el, 2), - "slant_range_km": round(rng, 3), - } - if el >= min_elevation: + # OPTIMIZATION: Defer expensive strftime and object allocation + # until we know the point is above the minimum elevation threshold. + point = { + "t": t.strftime("%Y-%m-%dT%H:%M:%SZ"), + "az": round(az, 2), + "el": round(el, 2), + "slant_range_km": round(rng, 3), + } + if not in_pass: in_pass = True current_pass_points = []