Skip to content

Commit

Permalink
Merge pull request #3294 from nvmkuruc/reduce_parser_copies
Browse files Browse the repository at this point in the history
Reduce copies of string values during text parsing

(Internal change: 2341140)
  • Loading branch information
pixar-oss committed Sep 17, 2024
2 parents 2bcdfba + 3de0b71 commit 673e543
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions pxr/usd/sdf/textFileFormatParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,20 @@ struct TextParserAction<String>
// obtain the text inside of the quotes
// we have to first check for multi-line quotes
// so we know what to pass to Sdf_EvalQuotedString
std::string inputString = in.string();
std::string evaluatedString;
if (TfStringStartsWith(inputString, "'''") ||
TfStringStartsWith(inputString, "\"\"\""))
const std::string_view inputStrView = in.string_view();
// firstThree will be clipped to inputStrView's size so no bound checks
// are needed.
if(const auto firstThree = inputStrView.substr(0, 3);
firstThree == "\"\"\"" || firstThree == "'''")
{
evaluatedString = Sdf_EvalQuotedString(
inputString.c_str(), inputString.length(), 3, nullptr);
inputStrView.data(), inputStrView.length(), 3, nullptr);
}
else
{
evaluatedString = Sdf_EvalQuotedString(
inputString.c_str(), inputString.length(), 1, nullptr);
inputStrView.data(), inputStrView.length(), 1, nullptr);
}

if (context.parsingContext.back() ==
Expand All @@ -319,15 +321,15 @@ struct TextParserAction<String>
context.data->Set(
context.path,
SdfFieldKeys->Documentation,
VtValue(evaluatedString));
VtValue::Take(evaluatedString));
}
else if (context.parsingContext.back() ==
Sdf_TextParserCurrentParsingContext::Metadata)
{
context.data->Set(
context.path,
SdfFieldKeys->Comment,
VtValue(evaluatedString));
VtValue::Take(evaluatedString));
}
else if (context.parsingContext.back() ==
Sdf_TextParserCurrentParsingContext::PrimSpec)
Expand All @@ -353,7 +355,7 @@ struct TextParserAction<String>
context.parsingContext.back() ==
Sdf_TextParserCurrentParsingContext::VariantSetsMetadata)
{
context.nameVector.emplace_back(evaluatedString);
context.nameVector.emplace_back(std::move(evaluatedString));
}
else if (context.parsingContext.back() ==
Sdf_TextParserCurrentParsingContext::VariantSetStatement)
Expand All @@ -372,10 +374,11 @@ struct TextParserAction<String>
throw PEGTL_NS::parse_error(allow.GetWhyNot(), in);
}

context.currentVariantSetNames.push_back(evaluatedString);
context.currentVariantSetNames.push_back(
std::move(evaluatedString));
context.currentVariantNames.emplace_back();
context.path = context.path.AppendVariantSelection(
evaluatedString, "");
context.currentVariantSetNames.back(), "");
}
else if (context.parsingContext.back() ==
Sdf_TextParserCurrentParsingContext::VariantStatementList)
Expand Down Expand Up @@ -1073,11 +1076,11 @@ struct TextParserAction<AssetRef>
context.parsingContext.back() ==
Sdf_TextParserCurrentParsingContext::PayloadListOpMetadata)
{
std::string inputString = in.string();
bool isTripleDelimited = TfStringStartsWith(inputString, "@@@");
const std::string_view inputStrView = in.string_view();
const bool isTripleDelimited = (inputStrView.substr(0, 3) == "@@@");
std::string evaluatedAssetPath = Sdf_EvalAssetPath(
inputString.c_str(),
inputString.length(),
inputStrView.data(),
inputStrView.length(),
isTripleDelimited);

if (evaluatedAssetPath.empty())
Expand All @@ -1095,21 +1098,21 @@ struct TextParserAction<AssetRef>
throw PEGTL_NS::parse_error(errorMessage, in);
}

context.layerRefPath = evaluatedAssetPath;
context.layerRefPath = std::move(evaluatedAssetPath);
context.layerRefOffset = SdfLayerOffset();
context.savedPath = SdfPath::EmptyPath();
}
else if(context.parsingContext.back() ==
Sdf_TextParserCurrentParsingContext::SubLayerMetadata)
{
std::string inputString = in.string();
bool isTripleDelimited = TfStringStartsWith(inputString, "@@@");
const std::string_view inputStrView = in.string_view();
const bool isTripleDelimited = (inputStrView.substr(0, 3) == "@@@");
std::string evaluatedAssetPath = Sdf_EvalAssetPath(
inputString.c_str(),
inputString.length(),
inputStrView.data(),
inputStrView.length(),
isTripleDelimited);

context.layerRefPath = evaluatedAssetPath;
context.layerRefPath = std::move(evaluatedAssetPath);
context.layerRefOffset = SdfLayerOffset();
}
}
Expand Down Expand Up @@ -1149,17 +1152,19 @@ struct TextParserAction<StringValue>
template <class Input>
static void apply(const Input& in, Sdf_TextParserContext& context)
{
std::string inputString = in.string();
const std::string_view inputStrView = in.string_view();
size_t numDelimeters = 1;
if(TfStringStartsWith(inputString, "\"\"\"") ||
TfStringStartsWith(inputString, "'''"))
// firstThree will be clipped to inputStrView's size so no bound checks
// are needed.
if(const auto firstThree = inputStrView.substr(0, 3);
firstThree == "\"\"\"" || firstThree == "'''")
{
numDelimeters = 3;
}

std::string evaluatedString = Sdf_EvalQuotedString(
inputString.c_str(),
inputString.length(),
inputStrView.data(),
inputStrView.length(),
numDelimeters);

TF_DEBUG(SDF_TEXT_FILE_FORMAT_CONTEXT).Msg(
Expand All @@ -1178,11 +1183,11 @@ struct TextParserAction<AssetRefValue>
// the ParserValueContext needs asset paths to be stored
// as SdfAssetPath instead of std::string to be able to
// distinguish between them
std::string inputString = in.string();
bool isTripleDelimited = TfStringStartsWith(inputString, "@@@");
const std::string_view inputStrView = in.string_view();
const bool isTripleDelimited = (inputStrView.substr(0, 3) == "@@@");
std::string evaluatedAssetPath = Sdf_EvalAssetPath(
inputString.c_str(),
inputString.length(),
inputStrView.data(),
inputStrView.length(),
isTripleDelimited);

context.values.AppendValue(SdfAssetPath(evaluatedAssetPath));
Expand Down

0 comments on commit 673e543

Please sign in to comment.