From 79fc0e70c0ef8cfd54c12a049033e35b79aecc19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dejean?= Date: Wed, 7 Dec 2005 09:35:50 +0000 Subject: [PATCH] Small improvements. Use g_array_sized_new instead of g_array_new with * procmap.c: (glibtop_get_proc_map_s): Small improvements. Use g_array_sized_new instead of g_array_new with reserved_size = 100. Don't use g_array_append_val to avoid copying glibtop_map_entries. Use g_array_set_size(size + 1) instead. I've run a little python benchmark and this shows a little speedup. I hope this would be more sensible in gnome-system-monitor (glibtop_get_procmap is intensively used for the 'Writable Memory' column). --- sysdeps/linux/ChangeLog | 15 +++++++++++++++ sysdeps/linux/procmap.c | 38 ++++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/sysdeps/linux/ChangeLog b/sysdeps/linux/ChangeLog index f1bb5b09..3c3d7e6a 100644 --- a/sysdeps/linux/ChangeLog +++ b/sysdeps/linux/ChangeLog @@ -1,3 +1,18 @@ +2005-12-07 Benoît Dejean + + * procmap.c: (glibtop_get_proc_map_s): + + Small improvements. + Use g_array_sized_new instead of g_array_new with + reserved_size = 100. + Don't use g_array_append_val to avoid copying + glibtop_map_entries. Use g_array_set_size(size + 1) instead. + + I've run a little python benchmark and this shows a little + speedup. I hope this would be more sensible in + gnome-system-monitor (glibtop_get_procmap is intensively used for + the 'Writable Memory' column). + 2005-10-29 Benoît Dejean * glibtop_private.c: (read_boot_time): diff --git a/sysdeps/linux/procmap.c b/sysdeps/linux/procmap.c index e0088737..03a64c33 100644 --- a/sysdeps/linux/procmap.c +++ b/sysdeps/linux/procmap.c @@ -59,8 +59,15 @@ glibtop_map_entry * glibtop_get_proc_map_s (glibtop *server, glibtop_proc_map *buf, pid_t pid) { char procfilename[GLIBTOP_MAP_FILENAME_LEN+1]; - GArray *entry_list = g_array_new(FALSE, FALSE, - sizeof(glibtop_map_entry)); + + /* + default size of 100 maybe inaccurate. + It's the average number of entry per process on my laptop + */ + + GArray *entry_list = g_array_sized_new(FALSE, FALSE, + sizeof(glibtop_map_entry), + 100); FILE *maps; glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_MAP, 0); @@ -77,13 +84,14 @@ glibtop_get_proc_map_s (glibtop *server, glibtop_proc_map *buf, pid_t pid) { unsigned long perm = 0; int rv; + guint len; unsigned short dev_major, dev_minor; unsigned long start, end, offset, inode; char flags[4]; char filename [GLIBTOP_MAP_FILENAME_LEN+1]; - glibtop_map_entry entry; + glibtop_map_entry *entry; /* 8 arguments */ @@ -113,16 +121,22 @@ glibtop_get_proc_map_s (glibtop *server, glibtop_proc_map *buf, pid_t pid) else if (flags [3] == 'p') perm |= GLIBTOP_MAP_PERM_PRIVATE; - entry.flags = _glibtop_sysdeps_map_entry; - entry.start = (guint64) start; - entry.end = (guint64) end; - entry.offset = (guint64) offset; - entry.perm = (guint64) perm; - entry.device = (guint64) MKDEV(dev_major, dev_minor); - entry.inode = (guint64) inode; - g_strlcpy (entry.filename, filename, sizeof entry.filename); + /* + avoid copying the entry, grow by 1 and point to the last + element. + */ + len = entry_list->len; + g_array_set_size(entry_list, len + 1); + entry = &g_array_index(entry_list, glibtop_map_entry, len); - g_array_append_val(entry_list, entry); + entry->flags = _glibtop_sysdeps_map_entry; + entry->start = (guint64) start; + entry->end = (guint64) end; + entry->offset = (guint64) offset; + entry->perm = (guint64) perm; + entry->device = (guint64) MKDEV(dev_major, dev_minor); + entry->inode = (guint64) inode; + g_strlcpy(entry->filename, filename, sizeof entry->filename); } fclose (maps);