[570] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[D-O-H] r688 - / branches/par

daemon@ATHENA.MIT.EDU (nelhage@MIT.EDU)
Thu Oct 29 18:07:31 2009

Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
To: dirty-owl-hackers@mit.edu
From: nelhage@MIT.EDU
Reply-to: dirty-owl-hackers@MIT.EDU
Date: Tue, 27 Mar 2007 22:04:11 -0400 (EDT)

Author: nelhage
Date: 2007-03-27 22:04:10 -0400 (Tue, 27 Mar 2007)
New Revision: 688

Modified:
   /
   branches/par/fmtext.c
   branches/par/global.c
   branches/par/message.c
   branches/par/owl.h
Log:
 r20719@phanatique:  nelhage | 2007-03-27 22:04:03 -0400
 Implementing an LRU cache of the message list fmtexts. This reduces
 memory usage by roughly 1MB/kilo-zephyrs in steady state.



Property changes on: 
___________________________________________________________________
Name: svk:merge
   - 6122c8b4-0e12-0410-9533-8bcd7c66c992:/local/dirty-owl-hacks:24493
6aa88b72-b502-0410-8cb4-a5dd0230fb79:/owl-local:1356
bb873fd7-8e23-0410-944a-99ec44c633eb:/local/barnowl:20713
bb873fd7-8e23-0410-944a-99ec44c633eb:/local/d-o-h:18636
   + 6122c8b4-0e12-0410-9533-8bcd7c66c992:/local/dirty-owl-hacks:24493
6aa88b72-b502-0410-8cb4-a5dd0230fb79:/owl-local:1356
bb873fd7-8e23-0410-944a-99ec44c633eb:/local/barnowl:20719
bb873fd7-8e23-0410-944a-99ec44c633eb:/local/d-o-h:18636

Modified: branches/par/fmtext.c
===================================================================
--- branches/par/fmtext.c	2007-03-27 01:18:13 UTC (rev 687)
+++ branches/par/fmtext.c	2007-03-28 02:04:10 UTC (rev 688)
@@ -8,15 +8,28 @@
 void owl_fmtext_init_null(owl_fmtext *f)
 {
   f->textlen=0;
-  f->textbuff=owl_strdup("");
+  f->bufflen=5;
+  f->textbuff=owl_malloc(5);
   f->fmbuff=owl_malloc(5);
   f->fgcolorbuff=owl_malloc(5);
   f->bgcolorbuff=owl_malloc(5);
+  f->textbuff[0]=0;
   f->fmbuff[0]=OWL_FMTEXT_ATTR_NONE;
   f->fgcolorbuff[0]=OWL_COLOR_DEFAULT;
   f->bgcolorbuff[0]=OWL_COLOR_DEFAULT;
 }
 
+/* Clear the data from an fmtext, but don't deallocate memory. This
+   fmtext can then be appended to again. */
+void owl_fmtext_clear(owl_fmtext *f)
+{
+    f->textlen = 0;
+    f->textbuff[0] = 0;
+    f->fmbuff[0]=OWL_FMTEXT_ATTR_NONE;
+    f->fgcolorbuff[0]=OWL_COLOR_DEFAULT;
+    f->bgcolorbuff[0]=OWL_COLOR_DEFAULT;
+}
+
 /* Internal function.  Set the attribute 'attr' from index 'first' to
  * index 'last'
  */
@@ -58,19 +71,26 @@
   }
 }
 
