2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-07 10:36:27 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-07 10:36:27 +01:00
|
|
|
#include "TagPool.hxx"
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2012-08-08 21:01:25 +02:00
|
|
|
#if GCC_CHECK_VERSION(4, 2)
|
|
|
|
/* workaround for a warning caused by G_STATIC_MUTEX_INIT */
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
GStaticMutex tag_pool_lock = G_STATIC_MUTEX_INIT;
|
|
|
|
|
|
|
|
#if GCC_CHECK_VERSION(4, 2)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2008-09-07 19:14:45 +02:00
|
|
|
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
#define NUM_SLOTS 4096
|
|
|
|
|
|
|
|
struct slot {
|
|
|
|
struct slot *next;
|
|
|
|
unsigned char ref;
|
|
|
|
struct tag_item item;
|
|
|
|
} mpd_packed;
|
|
|
|
|
2009-03-14 14:38:48 +01:00
|
|
|
static struct slot *slots[NUM_SLOTS];
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
|
|
|
|
static inline unsigned
|
|
|
|
calc_hash_n(enum tag_type type, const char *p, size_t length)
|
|
|
|
{
|
|
|
|
unsigned hash = 5381;
|
|
|
|
|
2013-01-07 10:36:27 +01:00
|
|
|
assert(p != nullptr);
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
|
|
|
|
while (length-- > 0)
|
|
|
|
hash = (hash << 5) + hash + *p++;
|
|
|
|
|
|
|
|
return hash ^ type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned
|
|
|
|
calc_hash(enum tag_type type, const char *p)
|
|
|
|
{
|
|
|
|
unsigned hash = 5381;
|
|
|
|
|
2013-01-07 10:36:27 +01:00
|
|
|
assert(p != nullptr);
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
|
|
|
|
while (*p != 0)
|
|
|
|
hash = (hash << 5) + hash + *p++;
|
|
|
|
|
|
|
|
return hash ^ type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct slot *
|
|
|
|
tag_item_to_slot(struct tag_item *item)
|
|
|
|
{
|
|
|
|
return (struct slot*)(((char*)item) - offsetof(struct slot, item));
|
|
|
|
}
|
|
|
|
|
2008-08-29 15:04:49 +02:00
|
|
|
static struct slot *slot_alloc(struct slot *next,
|
|
|
|
enum tag_type type,
|
|
|
|
const char *value, int length)
|
|
|
|
{
|
2008-10-13 09:40:14 +02:00
|
|
|
struct slot *slot;
|
|
|
|
|
2013-01-07 10:36:27 +01:00
|
|
|
slot = (struct slot *)
|
|
|
|
g_malloc(sizeof(*slot) - sizeof(slot->item.value) + length + 1);
|
2008-08-29 15:04:49 +02:00
|
|
|
slot->next = next;
|
|
|
|
slot->ref = 1;
|
|
|
|
slot->item.type = type;
|
|
|
|
memcpy(slot->item.value, value, length);
|
|
|
|
slot->item.value[length] = 0;
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2009-03-27 19:36:03 +01:00
|
|
|
struct tag_item *
|
|
|
|
tag_pool_get_item(enum tag_type type, const char *value, size_t length)
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
{
|
|
|
|
struct slot **slot_p, *slot;
|
|
|
|
|
|
|
|
slot_p = &slots[calc_hash_n(type, value, length) % NUM_SLOTS];
|
2013-01-07 10:36:27 +01:00
|
|
|
for (slot = *slot_p; slot != nullptr; slot = slot->next) {
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
if (slot->item.type == type &&
|
2009-03-27 19:36:24 +01:00
|
|
|
length == strlen(slot->item.value) &&
|
|
|
|
memcmp(value, slot->item.value, length) == 0 &&
|
|
|
|
slot->ref < 0xff) {
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
assert(slot->ref > 0);
|
|
|
|
++slot->ref;
|
|
|
|
return &slot->item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-29 15:04:49 +02:00
|
|
|
slot = slot_alloc(*slot_p, type, value, length);
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
*slot_p = slot;
|
|
|
|
return &slot->item;
|
|
|
|
}
|
|
|
|
|
2008-08-29 15:04:49 +02:00
|
|
|
struct tag_item *tag_pool_dup_item(struct tag_item *item)
|
|
|
|
{
|
|
|
|
struct slot *slot = tag_item_to_slot(item);
|
|
|
|
|
|
|
|
assert(slot->ref > 0);
|
|
|
|
|
|
|
|
if (slot->ref < 0xff) {
|
|
|
|
++slot->ref;
|
|
|
|
return item;
|
|
|
|
} else {
|
|
|
|
/* the reference counter overflows above 0xff;
|
|
|
|
duplicate the item, and start with 1 */
|
|
|
|
size_t length = strlen(item->value);
|
|
|
|
struct slot **slot_p =
|
|
|
|
&slots[calc_hash_n(item->type, item->value,
|
|
|
|
length) % NUM_SLOTS];
|
|
|
|
slot = slot_alloc(*slot_p, item->type,
|
|
|
|
item->value, strlen(item->value));
|
|
|
|
*slot_p = slot;
|
|
|
|
return &slot->item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
void tag_pool_put_item(struct tag_item *item)
|
|
|
|
{
|
|
|
|
struct slot **slot_p, *slot;
|
|
|
|
|
|
|
|
slot = tag_item_to_slot(item);
|
|
|
|
assert(slot->ref > 0);
|
|
|
|
--slot->ref;
|
|
|
|
|
|
|
|
if (slot->ref > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (slot_p = &slots[calc_hash(item->type, item->value) % NUM_SLOTS];
|
|
|
|
*slot_p != slot;
|
|
|
|
slot_p = &(*slot_p)->next) {
|
2013-01-07 10:36:27 +01:00
|
|
|
assert(*slot_p != nullptr);
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*slot_p = slot->next;
|
2009-01-03 14:51:37 +01:00
|
|
|
g_free(slot);
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
}
|