Hello wise monks, I'm trying to increase performance of my program (which uses a huge 3-dimensional hash - around 240MB in RAM) by moving it to a threaded model.

I though I'd explicitly share the big hash and then access the data from each thread (the operations I do are read only, so I'm not worried about individual threads conflicting).

However the process of creating the big shared hash takes ages for some reason - I've tried these two simplified versions for comparison:

my %data; foreach my $x (1..5000) { $data{$x} = {} unless $data{$x}; foreach my $y (1..1000) { $data{$x}{$y} = {} unless $data{$x}{$y}; } } real 0m4.075s user 0m3.767s sys 0m0.289s
vs the shared one:
use threads; use threads::shared; my %data:shared; foreach my $x (1..5000) { $data{$x} = &share( {} ) unless $data{$x}; foreach my $y (1..1000) { $data{$x}{$y} = &share( {} ) unless $data{$x}{$y}; } } real 1m4.984s user 1m4.211s ! that's ~16x times slower that the non-shared case sys 0m0.540s

Is there something wrong with my code or is this performance decrease simply an inevitable cost of sharing?


In reply to threads::shared seems to kill performance by Jacobs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.