Ah, yeah, it does now!  We wrote our module back on node 0.4, which, I 
think, did not have typed arrays.  That being said, it seems TypedArrays do 
not support un-aligned views (e.g. if you're reading a stream and want to 
read a F64 at byte offset 3, it throws an exception), as well they 
shouldn't, as that's not guaranteed to be efficient cross-platform, though 
is critical to performance in our specific situation.  Also, it doesn't 
seem you can make a typed array view off of a Buffer (or get an ArrayBuffer 
off of a network read?), so that involves some copying, although since 
creating views is a native call and a bit expensive, we found it quickest 
to copy any network-received Buffer into one that already has views created 
anyway.

Sorry, no public module for this, ours is pretty deeply entangled with a 
number of our other systems, and involves careful 
allocating/releasing/pooling of these buffers to be most efficient.  The 
gist of it is just to take a Buffer, for each type you want to read (say, 
e.g, 32-bit ints), make a number of views equal to the possible byte 
offsets (e.g. 4) and then do a read with the right offset from the right 
view (e.g. view[offset % 4][offset >> 2]).  V8 does "indexed properties to 
external data" lookups super-fast (Buffers use this), so this ends up being 
much faster than doing the JS operations to reconstruct a primitive type 
from multiple reads.

Disclaimer: I haven't done thorough speed testing on this since node 0.4, 
just some minor speed testing when Buffers had the readUInt32/etc methods 
added.  Also, I think my speed tests were concerned more with writing than 
reading, but it's basically the same code, so it's probably similar results 
;).

On Thursday, October 4, 2012 7:27:17 PM UTC-7, Ben Noordhuis wrote:
>
> On Thu, Oct 4, 2012 at 6:19 PM, Jimb Esser <[email protected]<javascript:>> 
> wrote: 
> > If you want *really* fast buffer access for larger integer types and 
> float 
> > types, you can set up a native module that calls 
> > SetIndexedPropertiesToExternalArrayData a few times on your Buffer and 
> gives 
> > you a native indexed view into the buffer for whatever primitive type 
> you 
> > want to read (much, much, much faster than trying to read a float 
> > byte-by-byte and assemble it).  We do this in our internal networking 
> > buffers, and got quite a speed-up compared to combining bytes in JS 
> (though 
> > either are *way* faster than calling into native methods to do the read, 
> > except maybe with floats).  Note: for un-aligned reads (e.g. reading a 
> > 64-bit float from 3 bytes within a buffer), I think this only works on 
> some 
> > platforms, but those platforms include x86 and x64, which is all we care 
> > about in our case. 
>
> You don't have to drop down to C++ for that, node.js supports typed 
> arrays[1]. I probably should have mentioned that in my other post. :-) 
>
> [1] https://developer.mozilla.org/en-US/docs/JavaScript_typed_arrays 
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to