Skip to content

Commit

Permalink
Attempting to see if adding a cache will speed up the sdf::Root
Browse files Browse the repository at this point in the history
construction

I'm just checking if adding a cache makes any sense.

Signed-off-by: Arjo Chakravarty <arjoc@intrinsic.ai>
  • Loading branch information
arjo129 committed Sep 6, 2024
1 parent c2c4f91 commit 0868abf
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
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),
"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

0 comments on commit 0868abf

Please sign in to comment.