Skip to content

Commit c97cb20

Browse files
authored
use const reference as much as possible when calling VtValue::UncheckedGet and VtValue::Get (#1892)
1 parent 9bd7381 commit c97cb20

File tree

6 files changed

+24
-22
lines changed

6 files changed

+24
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [usd#1859](https://github.com/Autodesk/arnold-usd/issues/1859) - Support PointInstancer invisibleIDs for lights
2222
- [usd#1881](https://github.com/Autodesk/arnold-usd/issues/1881) - Support UDIM and relative paths on mtlx image shaders
2323
- [usd#1884](https://github.com/Autodesk/arnold-usd/issues/1884) - Set a proper name to skydome image node in Hydra
24+
- [usd#1890](https://github.com/Autodesk/arnold-usd/issues/1890) - Reduce VtArray memory consumption, mostly in the instancer.
2425

2526
## Next minor release - 5454a9e
2627

libs/render_delegate/instancer.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,15 @@ void HdArnoldInstancer::CalculateInstanceMatrices(HdArnoldRenderDelegate* render
214214
const float fps = 1.0f / (reinterpret_cast<HdArnoldRenderParam*>(renderDelegate->GetRenderParam())->GetFPS());
215215
const float fps2 = fps * fps;
216216
VtValue velValue = GetDelegate()->Get(instancerId, HdTokens->velocities);
217-
VtVec3fArray velocities;
218-
if (velValue.IsHolding<VtVec3fArray>()) {
219-
velocities = velValue.UncheckedGet<VtVec3fArray>();
220-
}
217+
VtVec3fArray emptyVelocities;
218+
const VtVec3fArray& velocities =
219+
velValue.IsHolding<VtVec3fArray>() ? velValue.UncheckedGet<VtVec3fArray>() : emptyVelocities;
221220

222221
VtValue accelValue = GetDelegate()->Get(instancerId, HdTokens->accelerations);
223-
VtVec3fArray accelerations;
224-
if (accelValue.IsHolding<VtVec3fArray>()) {
225-
accelerations = accelValue.UncheckedGet<VtVec3fArray>();
226-
}
222+
VtVec3fArray emptyAccelerations;
223+
const VtVec3fArray& accelerations =
224+
accelValue.IsHolding<VtVec3fArray>() ? accelValue.UncheckedGet<VtVec3fArray>() : emptyAccelerations;
225+
227226
const bool hasVelocities = velocities.size() > 0;
228227
const bool hasAccelerations = accelerations.size() > 0;
229228
const bool velBlur = hasAccelerations || hasVelocities;

libs/render_delegate/render_delegate.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ void HdArnoldRenderDelegate::_SetRenderSetting(const TfToken& _key, const VtValu
748748
// Solaris-specific command line, it can have an argument "-o output.exr" to override
749749
// the output image. We might end up using this for arnold drivers
750750
if (value.IsHolding<VtStringArray>()) {
751-
const VtStringArray commandLine = value.UncheckedGet<VtArray<std::string>>();
751+
const VtStringArray &commandLine = value.UncheckedGet<VtArray<std::string>>();
752752
for (unsigned int i = 0; i < commandLine.size(); ++i) {
753753
// husk argument for output image
754754
if (commandLine[i] == "-o" && i < commandLine.size() - 2) {
@@ -790,10 +790,10 @@ void HdArnoldRenderDelegate::_ParseDelegateRenderProducts(const VtValue& value)
790790
if (!value.IsHolding<DataType>()) {
791791
return;
792792
}
793-
auto products = value.UncheckedGet<DataType>();
793+
const auto &products = value.UncheckedGet<DataType>();
794794
// For Render Delegate products, we want to eventually create arnold drivers
795795
// during batch rendering #1422
796-
for (auto& productIter : products) {
796+
for (const auto& productIter : products) {
797797
HdArnoldDelegateRenderProduct product;
798798
const auto* productType = TfMapLookupPtr(productIter, _tokens->productType);
799799

libs/render_delegate/utils.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,18 @@ inline size_t _ExtrapolatePositions(
8989
}
9090

9191
// Check if primvars or positions exists. These arrays are COW.
92-
VtVec3fArray velocities;
93-
VtVec3fArray accelerations;
92+
VtVec3fArray emptyVelocities;
93+
VtVec3fArray emptyAccelerations;
9494
auto primvarIt = primvars->find(HdTokens->velocities);
95-
if (primvarIt != primvars->end() && primvarIt->second.value.IsHolding<VtVec3fArray>()) {
96-
velocities = primvarIt->second.value.UncheckedGet<VtVec3fArray>();
97-
}
95+
const VtVec3fArray& velocities = (primvarIt != primvars->end() && primvarIt->second.value.IsHolding<VtVec3fArray>())
96+
? primvarIt->second.value.UncheckedGet<VtVec3fArray>()
97+
: emptyVelocities;
98+
9899
primvarIt = primvars->find(HdTokens->accelerations);
99-
if (primvarIt != primvars->end() && primvarIt->second.value.IsHolding<VtVec3fArray>()) {
100-
accelerations = primvarIt->second.value.UncheckedGet<VtVec3fArray>();
101-
}
100+
const VtVec3fArray& accelerations =
101+
(primvarIt != primvars->end() && primvarIt->second.value.IsHolding<VtVec3fArray>())
102+
? primvarIt->second.value.UncheckedGet<VtVec3fArray>()
103+
: emptyAccelerations;
102104

103105
// The positions in xf contain several several time samples, but the amount of vertices
104106
// can change in each sample. We want to consider the positions at the proper time, so

libs/translator/reader/read_geometry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ static bool CopyArrayElement(VtValue &value, unsigned int index)
13161316
if (!value.IsHolding<VtArray<T>>())
13171317
return false;
13181318

1319-
VtArray<T> array = value.UncheckedGet<VtArray<T>>();
1319+
const VtArray<T> &array = value.UncheckedGet<VtArray<T>>();
13201320
if (index < array.size()) {
13211321
value = array[index];
13221322
}

plugins/ndr/parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void _ReadShaderAttribute(const UsdAttribute &attr, NdrPropertyUniquePtrVec &pro
145145
NdrOptionVec options;
146146
auto it = customData.find(_tokens->enumValues);
147147
if (it != customData.end()) {
148-
VtStringArray enumValues = it->second.Get<VtStringArray>();
148+
const VtStringArray &enumValues = it->second.Get<VtStringArray>();
149149
for (const auto &enumValue : enumValues) {
150150
TfToken enumValTf(enumValue.c_str());
151151
options.emplace_back(enumValTf, enumValTf);
@@ -285,7 +285,7 @@ NdrNodeUniquePtr NdrArnoldParserPlugin::Parse(const NdrNodeDiscoveryResult& disc
285285
// we now create them in the same order as they appeared in the AtParamIterator in _ReadArnoldShaderDef
286286
if (primCustomData.find(_tokens->attrsOrder) != primCustomData.end()) {
287287
VtValue attrsOrderVal = primCustomData[_tokens->attrsOrder];
288-
VtArray<std::string> attrsOrder = attrsOrderVal.Get<VtArray<std::string>>();
288+
const VtArray<std::string> &attrsOrder = attrsOrderVal.Get<VtArray<std::string>>();
289289
for (const auto &attrName: attrsOrder) {
290290
if (declaredAttributes.find(attrName) != declaredAttributes.end())
291291
continue;

0 commit comments

Comments
 (0)