Mercurial > hg > octave-nkf > gnulib-hg
comparison lib/copy-file.c @ 12198:e8fbc5424b99
digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
This results in a significant decrease in syscall overhead
giving a 3% speedup to the digest utilities for example
(when processing large files from cache).
Storage is moved from the stack to the heap as some
threaded environments for example can have small stacks.
* lib/copy-file.c (copy_file_preserving): Use a 32KiB malloced buffer
* modules/copy-file: Depend on xalloc
* lib/md2.c: Likewise
* lib/md4.c: Likewise
* lib/md5.c: Likewise
* lib/sha1.c: Likewise
* lib/sha256.c: Likewise
* lib/sha512.c: Likewise
author | Pádraig Brady <P@draigBrady.com> |
---|---|
date | Thu, 22 Oct 2009 17:36:28 +0100 |
parents | bbbbbf4cd1c5 |
children | e8d2c6fc33ad |
comparison
equal
deleted
inserted
replaced
12197:e45d9bb2233e | 12198:e8fbc5424b99 |
---|---|
40 #include "safe-read.h" | 40 #include "safe-read.h" |
41 #include "full-write.h" | 41 #include "full-write.h" |
42 #include "acl.h" | 42 #include "acl.h" |
43 #include "binary-io.h" | 43 #include "binary-io.h" |
44 #include "gettext.h" | 44 #include "gettext.h" |
45 #include "xalloc.h" | |
45 | 46 |
46 #define _(str) gettext (str) | 47 #define _(str) gettext (str) |
47 | 48 |
48 /* The results of open() in this file are not used with fchdir, | 49 /* The results of open() in this file are not used with fchdir, |
49 therefore save some unnecessary work in fchdir.c. */ | 50 therefore save some unnecessary work in fchdir.c. */ |
50 #undef open | 51 #undef open |
51 #undef close | 52 #undef close |
52 | 53 |
54 enum { IO_SIZE = 32 * 1024 }; | |
53 | 55 |
54 void | 56 void |
55 copy_file_preserving (const char *src_filename, const char *dest_filename) | 57 copy_file_preserving (const char *src_filename, const char *dest_filename) |
56 { | 58 { |
57 int src_fd; | 59 int src_fd; |
58 struct stat statbuf; | 60 struct stat statbuf; |
59 int mode; | 61 int mode; |
60 int dest_fd; | 62 int dest_fd; |
61 char buf[4096]; | 63 char *buf = xmalloc (IO_SIZE); |
62 const size_t buf_size = sizeof (buf); | |
63 | 64 |
64 src_fd = open (src_filename, O_RDONLY | O_BINARY); | 65 src_fd = open (src_filename, O_RDONLY | O_BINARY); |
65 if (src_fd < 0 || fstat (src_fd, &statbuf) < 0) | 66 if (src_fd < 0 || fstat (src_fd, &statbuf) < 0) |
66 error (EXIT_FAILURE, errno, _("error while opening \"%s\" for reading"), | 67 error (EXIT_FAILURE, errno, _("error while opening \"%s\" for reading"), |
67 src_filename); | 68 src_filename); |
74 dest_filename); | 75 dest_filename); |
75 | 76 |
76 /* Copy the file contents. */ | 77 /* Copy the file contents. */ |
77 for (;;) | 78 for (;;) |
78 { | 79 { |
79 size_t n_read = safe_read (src_fd, buf, buf_size); | 80 size_t n_read = safe_read (src_fd, buf, IO_SIZE); |
80 if (n_read == SAFE_READ_ERROR) | 81 if (n_read == SAFE_READ_ERROR) |
81 error (EXIT_FAILURE, errno, _("error reading \"%s\""), src_filename); | 82 error (EXIT_FAILURE, errno, _("error reading \"%s\""), src_filename); |
82 if (n_read == 0) | 83 if (n_read == 0) |
83 break; | 84 break; |
84 | 85 |
85 if (full_write (dest_fd, buf, n_read) < n_read) | 86 if (full_write (dest_fd, buf, n_read) < n_read) |
86 error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename); | 87 error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename); |
87 } | 88 } |
89 | |
90 free (buf); | |
88 | 91 |
89 #if !USE_ACL | 92 #if !USE_ACL |
90 if (close (dest_fd) < 0) | 93 if (close (dest_fd) < 0) |
91 error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename); | 94 error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename); |
92 if (close (src_fd) < 0) | 95 if (close (src_fd) < 0) |