Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: [OT:] Is this Curriculum right?

by marioroy (Prior)
on Nov 25, 2021 at 00:40 UTC ( [id://11139095]=note: print w/replies, xml ) Need Help??


in reply to [OT:] Is this Curriculum right?

Greetings, karlgoethebier

How fortunate for the kids to have you as a tutor.

Many years ago before releasing MCE::Shared::Ordhash, I attempted a doubly-linked list, pure-Perl ordered hash implementation. It was based on Tie::Hash::Indexed. It lives in my gist repo Indhash.pm, but never released to CPAN. That's because it consumed more memory then I had wished for.

My goal at the time was making an ordered hash implementation that was as memory efficient as Hash::Ordered but wished for performance closer to Tie::Hash::Indexed, particularly deletions. Well, reached that goal and released MCE::Shared::Ordhash.

In the event the curriculum requires a doubly-linked list, see the TIEHASH, STORE, FETCH, and DELETE routines in Tie::Hash::Indexed and Indhash.pm (gist link above).

Replies are listed 'Best First'.
Re^2: [OT:] Is this Curriculum right?
by marioroy (Prior) on Nov 25, 2021 at 08:16 UTC

    Tie::Hash::Indexed now works with recent Perl releases. Thank you, Slaven Rezić and @mhx. It has been a while and ran the bench script now that Tie::Hash::Indexed is working again. Look at Tie::Hash::Indexed go :). As mentioned in the prior post, MCE::Shared::Indhash lives in gist.

    bench_oh.pl in gist

    $ perl bench_oh.pl 1 # memory efficient and fast Tie::Hash::Indexed store : 0.858 secs. delete : 1.544 secs. elapse : 2.401 secs. $ perl bench_oh.pl 2 # not memory efficient, but fast for pure-Perl +impl. MCE::Shared::Indhash store : 1.271 secs. delete : 2.197 secs. elapse : 3.468 secs. $ perl bench_oh.pl 3 # memory efficient and fast for being pure-Perl MCE::Shared::Ordhash store : 1.048 secs. delete : 2.829 secs. elapse : 3.877 secs. $ perl bench_oh.pl 4 # memory efficient, but deletions taking longer Hash::Ordered store : 1.076 secs. delete : 4.037 secs. elapse : 5.113 secs.

    Sharing using the TIE interface.

    Locking is necessary when performing FETCH followed by STORE; i.e. ++.

    use strict; use warnings; use MCE::Hobo; use MCE::Shared; use Tie::Hash::Indexed; use Hash::Ordered; use MCE::Shared::Ordhash; tie my %hi, 'MCE::Shared', { module => 'Tie::Hash::Indexed' }, total = +> 0; tie my %ho, 'MCE::Shared', { module => 'Hash::Ordered' }, total => 0; tie my %oh, 'MCE::Shared', { module => 'MCE::Shared::Ordhash' }, total + => 0; sub task { my $id = shift; $hi{$id} = $id; tied(%hi)->lock; $hi{total}++; tied(%hi)->unlock; $ho{$id} = $id; tied(%ho)->lock; $ho{total}++; tied(%ho)->unlock; $oh{$id} = $id; tied(%oh)->lock; $oh{total}++; tied(%oh)->unlock; } MCE::Hobo->create('task', $_) for 1..4; MCE::Hobo->wait_all(); my (@k, @v); @k = keys %hi; print "hi keys: @k\n"; @k = keys %ho; print "ho keys: @k\n"; @k = keys %oh; print "oh keys: @k\n"; @v = values %hi; print "hi vals: @v\n"; @v = values %ho; print "ho vals: @v\n"; @v = values %oh; print "oh vals: @v\n"; __END__ hi keys: total 1 2 3 4 ho keys: total 1 2 3 4 oh keys: total 1 2 3 4 hi vals: 4 1 2 3 4 ho vals: 4 1 2 3 4 oh vals: 4 1 2 3 4

    Sharing using the OO interface.

    Locking isn't necessary when calling OO methods, further reducing latency.

    use strict; use warnings; use Tie::Hash::Indexed; use Hash::Ordered; use MCE::Shared::Ordhash; use MCE::Hobo; use MCE::Shared; my $hi = MCE::Shared->share({ module => 'Tie::Hash::Indexed' }, total +=> 0); my $ho = MCE::Shared->share({ module => 'Hash::Ordered' }, total => 0) +; my $oh = MCE::Shared->share({ module => 'MCE::Shared::Ordhash' }, tota +l => 0); sub task { my $id = shift; $hi->set($id => $id); $hi->preinc('total'); $ho->set($id => $id); $ho->preinc('total'); $oh->set($id => $id); $oh->incr('total'); } MCE::Hobo->create('task', $_) for 1..4; MCE::Hobo->wait_all(); my (@k, @v); @k = $hi->keys; print "hi keys: @k\n"; @k = $ho->keys; print "ho keys: @k\n"; @k = $oh->keys; print "oh keys: @k\n"; @v = $hi->values; print "hi vals: @v\n"; @v = $ho->values; print "ho vals: @v\n"; @v = $oh->values; print "oh vals: @v\n"; __END__ hi keys: total 1 2 3 4 ho keys: total 1 2 3 4 oh keys: total 1 2 3 4 hi vals: 4 1 2 3 4 ho vals: 4 1 2 3 4 oh vals: 4 1 2 3 4

    I'm delighted that Tie::Hash::Indexed is working again, plus includes complementary OO methods. The module may be shared and works great.

    Update: I encountered a bug with Hash::Ordered. It's an old issue from 2016. Added a comment to issue #6.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139095]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-03-29 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found