Skip to content

Commit

Permalink
optimizeCallback returns a non typed function.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x committed Sep 21, 2024
1 parent 9f80b90 commit a5618b5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/function/matrix/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const createFilter = /* #__PURE__ */ factory(name, dependencies, ({ typed
* @private
*/
function _filterCallback (x, callback) {
const fastCallback = optimizeCallback(callback, x, 'filter')[0]
const fastCallback = optimizeCallback(callback, x, 'filter')
return filter(x, function (value, index, array) {
// invoke the callback function with the right number of arguments
return fastCallback(value, [index], array)
Expand Down
4 changes: 2 additions & 2 deletions src/function/matrix/forEach.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ export const createForEach = /* #__PURE__ */ factory(name, dependencies, ({ type
* @private
*/
function _forEach (array, callback) {
const [fastCallback, numberOfArguments] = optimizeCallback(callback, array, name, { detailedError: true })
deepForEach(array, array, fastCallback, numberOfArguments)
const fastCallback = optimizeCallback(callback, array, name, { detailedError: true })
deepForEach(array, array, fastCallback, fastCallback.length)
}
4 changes: 2 additions & 2 deletions src/function/matrix/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const createMap = /* #__PURE__ */ factory(name, dependencies, ({ typed })
* @private
*/
function _mapArray (array, callback) {
const [fastCallback, numberOfArguments] = optimizeCallback(callback, array, name, { detailedError: true })
return deepMap(array, array, fastCallback, numberOfArguments)
const fastCallback = optimizeCallback(callback, array, name, { detailedError: true })
return deepMap(array, array, fastCallback, fastCallback.length)
}
})
8 changes: 4 additions & 4 deletions src/type/matrix/DenseMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,11 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
DenseMatrix.prototype.map = function (callback) {
// matrix instance
const me = this
const [fastCallback, numberOfArguments] = optimizeCallback(callback, me._data, 'map')
const fastCallback = optimizeCallback(callback, me._data, 'map')

// determine the new datatype when the original matrix has datatype defined
// TODO: should be done in matrix constructor instead
const data = deepMap(me._data, me, fastCallback, numberOfArguments)
const data = deepMap(me._data, me, fastCallback, fastCallback.length)
const datatype = me._datatype !== undefined
? getArrayDataType(data, typeOf)
: undefined
Expand All @@ -558,8 +558,8 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
DenseMatrix.prototype.forEach = function (callback) {
// matrix instance
const me = this
const [fastCallback, numberOfArguments] = optimizeCallback(callback, me._data, 'forEach')
deepForEach(this._data, me, fastCallback, numberOfArguments)
const fastCallback = optimizeCallback(callback, me._data, 'forEach')
deepForEach(this._data, me, fastCallback, fastCallback.length)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/type/matrix/SparseMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ export const createSparseMatrixClass = /* #__PURE__ */ factory(name, dependencie
// rows and columns
const rows = this._size[0]
const columns = this._size[1]
const fastCallback = optimizeCallback(callback, me, 'map')[0]
const fastCallback = optimizeCallback(callback, me, 'map')
// invoke callback
const invoke = function (v, i, j) {
// invoke callback
Expand Down Expand Up @@ -962,7 +962,7 @@ export const createSparseMatrixClass = /* #__PURE__ */ factory(name, dependencie
// rows and columns
const rows = this._size[0]
const columns = this._size[1]
const fastCallback = optimizeCallback(callback, me, 'forEach')[0]
const fastCallback = optimizeCallback(callback, me, 'forEach')
// loop columns
for (let j = 0; j < columns; j++) {
// k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1]
Expand Down
34 changes: 22 additions & 12 deletions src/utils/optimizeCallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,34 @@ export function optimizeCallback (callback, array, name, options) {
const firstValue = array.isMatrix ? array.get(firstIndex) : get(array, firstIndex)
const hasSingleSignature = Object.keys(callback.signatures).length === 1
const numberOfArguments = _typedFindNumberOfArguments(callback, firstValue, firstIndex, array)
const fastCallback = hasSingleSignature ? Object.values(callback.signatures)[0] : (...args) => callback(...args)
if (options && options.detailedError) {
if (numberOfArguments >= 1 && numberOfArguments <= 3) {
const limitedCallback = (...args) => tryFunctionWithArgs(fastCallback, args.slice(0, numberOfArguments), name, callback.name)
return [limitedCallback, numberOfArguments]
} else {
const enhancedCallback = (...args) => tryFunctionWithArgs(fastCallback, args, name, callback.name)
return [enhancedCallback, numberOfArguments]
const fastCallback = hasSingleSignature ? Object.values(callback.signatures)[0] : callback
switch (numberOfArguments) {
case 1:
return (val) => tryFunctionWithArgs(fastCallback, [val], name, callback.name)
case 2:
return (val, idx) => tryFunctionWithArgs(fastCallback, [val, idx], name, callback.name)
case 3:
return (val, idx, array) => tryFunctionWithArgs(fastCallback, [val, idx, array], name, callback.name)
default:
return (...args) => tryFunctionWithArgs(fastCallback, args, name, callback.name)
}
} else if (hasSingleSignature) {
return Object.values(callback.signatures)[0]
} else {
if (numberOfArguments >= 1 && numberOfArguments <= 3) {
return [(...args) => fastCallback(...args.slice(0, numberOfArguments)), numberOfArguments]
} else {
return [(...args) => fastCallback(...args), numberOfArguments]
switch (numberOfArguments) {
case 1:
return val => callback(val)
case 2:
return (val, idx) => callback(val, idx)
case 3:
return (val, idx, array) => callback(val, idx, array)
default:
return callback
}
}
}
return [callback, callback.length]
return callback
}

export function findNumberOfArguments (callback, array) {
Expand Down

0 comments on commit a5618b5

Please sign in to comment.