From d942a0ebc6766437fbb09e27211d7f75d74e36b8 Mon Sep 17 00:00:00 2001
From: Yorhel <git@yorhel.nl>
Date: Wed, 28 Apr 2010 17:15:46 +0200
Subject: [PATCH] Only create the line format once when drawing the browser
 window

The memory for this format is now statically allocated as well. I
was under the impression its size would depend on wincols, but this is
the format we're talking about, the string does not have to hold the
actual line contents. I must have been sleeping again...

Oh well, this is a slight performance improvement, although it doesn't
seem the be the cause of the browing slowness when running under
valgrind. (Obviously running ncdu with valgrind is supposed to be
slower, but the current performance is rather bad...)
---
 src/browser.c | 37 ++++++++++++++++---------------------
 1 file changed, 16 insertions(+), 21 deletions(-)

diff --git a/src/browser.c b/src/browser.c
index 24857da..a5a5a8c 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -90,8 +90,8 @@ void browse_draw_info(struct dir *dr) {
 }
 
 
-void browse_draw_item(struct dir *n, int row) {
-  char *line, ct, dt, *size, gr[11];
+void browse_draw_item(struct dir *n, int row, char *line) {
+  char ct, dt, *size, gr[11];
   int i, o;
   float pc;
 
@@ -141,25 +141,12 @@ void browse_draw_item(struct dir *n, int row) {
   }
 
   /* format and add item to the list */
-  line = malloc(wincols > 35 ? wincols+1 : 36);
   switch(graph) {
-    case 0:
-      sprintf(line, "%%c %%8s  %%c%%-%ds", wincols-13);
-      mvprintw(row, 0, line, ct, size, dt, cropstr(n->name, wincols-13));
-      break;
-    case 1:
-      sprintf(line, "%%c %%8s [%%10s] %%c%%-%ds", wincols-25);
-      mvprintw(row, 0, line, ct, size, gr, dt, cropstr(n->name, wincols-25));
-      break;
-    case 2:
-      sprintf(line, "%%c %%8s [%%5.1f%%%%] %%c%%-%ds", wincols-21);
-      mvprintw(row, 0, line, ct, size, pc, dt, cropstr(n->name, wincols-21));
-      break;
-    case 3:
-      sprintf(line, "%%c %%8s [%%5.1f%%%% %%10s] %%c%%-%ds", wincols-32);
-      mvprintw(row, 0, line, ct, size, pc, gr, dt, cropstr(n->name, wincols-32));
+    case 0: mvprintw(row, 0, line, ct, size, dt, cropstr(n->name, wincols-13)); break;
+    case 1: mvprintw(row, 0, line, ct, size, gr, dt, cropstr(n->name, wincols-25)); break;
+    case 2: mvprintw(row, 0, line, ct, size, pc, dt, cropstr(n->name, wincols-21)); break;
+    case 3: mvprintw(row, 0, line, ct, size, pc, gr, dt, cropstr(n->name, wincols-32));
   }
-  free(line);
 
   if(n->flags & FF_BSEL)
     attroff(A_REVERSE);
@@ -168,7 +155,7 @@ void browse_draw_item(struct dir *n, int row) {
 
 void browse_draw() {
   struct dir *t;
-  char fmtsize[9], *tmp;
+  char fmtsize[9], *tmp, line[35];
   int selected, i;
 
   erase();
@@ -207,9 +194,17 @@ void browse_draw() {
   /* get start position */
   t = dirlist_top(0);
 
+  /* create line format */
+  switch(graph) {
+    case 0: sprintf(line, "%%c %%8s  %%c%%-%ds", wincols-13); break;
+    case 1: sprintf(line, "%%c %%8s [%%10s] %%c%%-%ds", wincols-25); break;
+    case 2: sprintf(line, "%%c %%8s [%%5.1f%%%%] %%c%%-%ds", wincols-21); break;
+    case 3: sprintf(line, "%%c %%8s [%%5.1f%%%% %%10s] %%c%%-%ds", wincols-32);
+  }
+
   /* print the list to the screen */
   for(i=0; t && i<winrows-3; t=dirlist_next(t),i++) {
-    browse_draw_item(t, 2+i);
+    browse_draw_item(t, 2+i, line);
     /* save the selected row number for later */
     if(t->flags & FF_BSEL)
       selected = i;
-- 
GitLab