+void _owl_fmtext_realloc(owl_fmtext *f, int newlen) /*noproto*/
+{
+    if(newlen + 1 > f->bufflen) {
+      f->textbuff=owl_realloc(f->textbuff, newlen+1);
+      f->fmbuff=owl_realloc(f->fmbuff, newlen+1);
+      f->fgcolorbuff=owl_realloc(f->fgcolorbuff, newlen+1);
+      f->bgcolorbuff=owl_realloc(f->bgcolorbuff, newlen+1);
+      f->bufflen = newlen+1;
+  }
+}
+
 /* append text to the end of 'f' with attribute 'attr' and color
  * 'color'
  */
 void owl_fmtext_append_attr(owl_fmtext *f, char *text, int attr, int fgcolor, int bgcolor)
 {
   int newlen;
-
   newlen=strlen(f->textbuff)+strlen(text);
-  f->textbuff=owl_realloc(f->textbuff, newlen+2);
-  f->fmbuff=owl_realloc(f->fmbuff, newlen+2);
-  f->fgcolorbuff=owl_realloc(f->fgcolorbuff, newlen+2);
-  f->bgcolorbuff=owl_realloc(f->bgcolorbuff, newlen+2);
-
+  _owl_fmtext_realloc(f, newlen);
+  
   strcat(f->textbuff, text);
   _owl_fmtext_set_attr(f, attr, f->textlen, newlen);
   _owl_fmtext_set_fgcolor(f, fgcolor, f->textlen, newlen);
@@ -153,10 +173,7 @@
   int newlen, i;
 
   newlen=strlen(f->textbuff)+(stop-start+1);
-  f->textbuff=owl_realloc(f->textbuff, newlen+1);
-  f->fmbuff=owl_realloc(f->fmbuff, newlen+1);
-  f->fgcolorbuff=owl_realloc(f->fgcolorbuff, newlen+1);
-  f->bgcolorbuff=owl_realloc(f->bgcolorbuff, newlen+1);
+  _owl_fmtext_realloc(f, newlen);
 
   strncat(f->textbuff, in->textbuff+start, stop-start+1);
   f->textbuff[newlen]='\0';

Modified: branches/par/global.c
===================================================================
--- branches/par/global.c	2007-03-27 01:18:13 UTC (rev 687)
+++ branches/par/global.c	2007-03-28 02:04:10 UTC (rev 688)
@@ -107,6 +107,8 @@
   owl_timer_create_countdown(&(g->zephyr_buddycheck_timer), 60*3);
 
   owl_obarray_init(&(g->obarray));
+
+  owl_message_init_fmtext_cache();
 }
 
 void _owl_global_setup_windows(owl_global *g) {

Modified: branches/par/message.c
===================================================================
--- branches/par/message.c	2007-03-27 01:18:13 UTC (rev 687)
+++ branches/par/message.c	2007-03-28 02:04:10 UTC (rev 688)
@@ -12,13 +12,36 @@
 
 static const char fileIdent[] = "$Id$";
 
+static owl_fmtext_cache fmtext_cache[OWL_FMTEXT_CACHE_SIZE];
+static owl_fmtext_cache * fmtext_cache_next = fmtext_cache;
+
+void owl_message_init_fmtext_cache ()
+{
+    int i;
+    for(i = 0; i < OWL_FMTEXT_CACHE_SIZE; i++) {
+        owl_fmtext_init_null(&(fmtext_cache[i].fmtext));
+        fmtext_cache[i].message = NULL;
+    }
+}
+
+owl_fmtext_cache * owl_message_next_fmtext() /*noproto*/
+{
+    if(fmtext_cache_next->message != NULL) {
+        owl_message_invalidate_format(fmtext_cache_next->message);
+    }
+    owl_fmtext_cache * f = fmtext_cache_next;
+    fmtext_cache_next++;
+    if(fmtext_cache_next - fmtext_cache == OWL_FMTEXT_CACHE_SIZE)
+        fmtext_cache_next = fmtext_cache;
+    return f;
+}
+
 void owl_message_init(owl_message *m)
 {
   m->id=owl_global_get_nextmsgid(&g);
   owl_message_set_direction_none(m);
   m->delete=0;
   m->zwriteline=NULL;
-  m->invalid_format=1;
 
   owl_message_set_hostname(m, "");
   owl_list_create(&(m->attributes));
@@ -28,8 +51,7 @@
   m->timestr=owl_strdup(ctime(&(m->time)));
   m->timestr[strlen(m->timestr)-1]='\0';
 
-  /* initialize the fmtext */
-  owl_fmtext_init_null(&(m->fmtext));
+  m->fmtext = NULL;
 }
 
 /* add the named attribute to the message.  If an attribute with the
@@ -105,13 +127,17 @@
 
 void owl_message_invalidate_format(owl_message *m)
 {
-  m->invalid_format=1;
+  if(m->fmtext) {
+    m->fmtext->message = NULL;
+    owl_fmtext_clear(&(m->fmtext->fmtext));
+    m->fmtext=NULL;
+  }
 }
 
 owl_fmtext *owl_message_get_fmtext(owl_message *m)
 {
   owl_message_format(m);
-  return(&(m->fmtext));
+  return(&(m->fmtext->fmtext));
 }
 
 void owl_message_format(owl_message *m)
@@ -119,15 +145,14 @@
   owl_style *s;
   owl_view *v;
 
-  if (m->invalid_format) {
+  if (!m->fmtext) {
+    m->fmtext = owl_message_next_fmtext();
+    m->fmtext->message = m;
     /* for now we assume there's just the one view and use that style */
     v=owl_global_get_current_view(&g);
     s=owl_view_get_style(v);
 
-    owl_fmtext_free(&(m->fmtext));
-    owl_fmtext_init_null(&(m->fmtext));
-    owl_style_get_formattext(s, &(m->fmtext), m);
-    m->invalid_format=0;
+    owl_style_get_formattext(s, &(m->fmtext->fmtext), m);
   }
 }
 
