/// <summary>
                /// Saves subject runtime state.
                /// </summary>
                /// <param name="entity">The entity to save.</param>
                /// <param name="stream">The stream to save to.</param>
                protected virtual void SaveSubjectState(SubjectEntity entity, Stream stream)
                {
                    Debug.Assert(entity != null, "Entity should not be null.");
                    Debug.Assert(stream != null, "Stream should not be null.");

                    var policy = _engine.Parent._serializationPolicy;

                    using (entity.Measure(EntityMetric.SaveState))
                    {
                        using var operatorStateWriter = new OperatorStateWriterFactory(stream, policy);

                        operatorStateWriter.WriteHeader();

                        if (entity.Instance is IStatefulOperator op)
                        {
                            operatorStateWriter.SaveState(op);
                        }
                    }
                }
                /// <summary>
                /// Saves subscription runtime state.
                /// </summary>
                /// <param name="entity">The entity to save.</param>
                /// <param name="stream">The stream to save to.</param>
                protected virtual void SaveSubscriptionState(SubscriptionEntity entity, Stream stream)
                {
                    Debug.Assert(entity != null, "Entity should not be null.");
                    Debug.Assert(stream != null, "Stream should not be null.");

                    var instance = entity.Instance;

                    if (instance != null)
                    {
                        var policy = _engine.Parent._serializationPolicy;

                        using (entity.Measure(EntityMetric.SaveState))
                        {
                            using var operatorStateWriter = new OperatorStateWriterFactory(stream, policy);

                            operatorStateWriter.WriteHeader();

                            SubscriptionStateVisitor.SaveState(instance, operatorStateWriter);
                        }
                    }
                }