Re: GtkExText




One thing I noticed with my widget is that it take 
lots if time to free lines if it have a big file in the widget.

the line struct looks like this:
LineData
{
  gint16 length;
  gint16 flags;
  gpointer userdata;
  LineData *next;
  LineData *prev;
}

This is ofcource because it iterates line by line and g_free them.

Maybe its better to allocate more lines when initiating:
using arrays instead (dont sure this is ok in C?)

#define LINE_INIT_SIZE 4096

LineData *lines=NULL;

lines=g_malloc0(sizeof(LineData)*LINE_INIT_SIZE);
line_size=LINE_INIT_SIZE;         /* hold allocated size */
line_count=1;  // there is always one line in the widget 

by this coding style you dont need linked lists (saves 8 bytes in 
structure)
instead we iterate array with [line_number]

for (int lnum=0; lnum< line_count; lnum++)
 g_print("line_number %d length\n",lnum,lines[lnum].length);

then when line_count == line_size we reallocate memory:

lines=g_realloc(lines,line_size + (sizeof(LineCache)*LINE_INIT_SIZE))
oldsize=line_size;
line_size=line_size + (sizeof(LineCache)*LINE_INIT_SIZE))

// realloc dont set new memory to 0 so we need to do this manually */
memset(&lines[oldsize],0,sizeof(LineCache)*LINE_INIT_SIZE);

But again is this valid coding style in ANSI C?
Is there some drawbacks using this method?

As I see it this saves 8 bytes per structure, because we dont need
the *next/*prev pointers but  it takes more memory ones instead.

Greats

MikeH


---

Check out my homepage at (under heavy reconstruction): 
http://www.bahnhof.se/~mikeh	

Email: mikeh@bahnhof.se
international Phone 46-44-126995
Sweden Phone: 044-126995
ICQ: Error fix later



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