qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: konrad.schwarz@gmail.com
To: qemu-devel@nongnu.org
Cc: philmd@linaro.org, Konrad Schwarz <konrad.schwarz@siemens.com>,
	jan.kiszka@siemens.com
Subject: [PATCH 1/1] scripts: Changed potential O(n) file size calculation to O(1)
Date: Wed, 19 Nov 2025 12:28:16 -0500	[thread overview]
Message-ID: <CA+kmUXafV4PPo2t+P23g9QRXHjaH_XBke3DjzbvciqBtw+i-OA@mail.gmail.com> (raw)

The mkemmc.sh script calculates file sizes via `wc -c'.  `wc'
normally works by reading the entire file, resulting in O(n) performance.

Unix file systems obviously know a file's size and POSIX `ls' reports this
information unambiguously, so replacing `wc' with `ls' ensures O(1)
performance.  The files in question tend to be large making this change
worthwhile.

Signed-off-by: Konrad Schwarz <konrad.schwarz@siemens.com>
---
 scripts/mkemmc.sh | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/scripts/mkemmc.sh b/scripts/mkemmc.sh
index 45dc3f08fa..d2c4e84b16 100755
--- a/scripts/mkemmc.sh
+++ b/scripts/mkemmc.sh
@@ -37,13 +37,19 @@ usage() {
     exit "$1"
 }

+file_size() {
+	ls_line=$(ls -Hdog "$1") || return
+	printf %s\\n "$ls_line" | cut -d\  -f3
+	unset ls_line
+}
+
 process_size() {
     name=$1
     image_file=$2
     alignment=$3
     image_arg=$4
     if [ "${image_arg#*:}" = "$image_arg"  ]; then
-        if ! size=$(wc -c < "$image_file" 2>/dev/null); then
+        if ! size=$(file_size "$image_file"); then
             echo "Missing $name image '$image_file'." >&2
             exit 1
         fi
@@ -105,7 +111,7 @@ check_truncation() {
     if [ "$image_file" = "/dev/zero" ]; then
         return
     fi
-    if ! actual_size=$(wc -c < "$image_file" 2>/dev/null); then
+    if ! actual_size=$(file_size "$image_file"); then
         echo "Missing image '$image_file'." >&2
         exit 1
     fi
-- 
2.39.5


             reply	other threads:[~2025-11-19 18:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-19 17:28 konrad.schwarz [this message]
2025-11-21 10:16 ` [PATCH 1/1] scripts: Changed potential O(n) file size calculation to O(1) Jan Kiszka
2025-11-21 11:09   ` Daniel P. Berrangé
2025-11-21 14:07     ` Schwarz, Konrad
2025-11-21 13:32   ` Schwarz, Konrad
2025-11-21 14:10     ` Jan Kiszka
2025-11-21 12:25 ` Paolo Bonzini
     [not found]   ` <AS8PR10MB4680FED05DB882C0C7751E058AD5A@AS8PR10MB4680.EURPRD10.PROD.OUTLOOK.COM>
     [not found]     ` <CABgObfb89UUyha3xz5d2MihRkg2WKBTpUOiNMA5o8oPE=MBMyQ@mail.gmail.com>
2025-11-21 14:57       ` Schwarz, Konrad

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+kmUXafV4PPo2t+P23g9QRXHjaH_XBke3DjzbvciqBtw+i-OA@mail.gmail.com \
    --to=konrad.schwarz@gmail.com \
    --cc=jan.kiszka@siemens.com \
    --cc=konrad.schwarz@siemens.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).