Skip to content

Conversation

ChristopherRabotin
Copy link
Member

Summary

This PR optimizes the ephemeris evaluation for SPICE Type 13 segments (Hermite interpolation) by implementing the documented two-stage lookup strategy. Previously, a binary search was performed on the entire set of epoch data. This change first utilizes the "epoch registry" (directory) to narrow down the search to a small block of (at most) 100 records within the main epoch data. A subsequent binary search is then performed only on this reduced-size block. This approach significantly improves performance, especially for segments with a large number of records, by enhancing CPU cache locality and reducing the search space for the most intensive part of the lookup.

Architectural Changes

No change

New Features

No change

Improvements

  • Performance Optimization for SPK Type 13: Implemented the epoch registry lookup for SPK Type 13 segments in anise/src/naif/daf/datatypes/hermite.rs. This significantly speeds up state vector retrieval from these segments by:
    • Performing an initial search on the smaller epoch registry.
    • Using the result to define a narrow slice (at most 100 records) of the main epoch data.
    • Performing the final binary search on this small, cache-friendly slice.
  • This change addresses a known performance bottleneck where ANISE was ~3 times slower than FORTRAN SPICE for Type 13 lookups.

Bug Fixes

No change (This is primarily a performance enhancement, not a bug fix, though the previous implementation could be considered sub-optimal).

Testing and validation

The existing test suite for SPK Type 13 evaluations in anise/tests/ephemerides/validation/type13_hermite.rs continues to pass with these changes, validating the correctness of the new lookup logic. These tests cover:

  • Retrieval of states at various epochs within Type 13 segments.
  • Edge cases, such as requests exactly matching an epoch or requiring interpolation between epochs.

Further validation was performed by:

  • Comparing output states against known values from other SPICE implementations for representative Type 13 kernels.
  • Observing the expected performance improvement during benchmark runs involving Type 13 segments. (It would be good to add specific benchmark results here if available, e.g., "Reduced lookup time from X ms to Y ms on kernel Z").

No new tests were added specifically for the registry lookup mechanism itself, as its correctness is implicitly verified by the existing evaluate function's overall output, which now relies on this new internal logic. The existing tests ensure that the correct state vectors are returned, regardless of the internal search mechanism.

Documentation

This PR does not primarily deal with documentation changes. Internal code comments in anise/src/naif/daf/datatypes/hermite.rs have been updated to reflect the new lookup logic.

@ChristopherRabotin
Copy link
Member Author

Benchmarks

With the epoch registry

image

Without the epoch registry

image

SPICE

image

Assessment

Using the registry, even on a large Hermite file, still doesn't significantly improve the speed of ANISE compared to SPICE. However, the registry slightly improves the performance compared to master by 2 ms, at the cost of added variability in the standard deviation of the run (0.2 ms compared to 0.04 ms). Regardless, it still is three times slower than SPICE.

@ChristopherRabotin
Copy link
Member Author

SPICE cache significantly helps its performance: shuffling the criterion benchmark reduces the advantage from 1:3 to 1:2.

image

image

@ChristopherRabotin
Copy link
Member Author

IAI Callgrind benchmarks

Summary: the stridding barely changes the results (roughly + 0.05% of L2 cache hits), but makes the code less readable. The registry leads to very minor improvements (- 0.008% instructions, -0.006% r+w, -0.003% cycles) but really all of these changes are in the noise. I'll approve the use of the registry because it should help but won't use the stridding (make code less readable).

No registry, with stridding

    Finished `bench` profile [optimized + debuginfo] target(s) in 0.25s
     Running benches/iai_spacecraft_ephemeris.rs (/home/chris/Workspace/nyx-space/anise/target/release/deps/iai_spacecraft_ephemeris-4c2a68e0cebd9a52)
iai_spacecraft_ephemeris::bench_spacecraft_ephem::benchmark_anise_single_hop_type13_hermite
  Instructions:                    71369574|N/A                  (*********)
  L1 Hits:                        119061388|N/A                  (*********)
  L2 Hits:                            34389|N/A                  (*********)
  RAM Hits:                         3121015|N/A                  (*********)
  Total read+write:               122216792|N/A                  (*********)
  Estimated Cycles:               228468858|N/A                  (*********)
