-
Notifications
You must be signed in to change notification settings - Fork 230
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
Optimize ContentLayers._isSubtreeDirty #1964
Conversation
isDirty = isDirty || _isSubtreeDirty(childElement); | ||
}); | ||
return isDirty; | ||
bool _isSubtreeDirty(Element element) { |
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.
Please order method declarations in the order that they're used. In this case _isSubtreeDirty()
before _isSubtreeDirtyVisitor()
.
PS - Typically we place all static members above all instance members. However, in this very unusual situation, we'll treat the static property and method as instance members because we're working around a language issue.
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.
Where should the static variable go?
} | ||
element.visitChildren(_isSubtreeDirtyVisitor); |
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.
I'm surprised the compiler wasn't already in-lining this.
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.
It's an indirect call to a closure or function pointer passed to a virtual method. I don't think it's really possible to inline this. The call overhead is much less of a problem than the need to allocate closure that references local variable (which on top of it is modified from closure so itself needs to be boxed).
// This is intentionally static to prevent closure allocation during | ||
// the traversal of the element tree. | ||
static void _isSubtreeDirtyVisitor(Element element) { | ||
// Can't use the () => message syntax because it allocates a closure. |
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.
If () =>
allocates a closure, and that's harmful to performance, then why did you use that syntax up above?
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.
Because there's several orders of magnitude less overlay / underlay layers than the widgets in document. In this case I think we have over 100000 elements in document all being traversed. If you want me to I can do the same thing above (for consistency).
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.
Btw. I introduced the () =>
closure to prevent diagnosticable tree creates in debug mode during every call (closures in log are only evaluated when log lovel is reached), which is quite slow especially in debug mode.
@knopp - Can you check the failing analysis? I'm guessing it's some new lint check that's only on master? I'm not sure. |
Will do. |
@matthew-carroll, I moved the visitor method below and the PR to fix the analyser complaining is here. |
Friendly ping, @matthew-carroll, does this need anything else? |
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.
LGTM
@angelosilvestre can you cherry pick this? |
… caused by logs (#1964)
… caused by logs (#1964)
I was investigating excessive garbage collection when changing selection in documents with large number of tasks.
Much of the garbage seems to be generated from
isSubtreeDirty
, which is used to traverse, in this instance, a very large widget tree.There are two issues here. Interpolating the log string (and serializing the diagnosticable nodes) and the closure allocated for each recursive call. The string interpolation can be put behind a log level check and ideally only do for debug build, since two extra branches can give a lot of overhead and the closure allocation can be avoided by using a static method.
With this PR the garbage collection pretty much disappears from the profile: