time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (12261B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2018 The Stdlib Authors.
      6 
      7 Licensed under the Apache License, Version 2.0 (the "License");
      8 you may not use this file except in compliance with the License.
      9 You may obtain a copy of the License at
     10 
     11    http://www.apache.org/licenses/LICENSE-2.0
     12 
     13 Unless required by applicable law or agreed to in writing, software
     14 distributed under the License is distributed on an "AS IS" BASIS,
     15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 See the License for the specific language governing permissions and
     17 limitations under the License.
     18 
     19 -->
     20 
     21 # Typed Array Pool
     22 
     23 > Allocate typed arrays from a typed array memory pool.
     24 
     25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
     26 
     27 <section class="intro">
     28 
     29 </section>
     30 
     31 <!-- /.intro -->
     32 
     33 <!-- Package usage documentation. -->
     34 
     35 <section class="usage">
     36 
     37 ## Usage
     38 
     39 ```javascript
     40 var typedarraypool = require( '@stdlib/array/pool' );
     41 ```
     42 
     43 #### typedarraypool( \[dtype] )
     44 
     45 Returns an **uninitialized** [typed array][mdn-typed-array] having a specified data type `dtype`.
     46 
     47 ```javascript
     48 var arr = typedarraypool();
     49 // returns <Float64Array>[]
     50 
     51 // ...
     52 
     53 typedarraypool.free( arr );
     54 ```
     55 
     56 The function recognizes the following data types:
     57 
     58 -   `float64`: double-precision floating-point numbers (IEEE 754)
     59 -   `float32`: single-precision floating-point numbers (IEEE 754)
     60 -   `int32`: 32-bit two's complement signed integers
     61 -   `uint32`: 32-bit unsigned integers
     62 -   `int16`: 16-bit two's complement signed integers
     63 -   `uint16`: 16-bit unsigned integers
     64 -   `int8`: 8-bit two's complement signed integers
     65 -   `uint8`: 8-bit unsigned integers
     66 -   `uint8c`: 8-bit unsigned integers clamped to `0-255`
     67 
     68 By default, the output [typed array][mdn-typed-array] is `float64`. To specify an alternative data type, set the `dtype` parameter.
     69 
     70 ```javascript
     71 var arr = typedarraypool( 'int32' );
     72 // returns <Int32Array>[]
     73 
     74 // ...
     75 
     76 typedarraypool.free( arr );
     77 ```
     78 
     79 #### typedarraypool( length\[, dtype] )
     80 
     81 Returns an **uninitialized** [typed array][mdn-typed-array] having a specified `length` from a [typed array][mdn-typed-array] memory pool.
     82 
     83 ```javascript
     84 var arr1 = typedarraypool( 5 );
     85 // returns <Float64Array>
     86 
     87 var arr2 = typedarraypool( 5, 'uint8' );
     88 // returns <Uint8Array>
     89 
     90 // ...
     91 
     92 typedarraypool.free( arr1 );
     93 typedarraypool.free( arr2 );
     94 ```
     95 
     96 #### typedarraypool( typedarray\[, dtype] )
     97 
     98 Returns a pooled [typed array][mdn-typed-array] from another [typed array][mdn-typed-array].
     99 
    100 ```javascript
    101 var arr1 = typedarraypool( [ 5.0, -3.0, 2.0 ] );
    102 // returns <Float64Array>[ 5.0, -3.0, 2.0 ]
    103 
    104 var arr2 = typedarraypool( arr1 );
    105 // returns <Float64Array>[ 5.0, -3.0, 2.0 ]
    106 
    107 var arr3 = typedarraypool( arr1, 'int32' );
    108 // returns <Int32Array>[ 5, -3, 2 ]
    109 
    110 // ...
    111 
    112 typedarraypool.free( arr1 );
    113 typedarraypool.free( arr2 );
    114 typedarraypool.free( arr3 );
    115 ```
    116 
    117 #### typedarraypool( obj\[, dtype] )
    118 
    119 Returns a pooled [typed array][mdn-typed-array] from an array-like `object`.
    120 
    121 ```javascript
    122 var arr1 = typedarraypool( [ 0.5, 0.5, 0.5 ] );
    123 // returns <Float64Array>[ 0.5, 0.5, 0.5 ]
    124 
    125 var arr2 = typedarraypool( [ 0.5, 0.5, 0.5 ], 'float32' );
    126 // returns <Float32Array>[ 0.5, 0.5, 0.5 ]
    127 
    128 // ...
    129 
    130 typedarraypool.free( arr1 );
    131 typedarraypool.free( arr2 );
    132 ```
    133 
    134 #### typedarraypool.malloc( \[dtype] )
    135 
    136 Returns an **uninitialized** [typed array][mdn-typed-array] having a specified data type `dtype`.
    137 
    138 ```javascript
    139 var arr1 = typedarraypool.malloc();
    140 // returns <Float64Array>[]
    141 
    142 var arr2 = typedarraypool.malloc( 'int32' );
    143 // returns <Int32Array>[]
    144 
    145 // ...
    146 
    147 typedarraypool.free( arr1 );
    148 typedarraypool.free( arr2 );
    149 ```
    150 
    151 #### typedarraypool.malloc( length\[, dtype] )
    152 
    153 Returns an **uninitialized** [typed array][mdn-typed-array] having a specified `length` from a [typed array][mdn-typed-array] memory pool.
    154 
    155 ```javascript
    156 var arr1 = typedarraypool.malloc( 5 );
    157 // returns <Float64Array>
    158 
    159 var arr2 = typedarraypool.malloc( 5, 'uint8' );
    160 // returns <Uint8Array>
    161 
    162 // ...
    163 
    164 typedarraypool.free( arr1 );
    165 typedarraypool.free( arr2 );
    166 ```
    167 
    168 #### typedarraypool.malloc( typedarray\[, dtype] )
    169 
    170 Returns a pooled [typed array][mdn-typed-array] from another [typed array][mdn-typed-array].
    171 
    172 ```javascript
    173 var arr1 = typedarraypool.malloc( [ 5.0, -3.0, 2.0 ] );
    174 // returns <Float64Array>[ 5.0, -3.0, 2.0 ]
    175 
    176 var arr2 = typedarraypool.malloc( arr1 );
    177 // returns <Float64Array>[ 5.0, -3.0, 2.0 ]
    178 
    179 var arr3 = typedarraypool.malloc( arr1, 'int32' );
    180 // returns <Int32Array>[ 5, -3, 2 ]
    181 
    182 // ...
    183 
    184 typedarraypool.free( arr1 );
    185 typedarraypool.free( arr2 );
    186 typedarraypool.free( arr3 );
    187 ```
    188 
    189 #### typedarraypool.malloc( obj\[, dtype] )
    190 
    191 Returns a pooled [typed array][mdn-typed-array] from an array-like `object`.
    192 
    193 ```javascript
    194 var arr1 = typedarraypool.malloc( [ 0.5, 0.5, 0.5 ] );
    195 // returns <Float64Array>[ 0.5, 0.5, 0.5 ]
    196 
    197 var arr2 = typedarraypool.malloc( [ 0.5, 0.5, 0.5 ], 'float32' );
    198 // returns <Float32Array>[ 0.5, 0.5, 0.5 ]
    199 
    200 // ...
    201 
    202 typedarraypool.free( arr1 );
    203 typedarraypool.free( arr2 );
    204 ```
    205 
    206 #### typedarraypool.calloc( \[dtype] )
    207 
    208 Returns a **zero-initialized** [typed array][mdn-typed-array] having a specified data type `dtype`.
    209 
    210 ```javascript
    211 var arr1 = typedarraypool.calloc();
    212 // returns <Float64Array>[]
    213 
    214 var arr2 = typedarraypool.calloc( 'int32' );
    215 // returns <Int32Array>[]
    216 
    217 // ...
    218 
    219 typedarraypool.free( arr1 );
    220 typedarraypool.free( arr2 );
    221 ```
    222 
    223 #### typedarraypool.calloc( length\[, dtype] )
    224 
    225 Returns a **zero-initialized** [typed array][mdn-typed-array] having a specified `length` from a [typed array][mdn-typed-array] memory pool.
    226 
    227 ```javascript
    228 var arr1 = typedarraypool.calloc( 5 );
    229 // returns <Float64Array>[ 0.0, 0.0, 0.0, 0.0, 0.0 ]
    230 
    231 var arr2 = typedarraypool.calloc( 5, 'uint8' );
    232 // returns <Uint8Array>[ 0, 0, 0, 0, 0 ]
    233 
    234 // ...
    235 
    236 typedarraypool.free( arr1 );
    237 typedarraypool.free( arr2 );
    238 ```
    239 
    240 #### typedarraypool.free( buf )
    241 
    242 Frees a [typed array][mdn-typed-array] or typed array [buffer][mdn-arraybuffer] for use in a future allocation.
    243 
    244 ```javascript
    245 var arr = typedarraypool( 10, 'float64' );
    246 // returns <Float64Array>
    247 
    248 // ...
    249 
    250 // Free the allocated typed array for use in a future allocation:
    251 typedarraypool.free( arr );
    252 
    253 // Create another typed array:
    254 arr = typedarraypool( 10, 'float64' );
    255 // returns <Float64Array>
    256 
    257 // ...
    258 
    259 // Free the allocated typed array buffer for use in a future allocation:
    260 typedarraypool.free( arr.buffer );
    261 ```
    262 
    263 #### typedarraypool.clear()
    264 
    265 Clears the [typed array][mdn-typed-array] pool allowing garbage collection of previously allocated (and currently free) [array buffers][mdn-arraybuffer].
    266 
    267 ```javascript
    268 var arr = typedarraypool( 10, 'float64' );
    269 // returns <Float64Array>
    270 
    271 // ...
    272 
    273 typedarraypool.free( arr );
    274 
    275 // ...
    276 
    277 // Clear all freed buffers:
    278 typedarraypool.clear();
    279 ```
    280 
    281 #### typedarraypool.highWaterMark
    282 
    283 **Read-only** property returning the pool's high water mark (in bytes).
    284 
    285 ```javascript
    286 var limit = typedarraypool.highWaterMark;
    287 // returns <number>
    288 ```
    289 
    290 Once a high water mark is reached, [typed array][mdn-typed-array] allocation **fails**.
    291 
    292 #### typedarraypool.nbytes
    293 
    294 **Read-only** property returning the total number of allocated bytes.
    295 
    296 ```javascript
    297 var arr = typedarraypool( 5, 'float64' );
    298 
    299 var nbytes = typedarraypool.nbytes;
    300 // returns <number>
    301 ```
    302 
    303 The returned value is the total **accumulated** value. Hence, anytime a pool must allocate a new [array buffer][mdn-arraybuffer] (i.e., more memory), the pool increments this value. The only time this value is decremented is when a pool is cleared. This behavior means that, while allocated buffers which are never freed may, in fact, be garbage collected, they continue to count against the high water mark limit. Accordingly, you should **always** free allocated buffers in order to prevent the pool from believing that non-freed buffers are continually in use.
    304 
    305 #### typedarraypool.factory( \[options] )
    306 
    307 Creates a new [typed array][mdn-typed-array] pool.
    308 
    309 ```javascript
    310 var pool = typedarraypool.factory();
    311 
    312 var arr = pool( 5, 'float64' );
    313 // returns <Float64Array>
    314 
    315 // ...
    316 
    317 pool.free( arr );
    318 ```
    319 
    320 The method accepts the following `options`:
    321 
    322 -   **highWaterMark**: maximum total memory (in bytes) which can be allocated. Default: `2^53` bytes.
    323 
    324 By default, the maximum total memory a pool may allocate is `2^53` bytes (approximately `1` petabyte, which, in practical terms, means a pool has **unlimited** capacity). To specify an alternative limit, set the `highWaterMark` option.
    325 
    326 ```javascript
    327 // Create a new typed array pool which can allocate up to 1MB:
    328 var pool = typedarraypool.factory({
    329     'highWaterMark': 1e6
    330 });
    331 
    332 var arr = pool( 5, 'float64' );
    333 // returns <Float64Array>
    334 
    335 // ...
    336 
    337 pool.free( arr );
    338 ```
    339 
    340 </section>
    341 
    342 <!-- /.usage -->
    343 
    344 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    345 
    346 <section class="notes">
    347 
    348 ## Notes
    349 
    350 -   Uninitialized typed arrays may contain sensitive contents. If security is paramount (e.g., if freed [typed arrays][mdn-typed-array] have been used to store sensitive contents), use `calloc`.
    351 -   An allocated [typed array][mdn-typed-array] is **guaranteed** to have an underlying [array buffer][mdn-arraybuffer] with _at least_ `N * w` bytes, where `N` is the number of [typed array][mdn-typed-array] elements and `w` is the number of bytes per element. Note, however, that the underlying [array buffer][mdn-arraybuffer] is likely to have **excess** capacity. Thus, if you create many [typed arrays][mdn-typed-array] which are held in memory and are **not** freed, you are likely to consume significantly more memory than if you had directly used [typed array][mdn-typed-array] constructors. However, if you create many [typed arrays][mdn-typed-array] which are rapidly discarded and of relatively large size, then using a [typed array][mdn-typed-array] pool can offer significant performance advantages.
    352 
    353 </section>
    354 
    355 <!-- /.notes -->
    356 
    357 <!-- Package usage examples. -->
    358 
    359 <section class="examples">
    360 
    361 ## Examples
    362 
    363 <!-- eslint no-undef: "error" -->
    364 
    365 ```javascript
    366 var randu = require( '@stdlib/random/base/randu' );
    367 var typedarraypool = require( '@stdlib/array/pool' ).factory;
    368 
    369 // Create a typed array pool which can allocate at most 1GB:
    370 var typedarray = typedarraypool({
    371     'highWaterMark': 1e9
    372 });
    373 
    374 // Inspect the pool:
    375 console.log( 'Max bytes: %d', typedarray.highWaterMark );
    376 console.log( 'nbytes: %d', typedarray.nbytes );
    377 
    378 // Allocate an array for storing double-precision floating-point numbers:
    379 var arr1 = typedarray( 5, 'float64' );
    380 // returns <Float64Array>[ 0.0, 0.0, 0.0, 0.0, 0.0 ]
    381 
    382 // Inspect the pool:
    383 console.log( 'nbytes: %d', typedarray.nbytes );
    384 
    385 // Fill the array...
    386 var i;
    387 for ( i = 0; i < arr1.length; i++ ) {
    388     arr1[ i ] = randu();
    389 }
    390 
    391 // Inspect array contents:
    392 console.log( arr1 );
    393 
    394 // Free the array:
    395 typedarray.free( arr1 );
    396 
    397 // Allocate another array similar to the previous one:
    398 var arr2 = typedarray( 5, 'float64' );
    399 // returns <Float64Array>
    400 
    401 // Check that we have been returned a new typed array view:
    402 console.log( arr2 === arr1 );
    403 // => false
    404 
    405 // Inspect array contents:
    406 console.log( arr2 );
    407 
    408 // Free the array:
    409 typedarray.free( arr2 );
    410 
    411 // Allocate an initialized array:
    412 var arr3 = typedarray.calloc( 5, 'float64' );
    413 // returns <Float64Array>[ 0.0, 0.0, 0.0, 0.0, 0.0 ]
    414 
    415 // Inspect array contents:
    416 console.log( arr3 );
    417 
    418 // Free the array:
    419 typedarray.free( arr3 );
    420 
    421 // Clear the pool:
    422 typedarray.clear();
    423 
    424 // Inspect the pool:
    425 console.log( 'nbytes: %d', typedarray.nbytes );
    426 ```
    427 
    428 </section>
    429 
    430 <!-- /.examples -->
    431 
    432 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    433 
    434 <section class="references">
    435 
    436 </section>
    437 
    438 <!-- /.references -->
    439 
    440 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    441 
    442 <section class="links">
    443 
    444 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    445 
    446 [mdn-arraybuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
    447 
    448 </section>
    449 
    450 <!-- /.links -->