Browse Source

optimize fpassthru/readfile to use mmap instead of fread

which especially increases speed on large files.
experimetnal/RETURN_REF_PATCH
Sascha Schumann 27 years ago
parent
commit
dda0b783df
  1. 4
      configure.in.in
  2. 74
      ext/standard/file.c

4
configure.in.in

@ -256,7 +256,7 @@ else
fi
AC_MISSING_FCLOSE_DECL
dnl QNX requires unix.h to allow functions in libunix to work properly
AC_CHECK_HEADERS(fcntl.h unistd.h crypt.h sys/file.h memory.h pwd.h grp.h sys/socket.h sys/wait.h syslog.h string.h sys/varargs.h stdarg.h sys/time.h signal.h netinet/in.h dlfcn.h limits.h sys/types.h unix.h arpa/inet.h locale.h sys/select.h sys/statvfs.h sys/statfs.h)
AC_CHECK_HEADERS(fcntl.h unistd.h crypt.h sys/file.h memory.h pwd.h grp.h sys/socket.h sys/wait.h syslog.h string.h sys/varargs.h stdarg.h sys/time.h signal.h netinet/in.h dlfcn.h limits.h sys/types.h unix.h arpa/inet.h locale.h sys/select.h sys/statvfs.h sys/statfs.h sys/mman.h)
dnl Checks for typedefs, structures, and compiler characteristics.
AC_STRUCT_TM
@ -314,7 +314,7 @@ AC_CHECK_TYPE( ulong, unsigned long )
dnl Checks for library functions.
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(memcpy memmove strdup strerror strcasecmp strstr flock lockf putenv tempnam usleep setlocale gettimeofday setvbuf srand48 lrand48 srandom random link symlink regcomp getlogin cuserid vsnprintf snprintf gcvt utime crypt rint setitimer unsetenv strftime setsockopt tzset shutdown inet_aton statvfs statfs)
AC_CHECK_FUNCS(memcpy memmove strdup strerror strcasecmp strstr flock lockf putenv tempnam usleep setlocale gettimeofday setvbuf srand48 lrand48 srandom random link symlink regcomp getlogin cuserid vsnprintf snprintf gcvt utime crypt rint setitimer unsetenv strftime setsockopt tzset shutdown inet_aton statvfs statfs mmap)
AC_FUNC_UTIME_NULL
AC_FUNC_ALLOCA
dnl## OLDLIBS=$LIBS; LIBS=""

74
ext/standard/file.c

@ -56,11 +56,11 @@
# include <sys/time.h>
#endif
#if WIN32|WINNT
# include <winsock.h>
#include <winsock.h>
#else
# include <netinet/in.h>
# include <netdb.h>
# include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#endif
#include "snprintf.h"
#include "fsock.h"
@ -68,13 +68,17 @@
#include "php_globals.h"
#ifdef HAVE_SYS_FILE_H
# include <sys/file.h>
#include <sys/file.h>
#endif
#if MISSING_FCLOSE_DECL
extern int fclose();
#endif
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
static void _php3_closesocket(int *);
#ifndef THREAD_SAFE
@ -1212,16 +1216,56 @@ PHP_FUNCTION(rmdir)
}
/* }}} */
static size_t php_passthru_fd(int socketd, FILE *fp, int issock)
{
size_t bcount = 0;
int ready = 0;
#ifdef HAVE_MMAP
if(!issock) {
int fd;
struct stat sbuf;
off_t off;
void *p;
size_t len;
fd = fileno(fp);
off = ftell(fp);
fstat(fd, &sbuf);
len = sbuf.st_size - off;
p = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, off);
if(p!=MAP_FAILED) {
PHPWRITE(p, len);
munmap(p, len);
bcount += len;
ready = 1;
}
}
#endif
if(!ready) {
char buf[8192];
int b;
while ((b = FP_FREAD(buf, sizeof(buf), socketd, fp, issock)) > 0) {
PHPWRITE(buf, b);
bcount += b;
}
}
return bcount;
}
/* {{{ proto int readfile(string filename [, int use_include_path])
Output a file or a URL */
PHP_FUNCTION(readfile)
{
pval *arg1, *arg2;
char buf[8192];
FILE *fp;
int b, size;
int use_include_path = 0;
int size=0;
int use_include_path=0;
int issock=0, socketd=0;
/* check args */
@ -1255,10 +1299,8 @@ PHP_FUNCTION(readfile)
}
RETURN_FALSE;
}
size = 0;
while ((b = FP_FREAD(buf, sizeof(buf), socketd, fp, issock)) > 0) {
PHPWRITE(buf, b);
size += b;
if(php3_header()) {
size = php_passthru_fd(socketd, fp, issock);
}
if (issock) {
SOCK_FCLOSE(socketd);
@ -1304,8 +1346,7 @@ PHP_FUNCTION(fpassthru)
{
pval *arg1;
FILE *fp;
char buf[8192];
int id, size, b, type;
int id, size, type;
int issock=0;
int socketd=0, *sock;
@ -1326,10 +1367,7 @@ PHP_FUNCTION(fpassthru)
}
size = 0;
if (php3_header()) { /* force headers if not already sent */
while ((b = FP_FREAD(buf, sizeof(buf), socketd, fp, issock)) > 0) {
PHPWRITE(buf,b);
size += b ;
}
size = php_passthru_fd(socketd, fp, issock);
}
php3_list_delete(id);
RETURN_LONG(size);

Loading…
Cancel
Save