This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #121259] Always allow COW after $input=<>
authorFather Chrysostomos <sprout@cpan.org>
Mon, 17 Feb 2014 06:25:27 +0000 (22:25 -0800)
committerFather Chrysostomos <sprout@cpan.org>
Sat, 22 Feb 2014 14:20:02 +0000 (06:20 -0800)
sv_grow provides an extra byte for the sake of copy-on-write’s buffer
reference count, but skips this for multiples of 256 (though the com-
ments there say powers of two).

When we do

    $input=<>

The assignment is optimised away and the allocation takes place in
sv_gets.  In that code path, we know we don’t need a nice power of
two and allocating an extra byte won’t hurt, so go ahead and add an
extra byte.

This speeds up code doing m//g on $input, because it avoids the pre-
match copy.

sv.c

diff --git a/sv.c b/sv.c
index 0ebdb3c..655a8ad 100644 (file)
--- a/sv.c
+++ b/sv.c
@@ -8066,7 +8066,13 @@ Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
        if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode))  {
            const Off_t offset = PerlIO_tell(fp);
            if (offset != (Off_t) -1 && st.st_size + append > offset) {
-               (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
+               (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1
+#ifdef PERL_NEW_COPY_ON_WRITE
+                       /* Add an extra byte for the sake of copy-on-
+                          write's buffer reference count. */
+                                   + 1
+#endif
+               ));
            }
        }
        rsptr = NULL;