Skip to content

Conversation

@DavidLegg
Copy link
Contributor

@DavidLegg DavidLegg commented Nov 13, 2025

  • Tickets addressed: N/A
  • Review: By commit
  • Merge strategy: Merge (no squash)

Description

Changes SimulationResultsWriter to write output JSON more directly to the chosen output file (or stdout).
It no longer builds in-memory JsonValue nor String representations of large chunks of the output.
Instead, the resultsGenerator is connected directly to the chosen output stream, and build* subroutines to build in-memory JsonValue objects are replaced by corresponding write* subroutines to write that section of the output directly to file.

When using a ResourceFileStreamer, this means opening only one resource temp file at a time and streaming data from that file to the final output file, never loading the full resource profile into memory.

This makes stateless Aerie capable of handling very large simulation results.

Verification

This was tested with a Clipper plan that generated a nearly 14GB output file.

Documentation

N/A - purely a performance-oriented refactor

Future work

There are potentially more performance gains to be had by changing the output format to one that can be written incrementally. Something like a JSON Lines file where each line is a result, in time order, and a result can be any update from the simulation: activity start or end, event on a topic, or change in resource dynamics.
Such an output file could be written as the simulation runs. Combined with the results-streaming interface that's already in place, and we could write the final output file in parallel with simulation, with no need to run a post-processing step.

David Legg added 2 commits November 12, 2025 17:45
Changes SimulationResultsWriter to write output JSON more directly to the chosen output file (or stdout).
It no longer builds in-memory JsonValue nor String representations of large chunks of the output.
Instead, the resultsGenerator is connected directly to the chosen output stream, and build* subroutines to build in-memory JsonValue objects
are replaced by corresponding write* subroutines to write that section of the output directly to file.

When using a ResourceFileStreamer, this means opening only one resource temp file at a time and streaming data from that file to the final output file, never loading the full resource profile into memory.

This makes stateless Aerie capable of handling very large simulation results. This was tested with a Clipper plan that generated a nearly 14GB output file.
When rewriting the SimulationResultsWriter for performance, I also corrected the way it handles topics and events to maintain parity with the database-based simulation results.

// Append to the array builder
simulatedActivitiesBuilder.add(actBuilder);
resultsGenerator
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor, nonblocking: the initial order of fields for directives were chosen deliberately so that all of the ID fields were next to each other at the top of the entry. Could you restore this order?

// Sadly, this requires reading the object into a JsonValue, just to write it back out (!)
try (final JsonReader jr = Json.createReader(new StringReader(s))) {
segmentsBuilder.add(jr.readObject());
resultsGenerator.write(jr.readValue());
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a comment, no changes: Shame there's no writeRaw method like in Jackson's implementation. That said, the reader and JSON Value only exist for the length of the loop, so memory should be fine.

}

/*
Json Schema for Sim results:
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you update the JSON Schema to reflect the new pattern for topics and events?

Copy link
Contributor

@Mythicaeda Mythicaeda left a comment

Choose a reason for hiding this comment

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

Thanks for improving the memory use! I think we can revisit parallelization/streaming as we go once that starts being an issue with stateless sim.

I'm only holding off on approval because of the schema docs. It's not the best place to have them, I know, but they ought to stay up-to-date while we find them a new home.

Additionally for @dandelany: while it can still only sim, the change to events/topics is the one change that I really wanted done before we "announced" Stateless Aerie/took it out of "beta"/whatever the appropriate phrase is.

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.

3 participants