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

ValuePlug : Avoid unnecessary reference counting #5520

Conversation

johnhaddon
Copy link
Member

When getObjectValue() returns m_staticValue, the caller does not need to own a reference, because it is guaranteed to be kept alive by the plug itself. The caller only needs to own a reference to computed values, which may not be cached, or may be evicted from the cache on a secondard thread.

This yields the following performance improvements :

  • testHashCacheOverhead (GafferTest.ValuePlugTest.ValuePlugTest) : was 1.15s now 1.05s (9% reduction)
  • testStaticNumericValuePerformance (GafferTest.ValuePlugTest.ValuePlugTest) : was 0.76s now 0.01s (99% reduction)
  • testStaticObjectValuePerformance (GafferTest.ValuePlugTest.ValuePlugTest) : was 0.78s now 0.72s (8% reduction)
  • testStaticStringValuePerformance (GafferTest.ValuePlugTest.ValuePlugTest) : was 0.95s now 0.01s (99% reduction)

@danieldresser-ie, I did wonder about creating some sort of intrusive/raw pointer variant to return from getObjectValue() rather than passing the owner in separately. I think such a thing might make the call sites slightly simpler, but since this is really only likely to be used internally I wasn't sure it was worth the extra code. Let me know what you think...

When `getObjectValue()` returns `m_staticValue`, the caller does not need to own a reference, because it is guaranteed to be kept alive by the plug itself. The caller only needs to own a reference to computed values, which may not be cached, or may be evicted from the cache on a secondard thread.

This yields the following performance improvements :

- testHashCacheOverhead (GafferTest.ValuePlugTest.ValuePlugTest) : was 1.15s now 1.05s (9% reduction)
- testStaticNumericValuePerformance (GafferTest.ValuePlugTest.ValuePlugTest) : was 0.76s now 0.01s (99% reduction)
- testStaticObjectValuePerformance (GafferTest.ValuePlugTest.ValuePlugTest) : was 0.78s now 0.72s (8% reduction)
- testStaticStringValuePerformance (GafferTest.ValuePlugTest.ValuePlugTest) : was 0.95s now 0.01s (99% reduction)
@johnhaddon johnhaddon self-assigned this Nov 1, 2023
@danieldresser-ie
Copy link
Contributor

This all looks good to me. As for your question about IntrusiveOrNotPtr - I feel like whether that would be worthwhile or not depends on whether there are other places we'd want to use IntrusiveOrNotPtrs. I feel like it's probably not worth introducing a new idea like that just for this one API - but maybe there are other places they would be useful?

@johnhaddon
Copy link
Member Author

I feel like it's probably not worth introducing a new idea like that just for this one API - but maybe there are other places they would be useful?

I can't think of one at the moment, and I think it might be quite tricky to get the semantics and usage clear enough for general consumption. I did look around to see if there was a pre-rolled one in boost or not but there wasn't. There is a trick you can do with a unique_ptr with a custom deleter which uses a bit of state to tell it to not actually delete (or removeRef in this case). But I thought that returning a unique_ptr would be more confusing than the owner approach since we're really pointing to potentially shared values.

@danieldresser-ie
Copy link
Contributor

I don't see anything wrong with leaving this as is.

@johnhaddon johnhaddon merged commit 03a2875 into GafferHQ:1.3_maintenance Nov 1, 2023
4 checks passed
@johnhaddon johnhaddon deleted the valuePlugGetValueOptimisation branch November 8, 2023 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants