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

0033670: Coding - TopExp_Explorer performance update #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 10 additions & 24 deletions src/TopExp/TopExp_Explorer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TopExp_Explorer::TopExp_Explorer()
toAvoid (TopAbs_SHAPE),
hasMore (Standard_False)
{
myStack = (TopoDS_Iterator*)Standard::Allocate(theStackSize*sizeof(TopoDS_Iterator));
myStack = (TopoDS_Iterator*)Standard::AllocateOptimal(theStackSize*sizeof(TopoDS_Iterator));
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider using containers for safer memory management of myStack

The code manually manages memory for myStack using Standard::AllocateOptimal, placement new, and explicit destructor calls. This approach is error-prone and can lead to memory leaks or undefined behavior if not handled meticulously.

Consider using a container class like NCollection_Vector<TopoDS_Iterator> or std::vector<TopoDS_Iterator> to manage myStack. Containers handle memory allocation, deallocation, and object lifetimes automatically, reducing complexity and potential errors.

Also applies to: 63-63

}

//=======================================================================
Expand All @@ -60,7 +60,7 @@ TopExp_Explorer::TopExp_Explorer (const TopoDS_Shape& theS,
toAvoid (theToAvoid),
hasMore (Standard_False)
{
myStack = (TopoDS_Iterator*)Standard::Allocate(theStackSize*sizeof(TopoDS_Iterator));
myStack = (TopoDS_Iterator*)Standard::AllocateOptimal(theStackSize*sizeof(TopoDS_Iterator));

Init (theS, theToFind, theToAvoid);
}
Expand Down Expand Up @@ -138,7 +138,7 @@ const TopoDS_Shape& TopExp_Explorer::Current()const
void TopExp_Explorer::Next()
{
Standard_Integer NewSize;
TopoDS_Shape ShapTop;
TopoDS_Shape const* ShapTop = nullptr;
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Potential Dangling Pointer with ShapTop

Changing ShapTop to a pointer may lead to dangling pointers if myStack is reallocated, as reallocation can move the memory location of myStack. Ensure that myStack's memory does not change during the lifetime of ShapTop or consider alternative approaches.

🔗 Analysis chain

Verify the safety of changing ShapTop to a pointer

Changing ShapTop to a const TopoDS_Shape* avoids unnecessary copies of TopoDS_Shape. However, ensure that the pointer remains valid throughout its usage to prevent dangling pointers.

Run the following script to check all assignments to ShapTop and ensure it doesn't reference a temporary object:

Also applies to: 173-174, 185-185

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `ShapTop` does not point to temporary objects.

# Search for all assignments to `ShapTop`.
rg --type cpp --line-number --fixed-strings 'ShapTop = ' src/TopExp/TopExp_Explorer.cxx

# Search for usages of `ShapTop` after potential invalidation points.
rg --type cpp --line-number --fixed-strings 'ShapTop->' src/TopExp/TopExp_Explorer.cxx

Length of output: 469


Script:

#!/bin/bash
# Description: Corrected script to verify that `ShapTop` does not point to temporary objects.

# Search for all assignments to `ShapTop`.
rg --type cpp --line-number --fixed-strings 'ShapTop = ' src/TopExp/TopExp_Explorer.cxx

# Search for usages of `ShapTop` after potential invalidation points.
rg --type cpp --line-number --fixed-strings 'ShapTop->' src/TopExp/TopExp_Explorer.cxx

Length of output: 305


Script:

#!/bin/bash
# Description: Locate the declaration and all usages of `myStack` to verify its lifetime.

# Find the declaration of `myStack`.
rg --type cpp --line-number 'myStack' src/TopExp/TopExp_Explorer.cxx

# Find all usages of `myStack`.
rg --type cpp --line-number 'myStack' src/TopExp/TopExp_Explorer.cxx

Length of output: 2023

TopAbs_ShapeEnum ty;
Standard_NoMoreObject_Raise_if(!hasMore,"TopExp_Explorer::Next");

Expand All @@ -159,16 +159,9 @@ void TopExp_Explorer::Next()
else {
// push and try to find
if(++myTop >= mySizeOfStack) {
NewSize = mySizeOfStack + theStackSize;
TopExp_Stack newStack = (TopoDS_Iterator*)Standard::Allocate(NewSize*sizeof(TopoDS_Iterator));
Standard_Integer i;
for ( i =0; i < myTop; i++) {
new (&newStack[i]) TopoDS_Iterator(myStack[i]);
myStack[i].~TopoDS_Iterator();
}
Standard::Free(myStack);
mySizeOfStack = NewSize;
myStack = newStack;
NewSize = mySizeOfStack + theStackSize;
myStack = (TopoDS_Iterator*)Standard::Reallocate(myStack, NewSize * sizeof(TopoDS_Iterator));
mySizeOfStack = NewSize;
Comment on lines +162 to +164
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Potential issues with reallocation of myStack and object lifetimes

When reallocating myStack using Standard::Reallocate, the existing TopoDS_Iterator objects are not properly constructed in the new memory space. This may lead to undefined behavior since only raw memory is reallocated without invoking copy constructors or moving the objects.

Ensure that existing objects are correctly copied or moved to the new memory location after reallocation. Alternatively, using a container like NCollection_Vector<TopoDS_Iterator> or std::vector<TopoDS_Iterator> can handle this safely.

Also applies to: 182-182

}
new (&myStack[myTop]) TopoDS_Iterator(myShape);
}
Expand All @@ -177,26 +170,19 @@ void TopExp_Explorer::Next()

for (;;) {
if (myStack[myTop].More()) {
ShapTop = myStack[myTop].Value();
ty = ShapTop.ShapeType();
ShapTop = &myStack[myTop].Value();
ty = ShapTop->ShapeType();
if (SAMETYPE(toFind,ty)) {
hasMore = Standard_True;
return;
}
else if (LESSCOMPLEX(toFind,ty) && !AVOID(toAvoid,ty)) {
if(++myTop >= mySizeOfStack) {
NewSize = mySizeOfStack + theStackSize;
TopExp_Stack newStack = (TopoDS_Iterator*)Standard::Allocate(NewSize*sizeof(TopoDS_Iterator));
Standard_Integer i;
for (i =0; i < myTop; i++) {
new (&newStack[i]) TopoDS_Iterator(myStack[i]);
myStack[i].~TopoDS_Iterator();
}
Standard::Free(myStack);
myStack = (TopoDS_Iterator*)Standard::Reallocate(myStack, NewSize * sizeof(TopoDS_Iterator));
mySizeOfStack = NewSize;
myStack = newStack;
}
new (&myStack[myTop]) TopoDS_Iterator(ShapTop);
new (&myStack[myTop]) TopoDS_Iterator(*ShapTop);
}
else {
myStack[myTop].Next();
Expand Down
Loading