develooper Front page | perl.perl5.porters | Postings from January 2003

patch to speed up Perl's slurp mode

Thread Next
From:
Enache Adrian
Date:
January 23, 2003 20:19
Subject:
patch to speed up Perl's slurp mode
Message ID:
20030124042354.GA30362@ratsnest.hole
Please take a look to the following patch. It greatly speeds
up reading files in 'slurp' mode when the user was blessed with
a stupid realloc - but is faster even with a good realloc and
on smaller files.
Note that it changes the sv_gets behaviour in record mode ( see my
previous question on this list ) to the one documented in perlapi.

Regards
Adi

------------------8x-------------------
--- /arc/perl-current/sv.c	2003-01-22 16:14:01.000000000 +0200
+++ perl-current/sv.c	2003-01-24 00:21:55.000000000 +0200
@@ -6153,6 +6153,7 @@
     register I32 cnt;
     I32 i = 0;
     I32 rspara = 0;
+    I32 recsize;
 
     SV_CHECK_THINKFIRST_COW_DROP(sv);
     /* XXX. If you make this PVIV, then copy on write can copy scalars read
@@ -6163,6 +6164,7 @@
     (void)SvUPGRADE(sv, SVt_PV);
 
     SvSCREAM_off(sv);
+    SvPOK_only(sv);    /* Validate pointer */
 
     if (PL_curcop == &PL_compiling) {
 	/* we always read code in line mode */
@@ -6170,17 +6172,22 @@
 	rslen = 1;
     }
     else if (RsSNARF(PL_rs)) {
+	Stat_t st;
+	if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && st.st_size
+		&& (recsize = st.st_size - PerlIO_tell(fp)))
+	    goto read_record;
 	rsptr = NULL;
 	rslen = 0;
     }
     else if (RsRECORD(PL_rs)) {
-      I32 recsize, bytesread;
+      I32 bytesread;
       char *buffer;
 
       /* Grab the size of the record we're getting */
       recsize = SvIV(SvRV(PL_rs));
-      (void)SvPOK_only(sv);    /* Validate pointer */
-      buffer = SvGROW(sv, (STRLEN)(recsize + 1));
+
+    read_record:
+      buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
       /* Go yank in */
 #ifdef VMS
       /* VMS wants read instead of fread, because fread doesn't respect */
@@ -6190,13 +6197,9 @@
 #else
       bytesread = PerlIO_read(fp, buffer, recsize);
 #endif
-      SvCUR_set(sv, bytesread);
+      SvCUR_set(sv, bytesread += append);
       buffer[bytesread] = '\0';
-      if (PerlIO_isutf8(fp))
-	SvUTF8_on(sv);
-      else
-	SvUTF8_off(sv);
-      return(SvCUR(sv) ? SvPVX(sv) : Nullch);
+      goto check_utf8_and_return;
     }
     else if (RsPARA(PL_rs)) {
 	rsptr = "\n\n";
@@ -6265,7 +6268,6 @@
     /* Here is some breathtakingly efficient cheating */
 
     cnt = PerlIO_get_cnt(fp);			/* get count into register */
-    (void)SvPOK_only(sv);		/* validate pointer */
     if ((I32)(SvLEN(sv) - append) <= cnt + 1) { /* make sure we have the room */
 	if (cnt > 80 && (I32)SvLEN(sv) > append) {
 	    shortbuffered = cnt - SvLEN(sv) + append + 1;
@@ -6445,6 +6447,7 @@
 	}
     }
 
+check_utf8_and_return:
     if (PerlIO_isutf8(fp))
 	SvUTF8_on(sv);
     else
------------------8x-------------------


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About