Skip to content

Refactor JsonWorkflowStateSerializer to improve performance#6272

Merged
sfmskywalker merged 6 commits intomainfrom
perf/serialization
Jan 7, 2025
Merged

Refactor JsonWorkflowStateSerializer to improve performance#6272
sfmskywalker merged 6 commits intomainfrom
perf/serialization

Conversation

@sfmskywalker
Copy link
Copy Markdown
Member

@sfmskywalker sfmskywalker commented Jan 6, 2025

  • Improves performance: The converters and static properties are pre-configured when the class is initialized. Only the ReferenceHandler is refreshed per call, avoiding repeated configuration of common options like converters.
  • Thread-safe: Since _cachedOptions is immutable, it can safely be reused across threads.

Tradeoffs: Still a performance cost during cloning, but much faster than fully recreating options each time as was done before the PR.


This change is Reviewable

- **Improves performance:** The converters and static properties are pre-configured when the class is initialized. Only the `ReferenceHandler` is refreshed per call, avoiding repeated configuration of common options like converters.
- **Thread-safe:** Since `_cachedOptions` is immutable, it can safely be reused across threads.

**Tradeoffs:** Small performance cost during cloning, but still much faster than fully recreating options each time.
Updated the target framework from .NET 8 to .NET 9 for future compatibility and language improvements. Adjusted null handling in `WorkflowDefinitionImporter` for better readability and correctness. Added a cancellation token to `ReadToEndAsync` for improved async operation control.
Replaced direct use of `CrossScopedReferenceHandler` with `PerCallReferenceHandlerWrapper` for improved abstraction and reusability. Updated `ApplyOptions` to leverage the new wrapper, simplifying reference resolver management. Commented out redundant code to streamline the implementation.
The `ApplyOptions` method now correctly calls the base implementation before applying custom configurations. This ensures that any base behavior is preserved, preventing potential issues with serialization options.
Replaced `PerCallReferenceHandlerWrapper` with `CrossScopedReferenceHandler` and optimized the resolver initialization using `AsyncLocal`. This simplifies the code and ensures a more efficient handling of reference resolution during serialization.
Replaces static resolver state with an instance-level resolver in `CrossScopedReferenceHandler` to enhance flexibility and thread-safety. Refactors `JsonWorkflowStateSerializer` to use `GetOptions` for improved API consistency and clarity.
@sfmskywalker sfmskywalker merged commit ace32dd into main Jan 7, 2025
@sfmskywalker sfmskywalker deleted the perf/serialization branch January 7, 2025 10:27
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