@@ -391,7 +416,7 @@
 
 char *owl_message_get_text(owl_message *m)
 {
-  return(owl_fmtext_get_text(&(m->fmtext)));
+  return(owl_fmtext_get_text(&(m->fmtext->fmtext)));
 }
 
 void owl_message_set_direction_in(owl_message *m)
@@ -436,7 +461,7 @@
 {
   if (m == NULL) return(0);
   owl_message_format(m);
-  return(owl_fmtext_num_lines(&(m->fmtext)));
+  return(owl_fmtext_num_lines(&(m->fmtext->fmtext)));
 }
 
 void owl_message_mark_delete(owl_message *m)
@@ -503,7 +528,7 @@
   owl_fmtext_init_null(&a);
   owl_fmtext_init_null(&b);
   
-  owl_fmtext_truncate_lines(&(m->fmtext), aline, bline-aline+1, &a);
+  owl_fmtext_truncate_lines(&(m->fmtext->fmtext), aline, bline-aline+1, &a);
   owl_fmtext_truncate_cols(&a, acol, bcol, &b);
   if (fgcolor!=OWL_COLOR_DEFAULT) {
     owl_fmtext_colorize(&b, fgcolor);
@@ -697,7 +722,7 @@
 
   owl_message_format(m); /* is this necessary? */
   
-  return (owl_fmtext_search(&(m->fmtext), string));
+  return (owl_fmtext_search(&(m->fmtext->fmtext), string));
 }
 
 
@@ -988,5 +1013,5 @@
 
   owl_list_free_simple(&(m->attributes));
  
-  owl_fmtext_free(&(m->fmtext));
+  owl_message_invalidate_format(m);
 }

Modified: branches/par/owl.h
===================================================================
--- branches/par/owl.h	2007-03-27 01:18:13 UTC (rev 687)
+++ branches/par/owl.h	2007-03-28 02:04:10 UTC (rev 688)
@@ -248,6 +248,7 @@
 
 typedef struct _owl_fmtext {
   int textlen;
+  int bufflen;
   char *textbuff;
   char *fmbuff;
   char *fgcolorbuff;
@@ -328,14 +329,15 @@
   void *value;
 } owl_pair;
 
+struct _owl_fmtext_cache;
+
 typedef struct _owl_message {
   int id;
   int direction;
 #ifdef HAVE_LIBZEPHYR
   ZNotice_t notice;
 #endif
-  owl_fmtext fmtext;              /* this is now only a CACHED copy */
-  int invalid_format;             /* indicates whether fmtext needs to be regenerated */
+  struct _owl_fmtext_cache * fmtext;
   int delete;
   char *hostname;
   owl_list attributes;            /* this is a list of pairs */
@@ -344,6 +346,14 @@
   char *zwriteline;
 } owl_message;
 
+#define OWL_FMTEXT_CACHE_SIZE 1000
+/* We cache the saved fmtexts for the last bunch of messages we
+   rendered */
+typedef struct _owl_fmtext_cache {
+    owl_message * message;
+    owl_fmtext fmtext;
+} owl_fmtext_cache;
+
 typedef struct _owl_style {
   char *name;
   char *description;


home help back first fref pref prev next nref lref last post