Skip to content

Commit b53d1dc

Browse files
committed
in sv_gets() if the sv is empty then preallocate a small buffer
When the COW logic is triggered there is an unfortunate slowdown. https://rt.perl.org/Ticket/Display.html?id=121796 What happens is that the buffer gets "stolen" by COW, and $_ ends up with SvLEN()==0, which through a tortorious pathway leads us to allocate the same amount of memory as there is in the current buffer. Which then gets COW'ed out, and etc. By initializing the string to at least something this does not happen. sv_gets() seriously needs love. :-(
1 parent a795f28 commit b53d1dc

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

sv.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
char *gconvert(double, int, int, char *);
4747
#endif
4848

49+
#define PERL_PREALLOC_EMPTY_IN_SV_GETS 20
50+
4951
#ifdef PERL_NEW_COPY_ON_WRITE
5052
# ifndef SV_COW_THRESHOLD
5153
# define SV_COW_THRESHOLD 0 /* min string length for cow */
@@ -8154,6 +8156,10 @@ Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
81548156
(*fp)->_cnt++;
81558157
#endif
81568158

8159+
if (!SvLEN(sv)) {
8160+
SvGROW(sv,PERL_PREALLOC_EMPTY_IN_SV_GETS);
8161+
}
8162+
81578163
/* Here is some breathtakingly efficient cheating */
81588164

81598165
cnt = PerlIO_get_cnt(fp); /* get count into register */

0 commit comments

Comments
 (0)