What's the logic behind freeing the old buffer and re-allocating if the new length exceeds some limit (and why "1250"?)? For huge initial size, it should be insignificant if new content length is 1249 or 1250. In fact, the "1250" can be replaced with "1e8" (or "1e8 minus something small" if that matters) and again Perl re-allocates. I'd expect the opposite behaviour: if new content is the same or almost as big as container, then re-use the container. If it's couple of droplets in huge vessel, then replace the vessel.

use Devel::Peek qw( Dump ); $Devel::Peek::pv_limit = $Devel::Peek::pv_limit = 10; my $n; $_ = "x" x 1e8; $_ .= "x"; # Force unsharing. Dump( $_ ); $n = 1249; $_ = "x" x $n; Dump( $_ ); $_ = "x" x 1e8; $_ .= "x"; # Force unsharing. Dump( $_ ); $n = 1250; $_ = "x" x $n; Dump( $_ ); __END__ 5.042000 SV = PV(0x21d90461a70) at 0x21d9049cb58 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x21d9dd19060 "xxxxxxxxxx"...\0 CUR = 100000001 LEN = 100000002 SV = PV(0x21d90461a70) at 0x21d9049cb58 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x21d9dd19060 "xxxxxxxxxx"...\0 CUR = 1249 LEN = 100000002 SV = PV(0x21d90461a70) at 0x21d9049cb58 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x21d9dd1e060 "xxxxxxxxxx"...\0 CUR = 100000001 LEN = 100000002 SV = PV(0x21d90461a70) at 0x21d9049cb58 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x21d90497bd0 "xxxxxxxxxx"...\0 CUR = 1250 LEN = 1256

In reply to Re^6: [XS] sv_setpv change in behaviour with perl-5.42.0 and later by Anonymous Monk
in thread [XS] sv_setpv change in behaviour with perl-5.42.0 and later by syphilis

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.