-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
988: Cache RenderContext per instance #1251
Conversation
1c12e26
to
1bf6f2f
Compare
d54c92e
to
cf3f7c9
Compare
Idempotency checker is failing with Update: FIXED - our |
cf3f7c9
to
d9ace4f
Compare
renderState: Unit, | ||
context: RenderContext | ||
): RenderingT { | ||
if (cachedStatelessRenderContext == null || context != cachedStatefulRenderContext) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the bit you put in for ease of testing? Seems like it warrants a comment, esp. call out when / why the context might change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, this is the bit - I can add some comments here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
* This method is called a few times per instance, but we don't need to allocate a new | ||
* [StatefulWorkflow] every time, so we store it in a private property. | ||
* This is only called when the instance of the Workflow is created, so we can recreate this each | ||
* time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"this" refers to both the function and… something else – "so it can create and return a new instance each time"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
context: RenderContext | ||
): RenderingT { | ||
if (cachedRenderContext == null) { | ||
cachedRenderContext = RenderContext(context, this@StatelessWorkflow) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We support rendering the same workflow instance in multiple places, right? Do all of those render calls also get the same RenderContext
? If not, this cache would only work for a single use of this workflow object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the new equality check, at least it will now fallback to the old behavior (create a new context each time), which is at least correct if still wasted work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, and I ended up using instance ineqaulity !==
d9ace4f
to
6f2d156
Compare
1bf6f2f
to
a7dab3d
Compare
6f2d156
to
74fb6fb
Compare
a7dab3d
to
8e40b8a
Compare
0a36563
to
717d484
Compare
render = { props, _ -> render(props, RenderContext(this, this@StatelessWorkflow)) } | ||
) | ||
private val statefulWorkflow: StatefulWorkflow<PropsT, Unit, OutputT, RenderingT> = | ||
object : StatefulWorkflow<PropsT, Unit, OutputT, RenderingT>() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Consider pulling this into a named inner class. Might be better for future debugging if it has a human-readable name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! Will do.
private val statefulWorkflow: StatefulWorkflow<PropsT, Unit, OutputT, RenderingT> = | ||
object : StatefulWorkflow<PropsT, Unit, OutputT, RenderingT>() { | ||
// We want to cache the render context so that we don't have to recreate it each time | ||
// render() is called. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: These should all be kdoc.
8e40b8a
to
2e43d2c
Compare
For the InterceptedRenderContext and for the StatelessWorkflow.RenderContext.
717d484
to
646ebf7
Compare
Rather than re-creating the
RenderContext
on eachrender
for an intercepted workflow instance, or for theStatefulWorkflow
version of aStatelessWorkflow
, we create it only once, the first time it is rendered, then cache and re-use it, until a new instance is passed in torender()
, then we need to recreate it.Fixes #988 .