-
-
Notifications
You must be signed in to change notification settings - Fork 22
feat: optimize evaluator control flow and data handling for scalars #1545
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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -169,7 +169,12 @@ MacroFunctionDef::evaluateMFunction(Evaluator* eval, const ArrayOfVector& inputs | |||||
|
|
||||||
| uint64 tic = 0; | ||||||
| try { | ||||||
| insertLocalFunctions(context); | ||||||
| // Only insert local functions on first entry (they don't change during recursion). | ||||||
| if (recursionDepth == 1) { | ||||||
| insertLocalFunctions(context); | ||||||
| } else if (this->nextFunction != nullptr || this->prevFunction != nullptr) { | ||||||
| insertLocalFunctions(context); | ||||||
| } | ||||||
| setInputArgumentNames(context, inputs); | ||||||
| bindInputs(context, inputs); | ||||||
| context->getCurrentScope()->setNargOut(nargout); | ||||||
|
|
@@ -202,12 +207,15 @@ MacroFunctionDef::evaluateMFunction(Evaluator* eval, const ArrayOfVector& inputs | |||||
|
|
||||||
| outputs = prepareOutputs(context, nargout); | ||||||
|
|
||||||
| onCleanup(eval); | ||||||
|
|
||||||
| if (!cleanupTasks.empty()) { | ||||||
| onCleanup(eval); | ||||||
| } | ||||||
| context->popScope(); | ||||||
| eval->callstack.popDebug(); | ||||||
| } catch (const Exception&) { | ||||||
| onCleanup(eval); | ||||||
| if (!cleanupTasks.empty()) { | ||||||
| onCleanup(eval); | ||||||
| } | ||||||
| if (recursionDepth == 1 && tic != 0) { | ||||||
| internalProfileFunction stack | ||||||
| = computeProfileStack(eval, getCompleteName(), this->getFilename(), false); | ||||||
|
|
@@ -283,12 +291,26 @@ MacroFunctionDef::evaluateMScript(Evaluator* eval, const ArrayOfVector& inputs, | |||||
| ArrayOfVector | ||||||
| MacroFunctionDef::evaluateFunction(Evaluator* eval, const ArrayOfVector& inputs, int nargout) | ||||||
| { | ||||||
| lock(); | ||||||
| updateCode(); | ||||||
| if (isScript) { | ||||||
| return evaluateMScript(eval, inputs, nargout); | ||||||
| // Skip lock/updateCode on recursive calls � code cannot change mid-recursion. | ||||||
|
||||||
| // Skip lock/updateCode on recursive calls � code cannot change mid-recursion. | |
| // Skip lock/updateCode on recursive calls - code cannot change mid-recursion. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,7 +115,7 @@ ArrayOf | |
| ArrayOf::emptyCell(const Dimensions& dim) | ||
| { | ||
| if (dim.getElementCount() == 0) { | ||
| return ArrayOf(NLS_CELL_ARRAY, dim, nullptr, false); | ||
| return ArrayOf(NLS_CELL_ARRAY, dim, (void*)nullptr, false); | ||
|
||
| } | ||
| Error(_W("Invalid dimensions.")); | ||
| return {}; | ||
|
|
@@ -125,7 +125,7 @@ ArrayOf | |
| ArrayOf::emptyConstructor(const Dimensions& dim, bool bIsSparse) | ||
| { | ||
| if (dim.getElementCount() == 0) { | ||
| return ArrayOf(NLS_DOUBLE, dim, nullptr, bIsSparse); | ||
| return ArrayOf(NLS_DOUBLE, dim, (void*)nullptr, bIsSparse); | ||
|
||
| } | ||
| Error(_W("Invalid dimensions.")); | ||
|
|
||
|
|
@@ -137,7 +137,7 @@ ArrayOf::emptyConstructor(indexType m, indexType n, bool bIsSparse) | |
| { | ||
| if (((m == 0) && (n == 0)) || ((m == 0) && (n != 0)) || ((m != 0) && (n == 0))) { | ||
| Dimensions dim(m, n); | ||
| return ArrayOf(NLS_DOUBLE, dim, nullptr, bIsSparse); | ||
| return ArrayOf(NLS_DOUBLE, dim, (void*)nullptr, bIsSparse); | ||
|
||
| } | ||
| Error(_W("Invalid dimensions.")); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,19 +10,33 @@ | |||||||||||||||||||||||||||||||||
| //============================================================================= | ||||||||||||||||||||||||||||||||||
| #include <utility> | ||||||||||||||||||||||||||||||||||
| #include <atomic> | ||||||||||||||||||||||||||||||||||
| #include <cstring> | ||||||||||||||||||||||||||||||||||
| //============================================================================= | ||||||||||||||||||||||||||||||||||
| #include "Data.hpp" | ||||||||||||||||||||||||||||||||||
| #include "SparseDynamicFunctions.hpp" | ||||||||||||||||||||||||||||||||||
| //============================================================================= | ||||||||||||||||||||||||||||||||||
| namespace Nelson { | ||||||||||||||||||||||||||||||||||
| //============================================================================= | ||||||||||||||||||||||||||||||||||
| Data::Data(NelsonType aClass, const Dimensions& dims, void* s, bool sparseflag, stringVector fields) | ||||||||||||||||||||||||||||||||||
| : cp(s), owners(1), dimensions(dims), fieldNames(std::move(fields)), dataClass(aClass) | ||||||||||||||||||||||||||||||||||
| : cp(s) | ||||||||||||||||||||||||||||||||||
| , owners(1) | ||||||||||||||||||||||||||||||||||
| , dimensions(dims) | ||||||||||||||||||||||||||||||||||
| , fieldNames(std::move(fields)) | ||||||||||||||||||||||||||||||||||
| , dataClass(aClass) | ||||||||||||||||||||||||||||||||||
| , isInline(false) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| sparse = sparseflag; | ||||||||||||||||||||||||||||||||||
| refreshDimensionCache(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| //============================================================================= | ||||||||||||||||||||||||||||||||||
| Data::Data(NelsonType aClass, const Dimensions& dims, const void* scalarData, size_t dataSize) | ||||||||||||||||||||||||||||||||||
| : owners(1), dimensions(dims), dataClass(aClass), sparse(false), isInline(true) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| std::memcpy(inlineBuffer, scalarData, dataSize); | ||||||||||||||||||||||||||||||||||
| cp = inlineBuffer; | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+36
|
||||||||||||||||||||||||||||||||||
| : owners(1), dimensions(dims), dataClass(aClass), sparse(false), isInline(true) | |
| { | |
| std::memcpy(inlineBuffer, scalarData, dataSize); | |
| cp = inlineBuffer; | |
| : owners(1), dimensions(dims), dataClass(aClass), sparse(false), isInline(false) | |
| { | |
| if (dataSize <= INLINE_BUFFER_SIZE) { | |
| std::memcpy(inlineBuffer, scalarData, dataSize); | |
| cp = inlineBuffer; | |
| isInline = true; | |
| } else { | |
| void* heapData = ::operator new(dataSize); | |
| std::memcpy(heapData, scalarData, dataSize); | |
| cp = heapData; | |
| // isInline remains false for heap-allocated storage | |
| } |
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.
Binding a
const std::string&to the result ofgetName()is only safe ifgetName()returns a reference; if it returns by value, this becomes a dangling reference. Store by value (as before) or useauto currentFunction = ...;unlessgetName()is guaranteed to returnconst std::string&.