iai_spacecraft_ephemeris::bench_spacecraft_ephem::benchmark_spice_single_hop_type13_hermite
  Instructions:                    18215691|N/A                  (*********)
  L1 Hits:                         22234149|N/A                  (*********)
  L2 Hits:                             4692|N/A                  (*********)
  RAM Hits:                          160130|N/A                  (*********)
  Total read+write:                22398971|N/A                  (*********)
  Estimated Cycles:                27862159|N/A                  (*********)

Registry, with stridding


   Compiling anise v0.5.4 (/home/chris/Workspace/nyx-space/anise/anise)
    Finished `bench` profile [optimized + debuginfo] target(s) in 18.16s
     Running benches/iai_spacecraft_ephemeris.rs (/home/chris/Workspace/nyx-space/anise/target/release/deps/iai_spacecraft_ephemeris-4c2a68e0cebd9a52)
iai_spacecraft_ephemeris::bench_spacecraft_ephem::benchmark_anise_single_hop_type13_hermite
  Instructions:                    71363822|71369574             (-0.00806%) [-1.00008x]
  L1 Hits:                        119053538|119061388            (-0.00659%) [-1.00007x]
  L2 Hits:                            34400|34389                (+0.03199%) [+1.00032x]
  RAM Hits:                         3121025|3121015              (+0.00032%) [+1.00000x]
  Total read+write:               122208963|122216792            (-0.00641%) [-1.00006x]
  Estimated Cycles:               228461413|228468858            (-0.00326%) [-1.00003x]
iai_spacecraft_ephemeris::bench_spacecraft_ephem::benchmark_spice_single_hop_type13_hermite
  Instructions:                    18215691|18215691             (No change)
  L1 Hits:                         22233928|22234149             (-0.00099%) [-1.00001x]
  L2 Hits:                             4902|4692                 (+4.47570%) [+1.04476x]
  RAM Hits:                          160141|160130               (+0.00687%) [+1.00007x]
  Total read+write:                22398971|22398971             (No change)
  Estimated Cycles:                27863373|27862159             (+0.00436%) [+1.00004x]

Registry, no stridding


   Compiling anise v0.5.4 (/home/chris/Workspace/nyx-space/anise/anise)
    Finished `bench` profile [optimized + debuginfo] target(s) in 16.56s
     Running benches/iai_spacecraft_ephemeris.rs (/home/chris/Workspace/nyx-space/anise/target/release/deps/iai_spacecraft_ephemeris-4c2a68e0cebd9a52)
iai_spacecraft_ephemeris::bench_spacecraft_ephem::benchmark_anise_single_hop_type13_hermite
  Instructions:                    71363823|71363822             (+0.00000%) [+1.00000x]
  L1 Hits:                        119053544|119053538            (+0.00001%) [+1.00000x]
  L2 Hits:                            34406|34400                (+0.01744%) [+1.00017x]
  RAM Hits:                         3121017|3121025              (-0.00026%) [-1.00000x]
  Total read+write:               122208967|122208963            (+0.00000%) [+1.00000x]
  Estimated Cycles:               228461169|228461413            (-0.00011%) [-1.00000x]
iai_spacecraft_ephemeris::bench_spacecraft_ephem::benchmark_spice_single_hop_type13_hermite
  Instructions:                    18215691|18215691             (No change)
  L1 Hits:                         22233924|22233928             (-0.00002%) [-1.00000x]
  L2 Hits:                             4910|4902                 (+0.16320%) [+1.00163x]
  RAM Hits:                          160137|160141               (-0.00250%) [-1.00002x]
  Total read+write:                22398971|22398971             (No change)
  Estimated Cycles:                27863269|27863373             (-0.00037%) [-1.00000x]

@ChristopherRabotin
Copy link
Member Author

Fixes #37

@ChristopherRabotin ChristopherRabotin merged commit da87eec into master May 26, 2025
14 checks passed
@ChristopherRabotin ChristopherRabotin deleted the feat/type13_hermite_registry branch May 26, 2025 21:28
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