There is no faster way to get the history. The latency depends on the length and size of the history. This is a critical api in Cadence so the perf has been optimized to the best within its architecture.
However, based on your use case, this is a misuse of the api. You should implement Query handler for the workflow. Worker will do everything same as you are doing to parse history.
If preferred, you can implement a very generic Query handler which return almost everything in the workflow events. So that only one Query handler can let you get anything like getting from the history. Specifically, you can put all signals, activity input output etc into a list and return the list as the query results.
Using worker query will save the latency for you. This is because Cadence worker would cache the history. When there is no changes on the workflow it will not get any more history from server. when new events are appended, only delta of events will be transported between the worker and server. Therefore the latency is minimum regardless of the length of the history.
The only case that worker will need to reload the whole history is when cache is evicted or worker restart. so it’s always recommended to keep history short so that it won’t cost too much resources in that case.
See more docs about query.
https://cadenceworkflow.io/docs/concepts/queries/#stack-trace-query
Golang
https://cadenceworkflow.io/docs/go-client/queries/
Java
https://cadenceworkflow.io/docs/java-client/queries/
Taking Golang as an example, if you want to return activity result as the query output:
func MyWorkflow(ctx workflow.Context, input string) error {
var res map[string]string
err := workflow.SetQueryHandler(ctx, "current_state", func() (map[string]string, error) {
return res, nil
})
ctx = workflow.WithActivityOptions(ctx, ...)
var act_out string
err = workflow.ExecuteActivity(ctx, ActivityA, "my_input").Get(ctx, &act_out)
if err != nil {
res["ActivityA"] = act_out
return err
}
return nil
}