Skip to content

Commit f6266a0

Browse files
committed
Address performance bottleneck when resolving a data source's underlying data type
1 parent a1a553f commit f6266a0

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var isBuffer = require( '@stdlib/assert/is-buffer' );
24+
var isArray = require( '@stdlib/assert/is-array' );
25+
var Float64Array = require( '@stdlib/array/float64' );
26+
var Float32Array = require( '@stdlib/array/float32' );
27+
var Uint32Array = require( '@stdlib/array/uint32' );
28+
var Int32Array = require( '@stdlib/array/int32' );
29+
var Uint16Array = require( '@stdlib/array/uint16' );
30+
var Int16Array = require( '@stdlib/array/int16' );
31+
var Uint8Array = require( '@stdlib/array/uint8' );
32+
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
33+
var Int8Array = require( '@stdlib/array/int8' );
34+
var getType = require( '@stdlib/ndarray/base/buffer-dtype' );
35+
36+
37+
// VARIABLES //
38+
39+
var TYPES = [
40+
[ Float64Array, 'float64' ],
41+
[ Float32Array, 'float32' ],
42+
[ Uint32Array, 'uint32' ],
43+
[ Int32Array, 'int32' ],
44+
[ Uint16Array, 'uint16' ],
45+
[ Int16Array, 'int16' ],
46+
[ Uint8Array, 'uint8' ],
47+
[ Uint8ClampedArray, 'uint8c' ],
48+
[ Int8Array, 'int8' ]
49+
];
50+
var NTYPES = TYPES.length;
51+
52+
53+
// MAIN //
54+
55+
/**
56+
* Resolves a data source underlying data type.
57+
*
58+
* @private
59+
* @param {(Array|TypedArray|Buffer)} buffer - data source
60+
* @returns {string} data type
61+
*/
62+
function dtype( buffer ) {
63+
var i;
64+
if ( isArray( buffer ) ) {
65+
return 'generic';
66+
}
67+
if ( isBuffer( buffer ) ) {
68+
return 'binary';
69+
}
70+
// By process of elimination, left with typed arrays...
71+
for ( i = 0; i < NTYPES; i++ ) {
72+
if ( buffer instanceof TYPES[ i ][ 0 ] ) {
73+
return TYPES[ i ][ 1 ];
74+
}
75+
}
76+
// If the above failed, fall back to a more robust (and significantly slower) means for resolving underlying data types:
77+
return getType( buffer );
78+
}
79+
80+
81+
// EXPORTS //
82+
83+
module.exports = dtype;

lib/node_modules/@stdlib/ndarray/lib/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ var ctor = require( '@stdlib/ndarray/ctor' );
3333
var mctor = require( '@stdlib/ndarray/memoized-ctor' );
3434
var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' );
3535
var isOrder = require( '@stdlib/ndarray/base/assert/is-order' );
36-
var getType = require( '@stdlib/ndarray/base/buffer-dtype' );
3736
var createBuffer = require( '@stdlib/ndarray/base/buffer' );
3837
var arrayShape = require( '@stdlib/array/shape' );
3938
var flattenArray = require( '@stdlib/utils/flatten-array' );
4039
var isArrayLikeObject = require( './is_array_like_object.js' );
4140
var defaults = require( './defaults.json' );
41+
var getType = require( './dtype.js' );
4242
var castBuffer = require( './cast_buffer.js' );
4343
var copyView = require( './copy_view.js' );
4444

0 commit comments

Comments
 (0)