Skip to content

Commit

Permalink
Improve OMEdit instance API performance (#10743)
Browse files Browse the repository at this point in the history
- Return expressions, annotations and other things by reference/pointer
  rather than by value where possible, to avoid excessive copying of
  Expression:s.
- Only create Annotation:s for elements that actually have annotations,
  and instead return a predefined default annotation from the
  corresponding `getAnnotation` methods for elements that don't have an
  annotation.
- Move the common parts of the JSON deserialization for Element:s to
  `Element::deserialize` and call a virtual method for the rest, to
  reduce code duplication and avoid having derived classes worry about
  allocating the annotation when needed.
  • Loading branch information
perost committed May 25, 2023
1 parent 87b14c1 commit c4f7afe
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 209 deletions.
5 changes: 3 additions & 2 deletions OMEdit/OMEditLIB/Annotations/BooleanAnnotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
*/
#include "BooleanAnnotation.h"

BooleanAnnotation::BooleanAnnotation()
BooleanAnnotation::BooleanAnnotation(bool value)
: mValue(value)
{
clear();
setExp();
}

void BooleanAnnotation::clear()
Expand Down
3 changes: 2 additions & 1 deletion OMEdit/OMEditLIB/Annotations/BooleanAnnotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
class BooleanAnnotation : public DynamicAnnotation
{
public:
BooleanAnnotation();
BooleanAnnotation() = default;
BooleanAnnotation(bool value);

void clear() override;

Expand Down
8 changes: 4 additions & 4 deletions OMEdit/OMEditLIB/Annotations/DynamicAnnotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ void DynamicAnnotation::evaluate(ModelInstance::Model *pModel)
}
if (!expression.isNull()) {
try {
fromExp(expression.evaluate([&] (std::string name) {
fromExp(expression.evaluate([&] (std::string name) -> auto& {
auto vname = QString::fromStdString(name);
// the instance api returns the qualified cref
vname = StringHandler::getLastWordAfterDot(vname);
FlatModelica::Expression exp = pModel->getVariableBinding(vname);
if (exp.isNull()) {
auto exp = pModel->getVariableBinding(vname);
if (!exp) {
throw std::runtime_error(name + " could not be found");
}
return exp;
return *exp;
}));
} catch (const std::exception &e) {
qDebug() << "Failed to evaluate expression.";
Expand Down
5 changes: 3 additions & 2 deletions OMEdit/OMEditLIB/Annotations/RealAnnotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
*/
#include "RealAnnotation.h"

RealAnnotation::RealAnnotation()
RealAnnotation::RealAnnotation(qreal value)
: mValue(value)
{
clear();
setExp();
}

void RealAnnotation::clear()
Expand Down
3 changes: 2 additions & 1 deletion OMEdit/OMEditLIB/Annotations/RealAnnotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
class RealAnnotation : public DynamicAnnotation
{
public:
RealAnnotation();
RealAnnotation() = default;
RealAnnotation(qreal value);

void clear() override;

Expand Down
14 changes: 7 additions & 7 deletions OMEdit/OMEditLIB/Annotations/ShapeAnnotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class FilledShape
void setLinePattern(StringHandler::LinePattern pattern) {mLinePattern = pattern;}
StringHandler::LinePattern getLinePattern() {return mLinePattern;}
void setFillPattern(StringHandler::FillPattern pattern) {mFillPattern = pattern;}
FillPatternAnnotation getFillPattern() {return mFillPattern;}
const FillPatternAnnotation &getFillPattern() {return mFillPattern;}
void setLineThickness(qreal thickness) {mLineThickness = thickness;}
qreal getLineThickness() {return mLineThickness;}
protected:
Expand Down Expand Up @@ -165,8 +165,8 @@ class ShapeAnnotation : public QObject, public QGraphicsItem, public GraphicItem
GraphicsView* getGraphicsView() {return mpGraphicsView;}
OriginItem* getOriginItem() {return mpOriginItem;}
void setPoints(QVector<QPointF> points) {mPoints = points;}
PointArrayAnnotation getPoints() {return mPoints;}
ArrowAnnotation getArrow() {return mArrow;}
const PointArrayAnnotation &getPoints() {return mPoints;}
const ArrowAnnotation &getArrow() {return mArrow;}
void setStartArrow(StringHandler::Arrow startArrow) {mArrow.replace(0, startArrow);}
StringHandler::Arrow getStartArrow() {return mArrow.at(0);}
void setEndArrow(StringHandler::Arrow endArrow) {mArrow.replace(1, endArrow);}
Expand All @@ -176,7 +176,7 @@ class ShapeAnnotation : public QObject, public QGraphicsItem, public GraphicItem
void setSmooth(StringHandler::Smooth smooth) {mSmooth = smooth;}
StringHandler::Smooth getSmooth() {return mSmooth;}
void setExtents(QVector<QPointF> extents) {mExtent = extents;}
QVector<QPointF> getExtents() {return mExtent;}
const QVector<QPointF> &getExtents() {return mExtent;}
void setBorderPattern(StringHandler::BorderPattern pattern) {mBorderPattern = pattern;}
StringHandler::BorderPattern getBorderPattern() {return mBorderPattern;}
void setRadius(qreal radius) {mRadius = radius;}
Expand All @@ -188,13 +188,13 @@ class ShapeAnnotation : public QObject, public QGraphicsItem, public GraphicItem
void setClosure(StringHandler::EllipseClosure closure) {mClosure = closure;}
StringHandler::EllipseClosure getClosure() {return mClosure;}
void setTextString(QString textString);
QString getTextString() {return mTextString;}
const QString &getTextString() {return mTextString;}
void setFontName(QString fontName) {mFontName = fontName;}
QString getFontName() {return mFontName;}
const QString &getFontName() {return mFontName;}
void setFontSize(qreal fontSize) {mFontSize = fontSize;}
qreal getFontSize() {return mFontSize;}
void setTextStyles(QVector<StringHandler::TextStyle> textStyles) {mTextStyles = textStyles;}
QVector<StringHandler::TextStyle> getTextStyles() {return mTextStyles;}
const QVector<StringHandler::TextStyle> &getTextStyles() {return mTextStyles;}
void setTextHorizontalAlignment(StringHandler::TextAlignment textAlignment) {mHorizontalAlignment = textAlignment;}
StringHandler::TextAlignment getTextHorizontalAlignment() {return mHorizontalAlignment;}
void setFileName(QString fileName);
Expand Down
5 changes: 3 additions & 2 deletions OMEdit/OMEditLIB/Annotations/StringAnnotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
*/
#include "StringAnnotation.h"

StringAnnotation::StringAnnotation()
StringAnnotation::StringAnnotation(const QString &str)
: mValue(str)
{
clear();
setExp();
}

void StringAnnotation::clear()
Expand Down
3 changes: 2 additions & 1 deletion OMEdit/OMEditLIB/Annotations/StringAnnotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
class StringAnnotation : public DynamicAnnotation
{
public:
StringAnnotation();
StringAnnotation() = default;
StringAnnotation(const QString &str);

void clear() override;

Expand Down
3 changes: 2 additions & 1 deletion OMEdit/OMEditLIB/Element/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3015,7 +3015,8 @@ bool Element::checkEnumerationDisplayString(QString &displayString, const QStrin
void Element::updateToolTip()
{
if (mpGraphicsView->getModelWidget()->isNewApi()) {
QString comment = mpModelComponent->getComment().replace("\\\"", "\"");
QString comment = mpModelComponent->getComment();
comment.replace("\\\"", "\"");
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
comment = pOMCProxy->makeDocumentationUriToFileName(comment);
// since tooltips can't handle file:// scheme so we have to remove it in order to display images and make links work.
Expand Down
6 changes: 3 additions & 3 deletions OMEdit/OMEditLIB/Element/ElementProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,18 @@ Parameter::Parameter(ModelInstance::Element *pElement, ElementParameters *pEleme
mpElement = 0;
mpModelInstanceElement = pElement;
mpElementParameters = pElementParameters;
const ModelInstance::DialogAnnotation dialogAnnotation = mpModelInstanceElement->getAnnotation()->getDialogAnnotation();
auto &dialogAnnotation = mpModelInstanceElement->getAnnotation()->getDialogAnnotation();
mTab = dialogAnnotation.getTab();
mGroup = dialogAnnotation.getGroup();
mGroupDefined = !mGroup.isEmpty();
mEnable = dialogAnnotation.isEnabled();
mShowStartAttribute = dialogAnnotation.getShowStartAttribute();
mShowStartAndFixed = mShowStartAttribute;
mColorSelector = dialogAnnotation.isColorSelector();
const ModelInstance::Selector loadSelector = dialogAnnotation.getLoadSelector();
auto &loadSelector = dialogAnnotation.getLoadSelector();
mLoadSelectorFilter = loadSelector.getFilter();
mLoadSelectorCaption = loadSelector.getCaption();
const ModelInstance::Selector saveSelector = dialogAnnotation.getSaveSelector();
auto &saveSelector = dialogAnnotation.getSaveSelector();
mSaveSelectorFilter = saveSelector.getFilter();
mSaveSelectorCaption = saveSelector.getCaption();
mGroupImage = dialogAnnotation.getGroupImage();
Expand Down
6 changes: 3 additions & 3 deletions OMEdit/OMEditLIB/Element/ElementProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ class Parameter : public QObject
ModelInstance::Element* getModelInstanceElement() {return mpModelInstanceElement;}
bool isParameter() const;
void setTab(QString tab) {mTab = tab;}
StringAnnotation getTab() {return mTab;}
const StringAnnotation &getTab() {return mTab;}
void setGroup(QString group) {mGroup = group;}
StringAnnotation getGroup() {return mGroup;}
const StringAnnotation &getGroup() {return mGroup;}
void setGroupDefined(bool groupDefined) {mGroupDefined = groupDefined;}
bool isGroupDefined() const {return mGroupDefined;}
void setShowStartAttribute(bool showStartAttribute) {mShowStartAttribute = showStartAttribute;}
bool isShowStartAttribute() const {return mShowStartAttribute;}
void setShowStartAndFixed(bool showStartAndFixed) {mShowStartAndFixed = showStartAndFixed;}
bool isShowStartAndFixed() const {return mShowStartAndFixed;}
StringAnnotation getGroupImage() const {return mGroupImage;}
const StringAnnotation &getGroupImage() const {return mGroupImage;}
void updateNameLabel();
Label* getNameLabel() {return mpNameLabel;}
FixedCheckBox* getFixedCheckBox() {return mpFixedCheckBox;}
Expand Down
16 changes: 8 additions & 8 deletions OMEdit/OMEditLIB/Element/Transformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class Transformation
bool isValid() const {return mValid;}
void setWidth(const qreal &width) {mWidth = width;}
void setHeight(const qreal &height) {mHeight = height;}
BooleanAnnotation getVisible() const {return mVisible;}
BooleanAnnotation getVisibleIcon() const {return mVisibleIcon;}
const BooleanAnnotation& getVisible() const {return mVisible;}
const BooleanAnnotation& getVisibleIcon() const {return mVisibleIcon;}
void adjustPosition(qreal x, qreal y);
void setOrigin(QPointF origin);
PointAnnotation getOrigin() const;
Expand Down Expand Up @@ -93,20 +93,20 @@ class Transformation
qreal getHeight() const {return mHeight;}
void adjustPositionDiagram(qreal x, qreal y);
void setOriginDiagram(QPointF origin);
PointAnnotation getOriginDiagram() const {return mOriginDiagram;}
const PointAnnotation &getOriginDiagram() const {return mOriginDiagram;}
void setExtentDiagram(QVector<QPointF> extent) {mExtentDiagram = extent;}
ExtentAnnotation getExtentDiagram() const {return mExtentDiagram;}
const ExtentAnnotation &getExtentDiagram() const {return mExtentDiagram;}
void setRotateAngleDiagram(qreal rotateAngle) {mRotateAngleDiagram = rotateAngle;}
RealAnnotation getRotateAngleDiagram() const {return mRotateAngleDiagram;}
const RealAnnotation &getRotateAngleDiagram() const {return mRotateAngleDiagram;}
QPointF getPositionDiagram() const;
QTransform getTransformationMatrixIcon();
void adjustPositionIcon(qreal x, qreal y);
void setOriginIcon(QPointF origin);
PointAnnotation getOriginIcon() const {return mOriginIcon;}
const PointAnnotation &getOriginIcon() const {return mOriginIcon;}
void setExtentIcon(QVector<QPointF> extent) {mExtentIcon = extent;}
ExtentAnnotation getExtentIcon() const {return mExtentIcon;}
const ExtentAnnotation &getExtentIcon() const {return mExtentIcon;}
void setRotateAngleIcon(qreal rotateAngle) {mRotateAngleIcon = rotateAngle;}
RealAnnotation getRotateAngleIcon() const {return mRotateAngleIcon;}
const RealAnnotation &getRotateAngleIcon() const {return mRotateAngleIcon;}
QPointF getPositionIcon() const;
};

Expand Down
9 changes: 5 additions & 4 deletions OMEdit/OMEditLIB/Modeling/CoOrdinateSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ class CoOrdinateSystem
CoOrdinateSystem();
CoOrdinateSystem(const CoOrdinateSystem &coOrdinateSystem);
void setExtent(const QVector<QPointF> extent);
ExtentAnnotation getExtent() const {return mExtent;}
const ExtentAnnotation &getExtent() const {return mExtent;}
bool hasExtent() const {return mHasExtent;}
void setHasExtent(const bool hasExtent) {mHasExtent = hasExtent;}
void setPreserveAspectRatio(const bool preserveAspectRatio);
BooleanAnnotation getPreserveAspectRatio() const {return mPreserveAspectRatio;}
const BooleanAnnotation &getPreserveAspectRatio() const {return mPreserveAspectRatio;}
bool hasPreserveAspectRatio() const {return mHasPreserveAspectRatio;}
void setHasPreserveAspectRatio(const bool hasPreserveAspectRatio) {mHasPreserveAspectRatio = hasPreserveAspectRatio;}
void setInitialScale(const qreal initialScale);
RealAnnotation getInitialScale() const {return mInitialScale;}
const RealAnnotation &getInitialScale() const {return mInitialScale;}
bool hasInitialScale() const {return mHasInitialScale;}
void setHasInitialScale(const bool hasInitialScale) {mHasInitialScale = hasInitialScale;}
qreal getHorizontalGridStep();
qreal getVerticalGridStep();
void setGrid(const QPointF grid);
PointAnnotation getGrid() const {return mGrid;}
const PointAnnotation &getGrid() const {return mGrid;}
void setHasGrid(const bool hasGrid) {mHasGrid = hasGrid;}
bool hasGrid() const {return mHasGrid;}

Expand All @@ -71,6 +71,7 @@ class CoOrdinateSystem
bool isComplete() const;

CoOrdinateSystem& operator=(const CoOrdinateSystem &coOrdinateSystem) = default;
CoOrdinateSystem& operator=(CoOrdinateSystem &&coOrdinateSystem) = default;
private:
ExtentAnnotation mExtent;
bool mHasExtent;
Expand Down
14 changes: 7 additions & 7 deletions OMEdit/OMEditLIB/Modeling/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,15 @@ void AddComponentCommand::undo()
mpDiagramGraphicsView->deleteElementFromClass(mpDiagramComponent);
}

UpdateComponentTransformationsCommand::UpdateComponentTransformationsCommand(Element *pComponent, const Transformation &oldTransformation, const Transformation &newTransformation,
UpdateComponentTransformationsCommand::UpdateComponentTransformationsCommand(Element *pComponent, Transformation oldTransformation, Transformation newTransformation,
const bool positionChanged, const bool moveConnectorsTogether, UndoCommand *pParent)
: UndoCommand(pParent)
: UndoCommand(pParent),
mpComponent(pComponent),
mOldTransformation(std::move(oldTransformation)),
mNewTransformation(std::move(newTransformation)),
mPositionChanged(positionChanged),
mMoveConnectorsTogether(moveConnectorsTogether)
{
mpComponent = pComponent;
mOldTransformation = oldTransformation;
mNewTransformation = newTransformation;
mPositionChanged = positionChanged;
mMoveConnectorsTogether = moveConnectorsTogether;
setText(QString("Update Component %1 Transformations").arg(mpComponent->getName()));
}

Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Modeling/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class AddComponentCommand : public UndoCommand
class UpdateComponentTransformationsCommand : public UndoCommand
{
public:
UpdateComponentTransformationsCommand(Element *pComponent, const Transformation &oldTransformation, const Transformation &newTransformation,
UpdateComponentTransformationsCommand(Element *pComponent, Transformation oldTransformation, Transformation newTransformation,
const bool positionChanged, const bool moveConnectorsTogether, UndoCommand *pParent = 0);
void redoInternal();
void undo();
Expand Down

0 comments on commit c4f7afe

Please sign in to comment.