nano-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Nano-devel] [PATCH] utils: slightly speed up the calculation of the siz


From: Benno Schulenberg
Subject: [Nano-devel] [PATCH] utils: slightly speed up the calculation of the size of a buffer
Date: Sun, 11 Dec 2016 10:54:20 +0100

Achieve this by eliding two conditions from the inner loop,
which is possible because 'end' will never be NULL.
---
 src/utils.c | 29 ++++++++---------------------
 1 file changed, 8 insertions(+), 21 deletions(-)

diff --git a/src/utils.c b/src/utils.c
index 7c61cebf..989c72c4 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -599,32 +599,19 @@ void mark_order(const filestruct **top, size_t *top_x, 
const filestruct
 }
 #endif /* !NANO_TINY */
 
-/* Calculate the number of characters between begin and end, and return
- * it. */
+/* Count the number of characters from begin to end, and return it. */
 size_t get_totsize(const filestruct *begin, const filestruct *end)
 {
+    const filestruct *line;
     size_t totsize = 0;
-    const filestruct *f;
 
-    /* Go through the lines from begin to end->prev, if we can. */
-    for (f = begin; f != end && f != NULL; f = f->next) {
-       /* Count the number of characters on this line. */
-       totsize += mbstrlen(f->data);
+    /* Sum the number of characters (plus a newline) in each line. */
+    for (line = begin; line != end->next; line = line->next)
+       totsize += mbstrlen(line->data) + 1;
 
-       /* Count the newline if we have one. */
-       if (f->next != NULL)
-           totsize++;
-    }
-
-    /* Go through the line at end, if we can. */
-    if (f != NULL) {
-       /* Count the number of characters on this line. */
-       totsize += mbstrlen(f->data);
-
-       /* Count the newline if we have one. */
-       if (f->next != NULL)
-           totsize++;
-    }
+    /* If the buffer does not end with a newline, subtract it. */
+    if (line == NULL)
+       totsize--;
 
     return totsize;
 }
-- 
2.11.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]