-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
} | ||
|
||
//======================================================================= | ||
|
@@ -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); | ||
} | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Potential Dangling Pointer with Changing 🔗 Analysis chainVerify the safety of changing Changing Run the following script to check all assignments to Also applies to: 173-174, 185-185 🏁 Scripts executedThe 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"); | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential issues with reallocation of When reallocating Ensure that existing objects are correctly copied or moved to the new memory location after reallocation. Alternatively, using a container like Also applies to: 182-182 |
||
} | ||
new (&myStack[myTop]) TopoDS_Iterator(myShape); | ||
} | ||
|
@@ -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(); | ||
|
There was a problem hiding this comment.
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
usingStandard::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>
orstd::vector<TopoDS_Iterator>
to managemyStack
. Containers handle memory allocation, deallocation, and object lifetimes automatically, reducing complexity and potential errors.Also applies to: 63-63