From 8d5a2b776c0bcd54f74daf2876ae0bd37e87f7c0 Mon Sep 17 00:00:00 2001 From: Father Chrysostomos Date: Thu, 24 Nov 2011 01:16:32 -0800 Subject: [PATCH] Make COW-clobbering faster There is this special SV_COW_DROP_PV flag that gets passed to sv_force_normal_flags to signal that the SV is about to be clobbered, so there is no point in allocating a new PV. But this flag was not actually being used, until the previous commit commandeered it for globs (despite the name). Now it actually does what it says. Before and after: $ time ./perl -e '$x = __PACKAGE__, undef $x for 1..8000000' real 0m5.758s user 0m5.740s sys 0m0.008s $ time ./perl -e '$x = __PACKAGE__, undef $x for 1..8000000' real 0m3.290s user 0m3.282s sys 0m0.006s --- sv.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/sv.c b/sv.c index 44a8ba6..2582e5a 100644 --- a/sv.c +++ b/sv.c @@ -4794,9 +4794,14 @@ Perl_sv_force_normal_flags(pTHX_ register SV *const sv, const U32 flags) SvREADONLY_off(sv); SvPV_set(sv, NULL); SvLEN_set(sv, 0); - SvGROW(sv, len + 1); - Move(pvx,SvPVX(sv),len,char); - *SvEND(sv) = '\0'; + if (flags & SV_COW_DROP_PV) { + /* OK, so we don't need to copy our buffer. */ + SvPOK_off(sv); + } else { + SvGROW(sv, len + 1); + Move(pvx,SvPVX(sv),len,char); + *SvEND(sv) = '\0'; + } unshare_hek(SvSHARED_HEK_FROM_PV(pvx)); } else if (IN_PERL_RUNTIME) -- 2.7.4