summary refs log tree commit diff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2022-01-08 17:29:12 +0100
committerLudovic Courtès <ludo@gnu.org>2022-01-26 17:54:58 +0100
commita74d0cc209c076150968dfe7b3b9975faad44701 (patch)
treee30b15dfb056d5a608096bf2c03dab281d3b0274
parentb77b788421d189dc560185a3887b609c01163e9f (diff)
downloadguix-a74d0cc209c076150968dfe7b3b9975faad44701.tar.gz
utils: 'edit-expression' reads and conses less when not modifying.
* guix/utils.scm (edit-expression): Delay call to 'get-bytevector-all'
until after (string=? str* str).
-rw-r--r--guix/utils.scm34
1 files changed, 17 insertions, 17 deletions
diff --git a/guix/utils.scm b/guix/utils.scm
index cba6464523..a0ca9b9070 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -1,5 +1,5 @@
 ;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2012-2022 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2013, 2014, 2015 Mark H Weaver <mhw@netris.org>
 ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
 ;;; Copyright © 2014 Ian Denhardt <ian@zenhack.net>
@@ -454,27 +454,27 @@ This procedure returns #t on success."
                  (str     (iconv:bytevector->string
                            (get-bytevector-n in (- end start))
                            (port-encoding in)))
-                 (post-bv (get-bytevector-all in))
                  (str*    (proc str)))
             ;; Modify FILE only if there are changes.
             (unless (string=? str* str)
               ;; Verify the edited expression is still a scheme expression.
               (call-with-input-string str* read)
-              ;; Update the file with edited expression.
-              (with-atomic-file-output file
-                (lambda (out)
-                  (put-bytevector out pre-bv)
-                  (display str* out)
-                  ;; post-bv maybe the end-of-file object.
-                  (when (not (eof-object? post-bv))
-                    (put-bytevector out post-bv))
-                  #t))
-
-              ;; Due to 'with-atomic-file-output', IN and FILE no longer share
-              ;; the same inode, but we can reassign the source map up to LINE
-              ;; to the new file.
-              (move-source-location-map! (stat in) (stat file)
-                                         (+ 1 line)))))))))
+
+              (let ((post-bv (get-bytevector-all in)))
+                ;; Update the file with edited expression.
+                (with-atomic-file-output file
+                  (lambda (out)
+                    (put-bytevector out pre-bv)
+                    (display str* out)
+                    (unless (eof-object? post-bv)
+                      ;; Copy everything that came after STR.
+                      (put-bytevector out post-bv))))
+
+                ;; Due to 'with-atomic-file-output', IN and FILE no longer
+                ;; share the same inode, but we can reassign the source map up
+                ;; to LINE to the new file.
+                (move-source-location-map! (stat in) (stat file)
+                                           (+ 1 line))))))))))
 
 
 ;;;