Skip to content

Commit cdb2bb5

Browse files
committed
refactor: normalize dtypes to enums to reduce memory consumption and speed-up comparisons
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 8aae193 commit cdb2bb5

File tree

2 files changed

+46
-36
lines changed
  • lib/node_modules/@stdlib/ndarray/base/binary-reduce-strided1d-dispatch/lib

2 files changed

+46
-36
lines changed

lib/node_modules/@stdlib/ndarray/base/binary-reduce-strided1d-dispatch/lib/main.js

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ var isFunctionArray = require( '@stdlib/assert/is-function-array' );
3333
var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' );
3434
var isOutputDataTypePolicy = require( '@stdlib/ndarray/base/assert/is-output-data-type-policy' );
3535
var isInputCastingPolicy = require( '@stdlib/ndarray/base/assert/is-input-casting-policy' );
36-
var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' );
3736
var contains = require( '@stdlib/array/base/assert/contains' );
3837
var binaryReduceStrided1d = require( '@stdlib/ndarray/base/binary-reduce-strided1d' );
3938
var binaryOutputDataType = require( '@stdlib/ndarray/base/binary-output-dtype' );
4039
var binaryInputCastingDataType = require( '@stdlib/ndarray/base/binary-input-casting-dtype' );
40+
var resolveEnum = require( '@stdlib/ndarray/base/dtype-resolve-enum' );
4141
var dtypes2enums = require( '@stdlib/ndarray/base/dtypes2enums' );
4242
var dtypes2strings = require( '@stdlib/ndarray/base/dtypes2strings' );
43-
var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' );
4443
var spreadDimensions = require( '@stdlib/ndarray/base/spread-dimensions' );
4544
var getShape = require( '@stdlib/ndarray/shape' ); // note: non-base accessor is intentional due to input ndarrays originating in userland
4645
var ndims = require( '@stdlib/ndarray/ndims' );
@@ -118,6 +117,8 @@ var DEFAULT_ORDER = defaults.get( 'order' );
118117
* // returns <ndarray>[ -5.0 ]
119118
*/
120119
function BinaryStrided1dDispatch( table, idtypes, odtypes, policies ) {
120+
var idt;
121+
var odt;
121122
var dt;
122123
var i;
123124
if ( !( this instanceof BinaryStrided1dDispatch ) ) {
@@ -138,7 +139,7 @@ function BinaryStrided1dDispatch( table, idtypes, odtypes, policies ) {
138139
if ( !isCollection( idtypes ) ) {
139140
throw new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', idtypes ) );
140141
}
141-
idtypes = copy( idtypes );
142+
idt = [];
142143
for ( i = 0; i < idtypes.length; i++ ) {
143144
dt = idtypes[ i ];
144145
if (
@@ -148,7 +149,7 @@ function BinaryStrided1dDispatch( table, idtypes, odtypes, policies ) {
148149
) {
149150
throw new TypeError( format( 'invalid argument. Second argument must contain arrays of data types. Value: `%s`.', idtypes ) );
150151
}
151-
idtypes[ i ] = dtypes2strings( dt );
152+
idt.push( dtypes2enums( dt ) ); // note: convert to enums (i.e., integers) to ensure faster comparisons and reduced storage requirements
152153
}
153154
if (
154155
!isCollection( odtypes ) ||
@@ -157,7 +158,7 @@ function BinaryStrided1dDispatch( table, idtypes, odtypes, policies ) {
157158
) {
158159
throw new TypeError( format( 'invalid argument. Third argument must be an array of data types. Value: `%s`.', odtypes ) );
159160
}
160-
odtypes = dtypes2strings( odtypes );
161+
odt = dtypes2enums( odtypes ); // note: convert to enums (i.e., integers) to ensure faster comparisons and reduced storage requirements
161162

162163
if ( !isObject( policies ) ) {
163164
throw new TypeError( format( 'invalid argument. Fourth argument must be an object. Value: `%s`.', table ) );
@@ -170,14 +171,14 @@ function BinaryStrided1dDispatch( table, idtypes, odtypes, policies ) {
170171
}
171172
this._table = {
172173
'default': table.default,
173-
'types': ( table.types ) ? dtypes2enums( table.types ) : [], // note: convert to enums (i.e., integers) to ensure faster comparisons
174+
'types': ( table.types ) ? dtypes2enums( table.types ) : [], // note: convert to enums (i.e., integers) to ensure faster comparisons and reduced storage requirements
174175
'fcns': ( table.fcns ) ? copy( table.fcns ) : []
175176
};
176177
if ( this._table.types.length !== 2 * this._table.fcns.length ) {
177178
throw new Error( 'invalid argument. First argument specifies an unexpected number of types. Two input ndarray data types must be specified for each provided strided function.' );
178179
}
179-
this._idtypes = idtypes;
180-
this._odtypes = odtypes;
180+
this._idtypes = idt;
181+
this._odtypes = odt;
181182
this._policies = {
182183
'output': policies.output,
183184
'casting': policies.casting
@@ -240,6 +241,8 @@ setReadOnly( BinaryStrided1dDispatch.prototype, 'apply', function apply( x, y )
240241
var ordx;
241242
var ordy;
242243
var ordz;
244+
var xdte;
245+
var ydte;
243246
var err;
244247
var idx;
245248
var shx;
@@ -265,12 +268,14 @@ setReadOnly( BinaryStrided1dDispatch.prototype, 'apply', function apply( x, y )
265268
throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', y ) );
266269
}
267270
xdt = getDType( x );
268-
if ( !contains( this._idtypes[ 0 ], resolveStr( xdt ) ) ) {
269-
throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( this._idtypes[ 0 ], '", "' ), xdt ) );
271+
xdte = resolveEnum( xdt );
272+
if ( !contains( this._idtypes[ 0 ], xdte ) ) {
273+
throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( dtypes2strings( this._idtypes[ 0 ] ), '", "' ), xdt ) );
270274
}
271275
ydt = getDType( y );
272-
if ( !contains( this._idtypes[ 1 ], resolveStr( ydt ) ) ) {
273-
throw new TypeError( format( 'invalid argument. Second argument must have one of the following data types: "%s". Data type: `%s`.', join( this._idtypes[ 1 ], '", "' ), ydt ) );
276+
ydte = resolveEnum( ydt );
277+
if ( !contains( this._idtypes[ 1 ], ydte ) ) {
278+
throw new TypeError( format( 'invalid argument. Second argument must have one of the following data types: "%s". Data type: `%s`.', join( dtypes2strings( this._idtypes[ 1 ] ), '", "' ), ydt ) );
274279
}
275280
args = [ x, y ];
276281
for ( i = 2; i < nargs; i++ ) {
@@ -279,10 +284,10 @@ setReadOnly( BinaryStrided1dDispatch.prototype, 'apply', function apply( x, y )
279284
break;
280285
}
281286
dt = getDType( arr );
282-
if ( !contains( this._idtypes[ i ], resolveStr( dt ) ) ) {
283-
throw new TypeError( format( 'invalid argument. Argument %d must have one of the following data types: "%s". Data type: `%s`.', i, join( this._idtypes[ i ], '", "' ), dt ) );
287+
if ( !contains( this._idtypes[ i ], resolveEnum( dt ) ) ) {
288+
throw new TypeError( format( 'invalid argument. Argument %d must have one of the following data types: "%s". Data type: `%s`.', i, join( dtypes2strings( this._idtypes[ i ] ), '", "' ), dt ) );
284289
}
285-
// Note: we don't type promote additional ndarray arguments, as they are passed as scalars to the underlying strided reduction function...
290+
// Note: we don't type promote additional ndarray arguments...
286291
args.push( arr );
287292
}
288293
// If we didn't make it up until the last argument, this means that we found a non-options argument which was not an ndarray...
@@ -338,23 +343,23 @@ setReadOnly( BinaryStrided1dDispatch.prototype, 'apply', function apply( x, y )
338343

339344
// Determine whether we need to cast the input ndarrays...
340345
dt = binaryInputCastingDataType( xdt, ydt, zdt, this._policies.casting );
341-
if ( !isEqualDataType( xdt, dt ) ) {
342-
// TODO: replace the following logic with a call to `ndarray/base/(?maybe-)(cast|convert|copy)` or similar utility
346+
if ( xdte !== resolveEnum( dt ) ) {
347+
// TODO: consider replacing the following logic with a call to `ndarray/base/(?maybe-)(cast|convert)` or similar utility
343348
tmp = baseEmpty( dt, shx, ordx );
344349
assign( [ x, tmp ] );
345350
args[ 0 ] = tmp;
346351
xdt = dt;
347352
}
348353
dt = binaryInputCastingDataType( ydt, xdt, zdt, this._policies.casting );
349-
if ( !isEqualDataType( ydt, dt ) ) {
350-
// TODO: replace the following logic with a call to `ndarray/base/(?maybe-)(cast|convert|copy)` or similar utility
354+
if ( ydte !== resolveEnum( dt ) ) {
355+
// TODO: consider replacing the following logic with a call to `ndarray/base/(?maybe-)(cast|convert)` or similar utility
351356
tmp = baseEmpty( dt, shy, ordy );
352357
assign( [ y, tmp ] );
353358
args[ 1 ] = tmp;
354359
ydt = dt;
355360
}
356361
// Resolve the lower-level strided function satisfying the input ndarray data types:
357-
i = gindexOfRow( this._table.fcns.length, 2, this._table.types, 2, 1, 0, dtypes2enums( [ xdt, ydt ] ), 1, 0 ); // eslint-disable-line max-len
362+
i = gindexOfRow( this._table.fcns.length, 2, this._table.types, 2, 1, 0, [ xdte, ydte ], 1, 0 ); // eslint-disable-line max-len
358363
if ( i >= 0 ) {
359364
f = this._table.fcns[ i ];
360365
} else {
@@ -430,6 +435,8 @@ setReadOnly( BinaryStrided1dDispatch.prototype, 'assign', function assign( x, y
430435
var nargs;
431436
var opts;
432437
var args;
438+
var xdte;
439+
var ydte;
433440
var arr;
434441
var err;
435442
var flg;
@@ -455,12 +462,14 @@ setReadOnly( BinaryStrided1dDispatch.prototype, 'assign', function assign( x, y
455462
}
456463
// Validate the input ndarray data types in order to maintain similar behavior to `apply` above...
457464
xdt = getDType( x );
458-
if ( !contains( this._idtypes[ 0 ], resolveStr( xdt ) ) ) {
459-
throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( this._idtypes[ 0 ], '", "' ), xdt ) );
465+
xdte = resolveEnum( xdt );
466+
if ( !contains( this._idtypes[ 0 ], xdte ) ) {
467+
throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( dtypes2strings( this._idtypes[ 0 ] ), '", "' ), xdt ) );
460468
}
461469
ydt = getDType( y );
462-
if ( !contains( this._idtypes[ 1 ], resolveStr( ydt ) ) ) {
463-
throw new TypeError( format( 'invalid argument. Second argument must have one of the following data types: "%s". Data type: `%s`.', join( this._idtypes[ 1 ], '", "' ), ydt ) );
470+
ydte = resolveEnum( ydt );
471+
if ( !contains( this._idtypes[ 1 ], ydte ) ) {
472+
throw new TypeError( format( 'invalid argument. Second argument must have one of the following data types: "%s". Data type: `%s`.', join( dtypes2strings( this._idtypes[ 1 ] ), '", "' ), ydt ) );
464473
}
465474
// Verify that both input arrays have the same shape:
466475
shx = getShape( x );
@@ -500,11 +509,11 @@ setReadOnly( BinaryStrided1dDispatch.prototype, 'assign', function assign( x, y
500509
// Cache a reference to the output ndarray:
501510
z = args.pop();
502511

503-
// Verify that additional ndarray arguments have expected dtypes (note: we intentionally don't validate the output ndarray dtype in order to provide an escape hatch for a user wanting to have an output ndarray having a specific dtype that `apply` does not support; note: we don't type promote additional ndarray arguments, as they are passed as scalars to the underlying strided reduction function)...
512+
// Verify that additional ndarray arguments have expected dtypes (note: we intentionally don't validate the output ndarray dtype in order to provide an escape hatch for a user wanting to have an output ndarray having a specific dtype that `apply` does not support; note: we don't type promote additional ndarray arguments)...
504513
for ( i = 2; i < args.length; i++ ) {
505514
dt = getDType( args[ i ] );
506-
if ( !contains( this._idtypes[ i ], resolveStr( dt ) ) ) {
507-
throw new TypeError( format( 'invalid argument. Argument %d must have one of the following data types: "%s". Data type: `%s`.', i, join( this._idtypes[ i ], '", "' ), dt ) );
515+
if ( !contains( this._idtypes[ i ], resolveEnum( dt ) ) ) {
516+
throw new TypeError( format( 'invalid argument. Argument %d must have one of the following data types: "%s". Data type: `%s`.', i, join( dtypes2strings( this._idtypes[ i ] ), '", "' ), dt ) );
508517
}
509518
}
510519
// Validate any provided options...
@@ -523,23 +532,23 @@ setReadOnly( BinaryStrided1dDispatch.prototype, 'assign', function assign( x, y
523532
// Determine whether we need to cast the input ndarrays...
524533
zdt = getDType( z );
525534
dt = binaryInputCastingDataType( xdt, ydt, zdt, this._policies.casting );
526-
if ( !isEqualDataType( xdt, dt ) ) {
527-
// TODO: replace the following logic with a call to `ndarray/base/(?maybe-)(cast|convert|copy)` or similar utility
535+
if ( xdte !== resolveEnum( dt ) ) {
536+
// TODO: consider replacing the following logic with a call to `ndarray/base/(?maybe-)(cast|convert)` or similar utility
528537
tmp = baseEmpty( dt, shx, getOrder( x ) );
529538
assign( [ x, tmp ] );
530539
args[ 0 ] = tmp;
531540
xdt = dt;
532541
}
533542
dt = binaryInputCastingDataType( ydt, xdt, zdt, this._policies.casting );
534-
if ( !isEqualDataType( ydt, dt ) ) {
535-
// TODO: replace the following logic with a call to `ndarray/base/(?maybe-)(cast|convert|copy)` or similar utility
543+
if ( ydte !== resolveEnum( dt ) ) {
544+
// TODO: consider replacing the following logic with a call to `ndarray/base/(?maybe-)(cast|convert)` or similar utility
536545
tmp = baseEmpty( dt, shy, getOrder( y ) );
537546
assign( [ y, tmp ] );
538547
args[ 1 ] = tmp;
539548
ydt = dt;
540549
}
541550
// Resolve the lower-level strided function satisfying the input ndarray data type:
542-
i = gindexOfRow( this._table.fcns.length, 2, this._table.types, 2, 1, 0, dtypes2enums( [ xdt, ydt ] ), 1, 0 ); // eslint-disable-line max-len
551+
i = gindexOfRow( this._table.fcns.length, 2, this._table.types, 2, 1, 0, [ xdte, ydte ], 1, 0 ); // eslint-disable-line max-len
543552
if ( i >= 0 ) {
544553
f = this._table.fcns[ i ];
545554
} else {

lib/node_modules/@stdlib/ndarray/base/binary-reduce-strided1d-dispatch/lib/validate.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2626
var isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives;
2727
var isEmptyCollection = require( '@stdlib/assert/is-empty-collection' );
2828
var normalizeIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' );
29-
var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' );
29+
var resolveEnum = require( '@stdlib/ndarray/base/dtype-resolve-enum' );
30+
var dtypes2strings = require( '@stdlib/ndarray/base/dtypes2strings' );
3031
var join = require( '@stdlib/array/base/join' );
3132
var contains = require( '@stdlib/array/base/assert/contains' );
3233
var format = require( '@stdlib/string/format' );
@@ -85,9 +86,9 @@ function validate( opts, ndims, dtypes, options ) {
8586
opts.dims = tmp;
8687
}
8788
if ( hasOwnProp( options, 'dtype' ) ) {
88-
opts.dtype = resolveStr( options.dtype );
89-
if ( !contains( dtypes, opts.dtype ) ) {
90-
return new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Option: `%s`.', 'dtype', join( dtypes, '", "' ), options.dtype ) );
89+
opts.dtype = options.dtype;
90+
if ( !contains( dtypes, resolveEnum( opts.dtype ) ) ) {
91+
return new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Option: `%s`.', 'dtype', join( dtypes2strings( dtypes ), '", "' ), options.dtype ) );
9192
}
9293
}
9394
return null;

0 commit comments

Comments
 (0)