If I had answers, this might make a good meditation. Since I have mostly questions, it's a SoPW.

It has become fairly popular to chain calls to map, kind of like piping commands together. When I see those constructs, I wonder whether Perl is doing anything clever to avoid unnecessary copying, or if each call to map really does construct a new list, which the next map goes through to construct yet another list. How much memory overhead are we talking about?

After some pondering, it occurred to me that a construct like

map { BLOCK2; (return_list2) } map { BLOCK1; (return_list1) } @input;
is functionally equivalent to
map { BLOCK1; local $_ = $_; map { BLOCK2; (return_list2) } (return_li +st1) } @input
but the BLOCK2 map is called for each return_list1, rather than once on a list that is @input times as big. The actual BLOCK2 code gets executed the same number of times. So the questions are:
  1. Does perl do anything clever regarding memory management in chained maps?
  2. Is my equivalence correct? Dangerous? Beneficial?
  3. If beneficial, could perl optimize chained maps into nested maps internally?

We're not really tightening our belts, it just feels that way because we're getting fatter.

In reply to map: chaining vs. nesting by Roy Johnson

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.