aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2017-08-18 20:36:10 -0700
committerPaul Eggert2017-08-18 20:37:31 -0700
commite73691e1a47834aff367c9131fc3c7d78751d821 (patch)
treec1cf3cd0d4e99b8edaba6c13d53c5d762e0dc7e2
parente66e81679c3c91d6bf8f62c7abcd968430b4d1fe (diff)
downloademacs-e73691e1a47834aff367c9131fc3c7d78751d821.tar.gz
emacs-e73691e1a47834aff367c9131fc3c7d78751d821.zip
Improve make-temp-file performance on local files
* lisp/files.el (make-temp-file): Let make-temp-file-internal do the work of inserting the text. * src/fileio.c (Fmake_temp_file_internal): New arg TEXT. All callers changed.
-rw-r--r--lisp/files.el20
-rw-r--r--src/fileio.c22
-rw-r--r--src/filelock.c2
3 files changed, 25 insertions, 19 deletions
diff --git a/lisp/files.el b/lisp/files.el
index a2b474f8d2b..0311cc6d210 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -1404,24 +1404,20 @@ of PREFIX, and expanding against `temporary-file-directory' if necessary),
1404is guaranteed to point to a newly created file. 1404is guaranteed to point to a newly created file.
1405You can then use `write-region' to write new data into the file. 1405You can then use `write-region' to write new data into the file.
1406 1406
1407If TEXT is non-nil, it will be inserted in the new file. Otherwise
1408the file will be empty.
1409
1410If DIR-FLAG is non-nil, create a new empty directory instead of a file. 1407If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1411 1408
1412If SUFFIX is non-nil, add that at the end of the file name." 1409If SUFFIX is non-nil, add that at the end of the file name.
1410
1411If TEXT is a string, insert it into the new file; DIR-FLAG should be nil.
1412Otherwise the file will be empty."
1413 (let ((absolute-prefix 1413 (let ((absolute-prefix
1414 (if (or (zerop (length prefix)) (member prefix '("." ".."))) 1414 (if (or (zerop (length prefix)) (member prefix '("." "..")))
1415 (concat (file-name-as-directory temporary-file-directory) prefix) 1415 (concat (file-name-as-directory temporary-file-directory) prefix)
1416 (expand-file-name prefix temporary-file-directory))) 1416 (expand-file-name prefix temporary-file-directory))))
1417 (contents (if (stringp text) text "")))
1418 (if (find-file-name-handler absolute-prefix 'write-region) 1417 (if (find-file-name-handler absolute-prefix 'write-region)
1419 (files--make-magic-temp-file absolute-prefix dir-flag suffix contents) 1418 (files--make-magic-temp-file absolute-prefix dir-flag suffix text)
1420 (let ((file (make-temp-file-internal absolute-prefix 1419 (make-temp-file-internal absolute-prefix
1421 (if dir-flag t) (or suffix "")))) 1420 (if dir-flag t) (or suffix "") text))))
1422 (when (and (stringp text) (not dir-flag))
1423 (write-region contents nil file nil 'silent))
1424 file))))
1425 1421
1426(defun files--make-magic-temp-file (absolute-prefix 1422(defun files--make-magic-temp-file (absolute-prefix
1427 &optional dir-flag suffix text) 1423 &optional dir-flag suffix text)
diff --git a/src/fileio.c b/src/fileio.c
index 1b832be344d..6b3bdf2154d 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -657,18 +657,20 @@ In Unix-syntax, this function just removes the final slash. */)
657} 657}
658 658
659DEFUN ("make-temp-file-internal", Fmake_temp_file_internal, 659DEFUN ("make-temp-file-internal", Fmake_temp_file_internal,
660 Smake_temp_file_internal, 3, 3, 0, 660 Smake_temp_file_internal, 4, 4, 0,
661 doc: /* Generate a new file whose name starts with PREFIX, a string. 661 doc: /* Generate a new file whose name starts with PREFIX, a string.
662Return the name of the generated file. If DIR-FLAG is zero, do not 662Return the name of the generated file. If DIR-FLAG is zero, do not
663create the file, just its name. Otherwise, if DIR-FLAG is non-nil, 663create the file, just its name. Otherwise, if DIR-FLAG is non-nil,
664create an empty directory. The file name should end in SUFFIX. 664create an empty directory. The file name should end in SUFFIX.
665Do not expand PREFIX; a non-absolute PREFIX is relative to the Emacs 665Do not expand PREFIX; a non-absolute PREFIX is relative to the Emacs
666working directory. 666working directory. If TEXT is a string, insert it into the newly
667created file.
667 668
668Signal an error if the file could not be created. 669Signal an error if the file could not be created.
669 670
670This function does not grok magic file names. */) 671This function does not grok magic file names. */)
671 (Lisp_Object prefix, Lisp_Object dir_flag, Lisp_Object suffix) 672 (Lisp_Object prefix, Lisp_Object dir_flag, Lisp_Object suffix,
673 Lisp_Object text)
672{ 674{
673 CHECK_STRING (prefix); 675 CHECK_STRING (prefix);
674 CHECK_STRING (suffix); 676 CHECK_STRING (suffix);
@@ -688,7 +690,15 @@ This function does not grok magic file names. */)
688 : EQ (dir_flag, make_number (0)) ? GT_NOCREATE 690 : EQ (dir_flag, make_number (0)) ? GT_NOCREATE
689 : GT_DIR); 691 : GT_DIR);
690 int fd = gen_tempname (data, suffix_len, O_BINARY | O_CLOEXEC, kind); 692 int fd = gen_tempname (data, suffix_len, O_BINARY | O_CLOEXEC, kind);
691 if (fd < 0 || (NILP (dir_flag) && emacs_close (fd) != 0)) 693 bool failed = fd < 0;
694 if (!failed)
695 {
696 val = DECODE_FILE (val);
697 if (STRINGP (text) && SBYTES (text) != 0)
698 write_region (text, Qnil, val, Qnil, Qnil, Qnil, Qnil, fd);
699 failed = NILP (dir_flag) && emacs_close (fd) != 0;
700 }
701 if (failed)
692 { 702 {
693 static char const kind_message[][32] = 703 static char const kind_message[][32] =
694 { 704 {
@@ -698,7 +708,7 @@ This function does not grok magic file names. */)
698 }; 708 };
699 report_file_error (kind_message[kind], prefix); 709 report_file_error (kind_message[kind], prefix);
700 } 710 }
701 return DECODE_FILE (val); 711 return val;
702} 712}
703 713
704 714
@@ -715,7 +725,7 @@ For that reason, you should normally use `make-temp-file' instead. */)
715 (Lisp_Object prefix) 725 (Lisp_Object prefix)
716{ 726{
717 return Fmake_temp_file_internal (prefix, make_number (0), 727 return Fmake_temp_file_internal (prefix, make_number (0),
718 empty_unibyte_string); 728 empty_unibyte_string, Qnil);
719} 729}
720 730
721DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0, 731DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
diff --git a/src/filelock.c b/src/filelock.c
index fec9bc044ae..fd4f0aa864e 100644
--- a/src/filelock.c
+++ b/src/filelock.c
@@ -210,7 +210,7 @@ get_boot_time (void)
210 names up to 8 bytes long. Choose a 2 byte prefix, so 210 names up to 8 bytes long. Choose a 2 byte prefix, so
211 the 6-byte suffix does not make the name too long. */ 211 the 6-byte suffix does not make the name too long. */
212 filename = Fmake_temp_file_internal (build_string ("wt"), Qnil, 212 filename = Fmake_temp_file_internal (build_string ("wt"), Qnil,
213 empty_unibyte_string); 213 empty_unibyte_string, Qnil);
214 CALLN (Fcall_process, build_string ("gzip"), Qnil, 214 CALLN (Fcall_process, build_string ("gzip"), Qnil,
215 list2 (QCfile, filename), Qnil, 215 list2 (QCfile, filename), Qnil,
216 build_string ("-cd"), tempname); 216 build_string ("-cd"), tempname);