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

[Do Not merge] Attempting to see if adding a cache will speed up the sdf::Root construction #1479

Draft
wants to merge 1 commit into
base: sdf15
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/sdf/Param.hh
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,15 @@ namespace sdf
public: bool SetParentElement(ElementPtr _parentElement,
sdf::Errors &_errors);

/// \brief Set the parent Element of this Param.
/// \param[in] _parentElement Pointer to new parent Element. A nullptr can
/// be provided to remove the current parent Element.
/// \param[out] _errors Vector of errors.
/// \return True if the parent Element was set and the value was reparsed
/// successfully.
public: bool SetParentElementNoReparse(
ElementPtr _parentElement);

/// \brief Reset the parameter to the default value.
public: void Reset();

Expand Down
3 changes: 2 additions & 1 deletion include/sdf/SDFImpl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ namespace sdf
class SDFORMAT_VISIBLE SDF
{
public: SDF();
public: SDF(const SDF& other);//Copy constructor
/// \brief Destructor
public: ~SDF();
public: virtual ~SDF();
public: void PrintDescription();
public: void PrintDescription(sdf::Errors &_errors);
public: void PrintDoc();
Expand Down
4 changes: 2 additions & 2 deletions src/Element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ ElementPtr Element::Clone(sdf::Errors &_errors) const
aiter != this->dataPtr->attributes.end(); ++aiter)
{
auto clonedAttribute = (*aiter)->Clone();
SDF_ASSERT(clonedAttribute->SetParentElement(clone),
SDF_ASSERT(clonedAttribute->SetParentElementNoReparse(clone),
"Cannot set parent Element of cloned attribute Param to cloned "
"Element.");
clone->dataPtr->attributes.push_back(clonedAttribute);
Expand All @@ -279,7 +279,7 @@ ElementPtr Element::Clone(sdf::Errors &_errors) const
if (this->dataPtr->value)
{
clone->dataPtr->value = this->dataPtr->value->Clone();
SDF_ASSERT(clone->dataPtr->value->SetParentElement(clone),
SDF_ASSERT(clone->dataPtr->value->SetParentElementNoReparse(clone),
Copy link
Member

Choose a reason for hiding this comment

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

with the context that Param::Reparse was added in #589 to support parsing pose values based on the parent element's @degrees attribute value, I think it may be fine to clone Param values without reparsing.

"Cannot set parent Element of cloned value Param to cloned Element.");
}

Expand Down
7 changes: 7 additions & 0 deletions src/Param.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,13 @@ bool Param::SetParentElement(ElementPtr _parentElement, sdf::Errors &_errors)
return true;
}

//////////////////////////////////////////////////
bool Param::SetParentElementNoReparse(ElementPtr _parentElement)
{
this->dataPtr->parentElement = _parentElement;
return true;
}

//////////////////////////////////////////////////
void Param::Reset()
{
Expand Down
11 changes: 9 additions & 2 deletions src/Root.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,19 @@ Errors Root::LoadSdfString(const std::string &_sdf)
return this->LoadSdfString(_sdf, ParserConfig::GlobalConfig());
}

static SDFPtr cachedRoot;
static bool cacheActive = false;

/////////////////////////////////////////////////
Errors Root::LoadSdfString(const std::string &_sdf, const ParserConfig &_config)
{
Errors errors;
SDFPtr sdfParsed(new SDF());
init(sdfParsed);
if (!cacheActive) {
cacheActive = true;
cachedRoot = std::make_shared<SDF>();
init(cachedRoot);
}
SDFPtr sdfParsed = std::make_shared<SDF>(*cachedRoot);

// Read an SDF string, and store the result in sdfParsed.
if (!readString(_sdf, _config, sdfParsed, errors))
Expand Down
5 changes: 4 additions & 1 deletion src/SDF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ SDF::SDF()
: dataPtr(new SDFPrivate)
{
}

SDF::SDF(const SDF& other)
{
this->dataPtr = std::make_unique<SDFPrivate>(*other.dataPtr);
}
/////////////////////////////////////////////////
SDF::~SDF()
{
Expand Down
9 changes: 8 additions & 1 deletion src/SDFImplPrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ namespace sdf
{
public: SDFPrivate() : root(new Element)
{
};
}

public: SDFPrivate(const SDFPrivate& other)
{
this->path = other.path;
this->originalVersion = other.originalVersion;
this->root = other.root->Clone();
}

/// \brief Store the root element.
/// \sa ElementPtr Root()
Expand Down
Loading