Skip to content
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

Improve _GatherDependencies performance by pre-caching path lookup in parallel #1815

Conversation

csyshing
Copy link

Description of Change(s)

This PR is to improve the performance for UsdImagingDelegate::SetTime by pre-caching path dependencies in parallel, in our production shot, the time dropped from 1.2 min to be 10.x second in total.

Fixes Issue(s)

  • [x ] I have submitted a signed Contributor License Agreement

@jilliene
Copy link

Filed as internal issue #USD-7294

Copy link
Contributor

@tcauchois tcauchois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for the PR! I've left a few comments for you, on some ideas for performance improvements and then some discussion of names/function signatures. Would you mind taking a look?

typedef TfHashMap<SdfPath, SdfPathVector, SdfPath::Hash> _DependenciesCacheMap;
_DependenciesCacheMap _dependenciesCacheMap;

void _GatherDependenciesCache(SdfPath const &subtree,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to _CacheDependencies or _PrecacheDependencies? It makes it more clear what the function is doing.

The pixar code convention is that parameters a function writes out to have to be a pointer, not a reference. Since the SdfPathVector is owned by the _dependenciesCacheMap, you could change this to SdfPathVector**, or else make the return type "const SdfPathVector&" instead of void...

@@ -659,7 +659,13 @@ class UsdImagingDelegate : public HdSceneDelegate, public TfWeakBase {
_DependencyMap _dependencyInfo;

void _GatherDependencies(SdfPath const& subtree,
SdfPathVector *affectedCachePaths);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with _GatherDependenciesCache, our code convention is to not have data written out to reference parameters.

SdfPathVector& affectedCachePaths);

typedef TfHashMap<SdfPath, SdfPathVector, SdfPath::Hash> _DependenciesCacheMap;
_DependenciesCacheMap _dependenciesCacheMap;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried about this being confused with _dependencyInfo with the current name. Some other names that could work: _flattenedDependencyCache, _recursiveDependencyCache, etc.

// Pre-cache dependencies in parallel
WorkDispatcher resyncPathsCacheDispatcher;
for (const auto& usdPath: sortedUsdPathsToResync) {
auto& affectedCachePaths = _dependenciesCacheMap[usdPath];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Building "sortedUsdPathsToResync" will be expensive. You can delete the loop above by running through "usdPathsToResync", doing the GetPrimPath/GetParentPath/etc, and then replacing this line with:

auto pair = _dependenciesCacheMap.insert(usdPath);
if (pair.second) { continue; /* already inserted */ }
auto& affectedCachePaths = pair->first.second;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note also that you might want to add caching for "usdPathsToUpdate" as well. The paths to resync are a result of USD composition/type changes, whereas updates reflect property value changes; both call _GatherDependencies.

@csyshing
Copy link
Author

csyshing commented Jul 4, 2022

Thanks for the review, I will update as soon as I can.

…ion, rename variable for better readability
@csyshing
Copy link
Author

csyshing commented Aug 8, 2022

Hi @tcauchois , sorry for the delay, I have addressed the feedback in commit b8a78ea .
This PR was created based on v22.03, I could rebase the change and fast-forward to be based on latest dev instead, but let me know if you see any other problem, once it's ready, I could update again.
Please have a look and let me know, cheers!

@pixar-oss pixar-oss merged commit b1196c8 into PixarAnimationStudios:dev Sep 1, 2022
pixar-oss pushed a commit that referenced this pull request Jan 4, 2023
… dependency gathering

PR #1815 introduced a (persistent) flattened dependency cache map in UsdImagingDelegate to improve performance when a large hierarchy of prims are resync'd or updated. This resulted in a few bugs stemming from the cache and the existing _dependencyInfo map being out-of-sync.

This change addresses these issues:
- Use a local cache instead of a persistent one that is populated in parallel. This lets us salvage a lot of the performance benefits and avoids the complexity of maintaining an up-to-date and nimble cache.
- Clear the cache when any changes are made to the _dependencyInfo map from resyncing the Usd prim or subtree.
- Add a simple test case that would've caught the bug from the PR.

(Internal change: 2258076)
seando-adsk added a commit to autodesk-forks/USD that referenced this pull request Mar 1, 2024
…syshing/usdImaging_add_parallel_caching_for_GatherDependencies"

This reverts commit b1196c8, reversing
changes made to 4be508a.
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.

4 participants