in reply to Re: "Auto" vivifying shared data structures
in thread "Auto" vivifying shared data structures

Just to reinforce the point, could you give some general information of how much extra space is required to share a structure.

Eg. If a structure takes 10Kb as a non-shared entity, how much would it cost to share this with say 2 other threads (3 total)? Is it 30Kb, 40Kb, 50Kb extra?

Also, if you change a leaf (scalar) within that structure, how much slower is that than in a non-shared equivalent?

Finally, does the hit of updating the other copies of the modified value come

  • all at once in the context of the thread that makes the modification?
  • Or incrementally as the other sharing threads get a timeslice?
  • Or does the sharing thread have to defer until each of the other threads get a timeslice in order to update their copies?

    If the latter is the case, does the time the modifying thread waits for the others threads to update depend on the vagaries of the scheduler calling those threads (in competition with all other threads in the system), before the modifying thread can continue and unlock the modified scalar?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
    • Comment on Re^2: "Auto" vivifying shared data structures
  • Replies are listed 'Best First'.
    Re^3: "Auto" vivifying shared data structures
    by dave_the_m (Monsignor) on Nov 17, 2005 at 22:58 UTC
      Very roughly speaking, a shared variable is implemented internally using a form of tieing. Each thread has its own copy of the tied variable, and the real value is stored in a separate area of shared memory. When you read a variable, the tied code gets called (XS rather than perl code), which locks the real shared value, then copies its value into the thread's private address space. Similary a write to a shared variable involves copying the private value into the shared variable. So in a worst-case scenario:
      my $s : shared = 'a' x 10_000; for (1..100) { async { my $l = length $s; sleep } }
      would consume about 1Mb, with each thread having a cached copy of the string at the point it was accessed.

      Nested structures are more complicated than that, but roughly speaking the worst-case isn't as bad as the scalar case above.

      Dave.