aboutsummaryrefslogtreecommitdiffstats
path: root/src/tag_pool.c
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-08-29 15:04:49 +0200
committerMax Kellermann <max@duempel.org>2008-08-29 15:04:49 +0200
commitd38d2bc3533dcfb661a3cbb8bd6cd9574c31ab69 (patch)
tree2c50a2528b0b2e56a6b1749385b2290150810e5b /src/tag_pool.c
parentd8ad109e10c547ed4167daa9117a09d432b1b571 (diff)
downloadmpd-d38d2bc3533dcfb661a3cbb8bd6cd9574c31ab69.tar.gz
mpd-d38d2bc3533dcfb661a3cbb8bd6cd9574c31ab69.tar.xz
mpd-d38d2bc3533dcfb661a3cbb8bd6cd9574c31ab69.zip
tag: optimize tag_dup(), copy item references
Don't call tag_pool_get_item() for duplicating tags, just increase the item's reference counter instead.
Diffstat (limited to 'src/tag_pool.c')
-rw-r--r--src/tag_pool.c43
1 files changed, 37 insertions, 6 deletions
diff --git a/src/tag_pool.c b/src/tag_pool.c
index 744a82fdb..89efef1fc 100644
--- a/src/tag_pool.c
+++ b/src/tag_pool.c
@@ -61,6 +61,19 @@ tag_item_to_slot(struct tag_item *item)
return (struct slot*)(((char*)item) - offsetof(struct slot, item));
}
+static struct slot *slot_alloc(struct slot *next,
+ enum tag_type type,
+ const char *value, int length)
+{
+ struct slot *slot = xmalloc(sizeof(*slot) + length);
+ slot->next = next;
+ slot->ref = 1;
+ slot->item.type = type;
+ memcpy(slot->item.value, value, length);
+ slot->item.value[length] = 0;
+ return slot;
+}
+
struct tag_item *tag_pool_get_item(enum tag_type type,
const char *value, int length)
{
@@ -76,16 +89,34 @@ struct tag_item *tag_pool_get_item(enum tag_type type,
}
}
- slot = xmalloc(sizeof(*slot) + length);
- slot->next = *slot_p;
- slot->ref = 1;
- slot->item.type = type;
- memcpy(slot->item.value, value, length);
- slot->item.value[length] = 0;
+ slot = slot_alloc(*slot_p, type, value, length);
*slot_p = slot;
return &slot->item;
}
+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;
+ }
+}
+
void tag_pool_put_item(struct tag_item *item)
{
struct slot **slot_p, *slot;