This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don't fbm_compile() studied scalars, to give more flexibility in SV flag usage.
authorNicholas Clark <nick@ccl4.org>
Mon, 23 May 2011 20:52:40 +0000 (21:52 +0100)
committerNicholas Clark <nick@ccl4.org>
Sat, 11 Jun 2011 07:40:01 +0000 (09:40 +0200)
No real-world code would ever end up using a studied scalar as a compile-time
second argument to index, so this isn't a real pessimisation.

ext/Devel-Peek/t/Peek.t
util.c

index 7caccd0..16bfba4 100644 (file)
@@ -842,7 +842,7 @@ unless ($Config{useithreads}) {
 
     do_test('regular string constant', beer,
 'SV = PV\\($ADDR\\) at $ADDR
-  REFCNT = 4
+  REFCNT = 5
   FLAGS = \\(PADMY,POK,READONLY,pPOK\\)
   PV = $ADDR "foamy"\\\0
   CUR = 5
@@ -853,7 +853,23 @@ unless ($Config{useithreads}) {
 
     do_test('string constant now studied', beer,
 'SV = PVMG\\($ADDR\\) at $ADDR
-  REFCNT = 5
+  REFCNT = 6
+  FLAGS = \\(PADMY,SMG,POK,READONLY,pPOK,SCREAM\\)
+  IV = 0
+  NV = 0
+  PV = $ADDR "foamy"\\\0
+  CUR = 5
+  LEN = \d+
+  MAGIC = $ADDR
+    MG_VIRTUAL = &PL_vtbl_mglob
+    MG_TYPE = PERL_MAGIC_regex_global\\(g\\)
+');
+
+    is (eval 'index "not too foamy", beer', 8, 'correct index');
+
+    do_test('string constant still studied', beer,
+'SV = PVMG\\($ADDR\\) at $ADDR
+  REFCNT = 6
   FLAGS = \\(PADMY,SMG,POK,READONLY,pPOK,SCREAM\\)
   IV = 0
   NV = 0
diff --git a/util.c b/util.c
index 1c90fb0..1c68f3d 100644 (file)
--- a/util.c
+++ b/util.c
@@ -552,6 +552,13 @@ Perl_fbm_compile(pTHX_ SV *sv, U32 flags)
 
     PERL_ARGS_ASSERT_FBM_COMPILE;
 
+    /* Refuse to fbm_compile a studied scalar, as this gives more flexibility in
+       SV flag usage.  No real-world code would ever end up using a studied
+       scalar as a compile-time second argument to index, so this isn't a real
+       pessimisation.  */
+    if (SvSCREAM(sv))
+       return;
+
     if (flags & FBMcf_TAIL) {
        MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
        sv_catpvs(sv, "\n");            /* Taken into account in fbm_instr() */