in reply to Re: Re: Hash keyed on ranges
in thread Hash keyed on ranges

Assuming "a few thousand" is less than say low tens of thousands, then using an array, and storing the (same)object in each element of the range would certainly be the fastest method. The limitation being the memory consumption if your array is likely to get very large.

{ my array; sub set{ my( $frag, $start, @meta ) = @_; @array[ $start .. ( $start + length( $freg ) -1 ) ] = ( Fragment->new( $frag, @meta ) ) x length( $frag ); return $array[ $start ]; } sub find{ my( $offset ) = @_; return $array[ $offset ]; } } ... set( 'FOO', 0, ... ); set( 'QUUX', 3, ... ); set( 'BAR', 7, ... ); ... my $found = find( 5 ); print $found->value; # prints 'QUUX';

If that consumes too much space, then you could use a scalar to represent the array (in less space) and an array to store the objects.

{ my $pseudo; my @frags; sub set{ my( $frag, $offset, @meta ) = @_; push @frags, Fragment->new( $frag, @meta ); $pseudo .= pack( 'v', $#frags ) x length( $frag ); return $frags[ -1 ]; } sub find{ my $offset = @_; my $index = unpack( 'v', substr( $pseudo, $offset, 2 ) ); return $frags[ $index ]; } }

With this, you push the objects onto an array once for each fragment, and then store the index at which you pushed it, converted to it's binary representation, once for each position it represents.

This way is also very fast but reduces the memory consumption. A fragment that contains 100 characters requires about 200 + 24 bytes of storage, rather than 100 * 24 bytes. The limitation is that using a 16-bit integer to hold the indexes limits you to 64K fragments. You could move to 32-bits whch would probably cover mosts needs, with the obvious increase in memory usage.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail