You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Gunther Birznieks <gu...@extropia.com> on 2000/06/06 18:34:35 UTC

Re: Data structure question

Using tied hashes, you could conceivably make your own ordered hash class 
and use that as the data structure you return. You'd still basically have 
two data structures (for performance) but the fact that it is two data 
structures would be hidden behind the tied hash which would be programmed 
to iterate the keys using the array rather than the keys function on the 
hash part.

I think there is source code for this publicly available, but I forget 
where I saw it. You can get some docs from perldoc perltie though.

At 12:39 PM 6/6/00 -0400, Drew Taylor wrote:
>Hello,
>
>This doesn't directly relate to mod_perl, but I'd like to make this as
>memory efficient as possible since it runs under mod_perl. :-)
>
>I have a question about data structures. Currently, I am doing SQL
>queries and returning an array ref and a hash ref. The array is to
>preserve order, and the hash contains various bits of data about that
>particular product ( it could be another hash ref, and often is). After
>getting the two references, I use foreach to loop through the array, and
>within that loop I access various data from the hash where the productID
>is the key. It looks like this:
>
>Common.pm:
>sub getdata {
>    my $AR = [123, 456, 243, ... ]
>    my $HR = { 123 => {foo=>bar, name=>'name', price=>'123'}, ... }
>    return ($AR, $HR);
>}
>
>Otherstuff.pm:
>my ($AR, $HR) = $self->getdata();
>foreach (@{$AR}) {
>    my $name = $HR->{$_}{name};
>    ...
>}
>
>I would like to return a single data structure, but order IS important
>(hence the current setup). I was thinking of using an array, where each
>element is a hash reference. So I would return something like this:
>
>[ {ID=>123, name=>'name123', foor=>'bar'},  {ID=>321, name=>'name321',
>bar=>'foo'}, ... ]
>
>Are there any de-referenceing issues (performance wise) that would make
>this less efficient than the 2 structures? TIA for any pointers.
>
>--
>Drew Taylor
>Vialogix Communications, Inc.
>501 N. College Street
>Charlotte, NC 28202
>704 370 0550
>http://www.vialogix.com/


Re: Data structure question

Posted by Drew Taylor <dt...@vialogix.com>.
Eric Cholet wrote:
> 
> > Using tied hashes, you could conceivably make your own ordered hash class
> > and use that as the data structure you return. You'd still basically have
> > two data structures (for performance) but the fact that it is two data
> > structures would be hidden behind the tied hash which would be programmed
> > to iterate the keys using the array rather than the keys function on the
> > hash part.
> >
> > I think there is source code for this publicly available, but I forget
> > where I saw it. You can get some docs from perldoc perltie though.
> 
> Tie::IxHash
How much overhead does this module impose? I've heard about it in my
readings, but never looked into it very much. Does anyone have
experience using Tie::IxHash?

-- 
Drew Taylor
Vialogix Communications, Inc.
501 N. College Street
Charlotte, NC 28202
704 370 0550
http://www.vialogix.com/

Re: Data structure question

Posted by Drew Taylor <dt...@vialogix.com>.
Stas Bekman wrote:
> 
> and in perl5.6 it's called pseudohash (well it was known before but is
> supported in 5.6)
> http://www.perl.com/pub/doc/manual/html/pod/perldelta.html#Pseudo_hashes_are_supported
I know about pseudohashes - thanks to Damien again! :-). They look very
cool, but to be honest I'm afraid to implement them yet until I've had
time to play with them more.

> also take a look at this:
> Building a Better Hash
> http://www.dfan.org/real/tpj_hash.html
I'll definately take a look at this article.

-- 
Drew Taylor
Vialogix Communications, Inc.
501 N. College Street
Charlotte, NC 28202
704 370 0550
http://www.vialogix.com/

Re: Data structure question

Posted by Stas Bekman <sb...@stason.org>.
On Tue, 6 Jun 2000, Eric Cholet wrote:

> > Using tied hashes, you could conceivably make your own ordered hash class
> > and use that as the data structure you return. You'd still basically have
> > two data structures (for performance) but the fact that it is two data
> > structures would be hidden behind the tied hash which would be programmed
> > to iterate the keys using the array rather than the keys function on the
> > hash part.
> >
> > I think there is source code for this publicly available, but I forget
> > where I saw it. You can get some docs from perldoc perltie though.
> 
> 
> Tie::IxHash

and in perl5.6 it's called pseudohash (well it was known before but is
supported in 5.6) 
http://www.perl.com/pub/doc/manual/html/pod/perldelta.html#Pseudo_hashes_are_supported

also take a look at this:
Building a Better Hash
http://www.dfan.org/real/tpj_hash.html


_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide 
mailto:stas@stason.org   http://perl.org     http://stason.org/TULARC
http://singlesheaven.com http://perlmonth.com http://sourcegarden.org


Re: Data structure question

Posted by Eric Cholet <ch...@logilune.com>.
> Using tied hashes, you could conceivably make your own ordered hash class
> and use that as the data structure you return. You'd still basically have
> two data structures (for performance) but the fact that it is two data
> structures would be hidden behind the tied hash which would be programmed
> to iterate the keys using the array rather than the keys function on the
> hash part.
>
> I think there is source code for this publicly available, but I forget
> where I saw it. You can get some docs from perldoc perltie though.


Tie::IxHash

--
Eric



Re: Data structure question

Posted by Drew Taylor <dt...@vialogix.com>.
Stas Bekman wrote:
> 
> On Tue, 6 Jun 2000, Perrin Harkins wrote:
> 
> > On Tue, 6 Jun 2000, Drew Taylor wrote:
> > > I know about tied hashes - Thanks Damien for your excellent book! - but
> > > there is a performance penalty. How big is this penalty? Is it worth
> > > using tied hashes? Versus an array of hash refs?
> >
> > They're a lot slower than normal data structures, or even normal object
> > methods.  Whether that slowness will be noticeable next to the slowness of
> > accessing a database is questionable.  There were a few benchmarks posted
> > to this list that you could dig out of the archive.
I knew they were slower. I'll look for the benchmarks in the archives.
Unless I find something really cool that justifies tie()ing, I'm just
going to go withmy original idea. The DB already gives me the order I
want - I just need to transfer it. But I really like the ideas behind
tie'ing things. You can do some really neat stuff behind the scenes. :-)

> If you are going to run it with apache benchmarks try a fresh version of
> Apache::Benchmark
> http://stason.org/works/modules/Apache-Benchmark-0.01.tar.gz
> 
> ... just plugged this note about Apache::Benchmark so you'd go grab and
> try the package before I release it...
Nothing like a good plug. ;-)

-- 
Drew Taylor
Vialogix Communications, Inc.
501 N. College Street
Charlotte, NC 28202
704 370 0550
http://www.vialogix.com/

Re: Data structure question

Posted by Stas Bekman <sb...@stason.org>.
On Tue, 6 Jun 2000, Perrin Harkins wrote:

> On Tue, 6 Jun 2000, Drew Taylor wrote:
> > I know about tied hashes - Thanks Damien for your excellent book! - but
> > there is a performance penalty. How big is this penalty? Is it worth
> > using tied hashes? Versus an array of hash refs?
> 
> They're a lot slower than normal data structures, or even normal object
> methods.  Whether that slowness will be noticeable next to the slowness of
> accessing a database is questionable.  There were a few benchmarks posted
> to this list that you could dig out of the archive.

If you are going to run it with apache benchmarks try a fresh version of
Apache::Benchmark
http://stason.org/works/modules/Apache-Benchmark-0.01.tar.gz

but actually there is no reason, Benchmark is perfect for that... I have
posted a few examples, so you can roll your own benchmark. 
  perldoc Benchmark
will be no less helpful

... just plugged this note about Apache::Benchmark so you'd go grab and
try the package before I release it...

_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide 
mailto:stas@stason.org   http://perl.org     http://stason.org/TULARC
http://singlesheaven.com http://perlmonth.com http://sourcegarden.org


Re: Data structure question

Posted by Perrin Harkins <pe...@primenet.com>.
On Tue, 6 Jun 2000, Drew Taylor wrote:
> I know about tied hashes - Thanks Damien for your excellent book! - but
> there is a performance penalty. How big is this penalty? Is it worth
> using tied hashes? Versus an array of hash refs?

They're a lot slower than normal data structures, or even normal object
methods.  Whether that slowness will be noticeable next to the slowness of
accessing a database is questionable.  There were a few benchmarks posted
to this list that you could dig out of the archive.

- Perrin


Re: Data structure question

Posted by Drew Taylor <dt...@vialogix.com>.
Gunther Birznieks wrote:
> 
> Using tied hashes, you could conceivably make your own ordered hash class
> and use that as the data structure you return. You'd still basically have
> two data structures (for performance) but the fact that it is two data
> structures would be hidden behind the tied hash which would be programmed
> to iterate the keys using the array rather than the keys function on the
> hash part.
> 
> I think there is source code for this publicly available, but I forget
> where I saw it. You can get some docs from perldoc perltie though.
I know about tied hashes - Thanks Damien for your excellent book! - but
there is a performance penalty. How big is this penalty? Is it worth
using tied hashes? Versus an array of hash refs?

> At 12:39 PM 6/6/00 -0400, Drew Taylor wrote:
> >Hello,
> >
> >This doesn't directly relate to mod_perl, but I'd like to make this as
> >memory efficient as possible since it runs under mod_perl. :-)
> >
> >I have a question about data structures. Currently, I am doing SQL
> >queries and returning an array ref and a hash ref. The array is to
> >preserve order, and the hash contains various bits of data about that
> >particular product ( it could be another hash ref, and often is). After
> >getting the two references, I use foreach to loop through the array, and
> >within that loop I access various data from the hash where the productID
> >is the key. It looks like this:
> >
> >Common.pm:
> >sub getdata {
> >    my $AR = [123, 456, 243, ... ]
> >    my $HR = { 123 => {foo=>bar, name=>'name', price=>'123'}, ... }
> >    return ($AR, $HR);
> >}
> >
> >Otherstuff.pm:
> >my ($AR, $HR) = $self->getdata();
> >foreach (@{$AR}) {
> >    my $name = $HR->{$_}{name};
> >    ...
> >}
> >
> >I would like to return a single data structure, but order IS important
> >(hence the current setup). I was thinking of using an array, where each
> >element is a hash reference. So I would return something like this:
> >
> >[ {ID=>123, name=>'name123', foor=>'bar'},  {ID=>321, name=>'name321',
> >bar=>'foo'}, ... ]
> >
> >Are there any de-referenceing issues (performance wise) that would make
> >this less efficient than the 2 structures? TIA for any pointers.
> >
> >--
> >Drew Taylor
> >Vialogix Communications, Inc.
> >501 N. College Street
> >Charlotte, NC 28202
> >704 370 0550
> >http://www.vialogix.com/

-- 
Drew Taylor
Vialogix Communications, Inc.
501 N. College Street
Charlotte, NC 28202
704 370 0550
http://www.vialogix.com/