From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <ffmpeg-devel-bounces@ffmpeg.org> Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 9381E46CAE for <ffmpegdev@gitmailbox.com>; Mon, 21 Apr 2025 02:34:02 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 82201689EA6; Mon, 21 Apr 2025 05:33:58 +0300 (EEST) Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D2EF7687EB0 for <ffmpeg-devel@ffmpeg.org>; Mon, 21 Apr 2025 05:33:51 +0300 (EEST) Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-ae727e87c26so2339914a12.0 for <ffmpeg-devel@ffmpeg.org>; Sun, 20 Apr 2025 19:33:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emma-gg.20230601.gappssmtp.com; s=20230601; t=1745202829; x=1745807629; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=baet7zM8n6QQ7l0myMb1sq16dH9XEXq2eo+KdSW2Qao=; b=qB6pqJrIAFsXUp3P0Eeh+8jpnwIfw+17IIY3zhInskI67sHHOO0vluFfs4a+kou6Wd b+VjOUdpjX3do7chhzkxdlZUW82F5uvoh/42OxLidBAS0lK2t5UMbItUFXD9xTEhW7lu EG+x2lpiP3ZckbTX8q6KnFlg10ANDMZES1KyuLhTMtvVUCS1MKS60UcvhhzjCQwHXzJX JxERkynsWb2aKil/2dol9vfHq+TlkejNU3v5BXfjjFou3M8gNkmR7M99FAzUVqCoKjGL NMne7wq2T3h4emMkAwjoGvZ0X2J6ui0YrdiKsZAMsI1f+M77juya9NAupZSk5Gvjwei8 WzIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745202829; x=1745807629; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=baet7zM8n6QQ7l0myMb1sq16dH9XEXq2eo+KdSW2Qao=; b=cj27wJx8xA4GG5HpOgp7C9TOiFQBPAeDVJHVSxyEL5g1tqAi3GEOChQskSjKMBWhZq L/6SBfpJTi5ZaOTJU+T3GfZHhP1hgiSA7b0q/Otfh7oQmrgtkE2dqjx1cmjvg4zFUkd5 gaTCbD/HKl2hBXvoiHivLdylBf6OrrucVN/euYN0IzMtI8juiv8FhmzQHn5oM0Ga1+0q YcJ0H4tMzRy8YHhHOl4mZZXHNbiHo119qQff1YSw8+hprS6tGu0FikIr0FPLEe3Del2d 7Utc39xBfPCtQqQsQsKldWAtJDbwV135UHV0wbWDVytkbaTXhf281mfBigTHtupfIGlE EWDw== X-Gm-Message-State: AOJu0YxJsDfoEe4KVQnOu9ynGEmHuH2zAh+TI+DOeJEahR9NTW8SoOgG BlXV/NNuwfBxJSJDhG9EXaRD8t5e+DRa2fUeI9+/iMu45bXlJtJYaDqH/hzX5lki+idP3ScAB24 E X-Gm-Gg: ASbGnctZeMVCR/1PeVMayQ7CdFZRAqSXYM4ASx30y36ENuE19Wk8aAkAwuYyGL5kQQm KruxKOPjapxb1Zds7gbOMbYTf0ZoZ0xG/GuCnkfESPO9hyR/SrqTxNLK2fZPDtzXAlXFD/Gg9AJ jwgydrojXaJEpkYkqDayDCnrruIQkRjmt8XJgJyjJFoPAKUVLjy5Ig2D3ZRbnqBU/ivgUYWVSfX v7Rmqk+y/vGrDXDqw+PBrWnidXMzlyM+tP6e5mYXe1BANQCZ83+1ETkavQTnlWU2fx6bLdE1K+T yQfkfgNo7+MRZFW0Ue4TnRYglGabVx7I8R6WsKNDneV1WObnYo+XRSLbnR9SLeVVGw== X-Google-Smtp-Source: AGHT+IHNQaA68GEbvOhchsPGJQIkXcCZZNXKQ7cdDxppVdPNhrdNrYunnz9l8pOO0G8isvs4kgtIVA== X-Received: by 2002:a05:6a20:c892:b0:1f5:64fd:68eb with SMTP id adf61e73a8af0-203cbc26dfdmr14852487637.7.1745202829093; Sun, 20 Apr 2025 19:33:49 -0700 (PDT) Received: from localhost ([2600:1700:65a7:3600:b90a:a0ba:c740:60a5]) by smtp.gmail.com with UTF8SMTPSA id 41be03b00d2f7-b0db127447bsm4711116a12.2.2025.04.20.19.33.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 20 Apr 2025 19:33:48 -0700 (PDT) From: Emma Worley <emma@emma.gg> To: ffmpeg-devel@ffmpeg.org Date: Sun, 20 Apr 2025 19:33:43 -0700 Message-ID: <20250421023345.7558-1-emma@emma.gg> X-Mailer: git-send-email 2.48.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 1/3] lavu/hashtable: create generic robin hood hash table X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org> List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>, <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe> List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel> List-Post: <mailto:ffmpeg-devel@ffmpeg.org> List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help> List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>, <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe> Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org> Cc: Emma Worley <emma@emma.gg> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org> Archived-At: <https://master.gitmailbox.com/ffmpegdev/20250421023345.7558-1-emma@emma.gg/> List-Archive: <https://master.gitmailbox.com/ffmpegdev/> List-Post: <mailto:ffmpegdev@gitmailbox.com> Adds a general purpose hash table with the DXV encoder as an initial use case. Signed-off-by: Emma Worley <emma@emma.gg> --- libavutil/Makefile | 2 + libavutil/hashtable.c | 192 ++++++++++++++++++++++++++++++++++++ libavutil/hashtable.h | 91 +++++++++++++++++ libavutil/tests/hashtable.c | 110 +++++++++++++++++++++ 4 files changed, 395 insertions(+) create mode 100644 libavutil/hashtable.c create mode 100644 libavutil/hashtable.h create mode 100644 libavutil/tests/hashtable.c diff --git a/libavutil/Makefile b/libavutil/Makefile index 9ef118016b..43cb32c9d6 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -144,6 +144,7 @@ OBJS = adler32.o \ fixed_dsp.o \ frame.o \ hash.o \ + hashtable.o \ hdr_dynamic_metadata.o \ hdr_dynamic_vivid_metadata.o \ hmac.o \ @@ -272,6 +273,7 @@ TESTPROGS = adler32 \ file \ fifo \ hash \ + hashtable \ hmac \ hwdevice \ integer \ diff --git a/libavutil/hashtable.c b/libavutil/hashtable.c new file mode 100644 index 0000000000..155a264665 --- /dev/null +++ b/libavutil/hashtable.c @@ -0,0 +1,192 @@ +/* + * Generic hashtable + * Copyright (C) 2024 Connor Worley <connorbworley@gmail.com> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <string.h> + +#include "crc.h" +#include "error.h" +#include "mem.h" +#include "hashtable.h" + +#define ALIGN _Alignof(size_t) + +struct AVHashtableContext { + size_t key_size; + size_t key_size_aligned; + size_t val_size; + size_t val_size_aligned; + size_t entry_size; + size_t max_entries; + size_t utilization; + const AVCRC *crc; + uint8_t *table; + uint8_t *swapbuf; +}; + +#define ENTRY_PSL(entry) (entry) +#define ENTRY_OCC(entry) (ENTRY_PSL(entry) + FFALIGN(sizeof(size_t), ALIGN)) +#define ENTRY_KEY(entry) (ENTRY_OCC(entry) + FFALIGN(sizeof(size_t), ALIGN)) +#define ENTRY_VAL(entry) (ENTRY_KEY(entry) + ctx->key_size_aligned) + +#define KEYS_EQUAL(k1, k2) !memcmp(k1, k2, ctx->key_size) + +int av_hashtable_alloc(struct AVHashtableContext **ctx, size_t key_size, size_t val_size, size_t max_entries) +{ + struct AVHashtableContext *res = av_malloc(sizeof(struct AVHashtableContext)); + if (!res) + return AVERROR(ENOMEM); + res->key_size = key_size; + res->key_size_aligned = FFALIGN(key_size, ALIGN); + res->val_size = val_size; + res->val_size_aligned = FFALIGN(val_size, ALIGN); + res->entry_size = FFALIGN(sizeof(size_t), ALIGN) + + FFALIGN(sizeof(size_t), ALIGN) + + res->key_size_aligned + + res->val_size_aligned; + res->max_entries = max_entries; + res->utilization = 0; + res->crc = av_crc_get_table(AV_CRC_32_IEEE); + if (!res->crc) { + av_hashtable_freep(&res); + return AVERROR_BUG; + } + res->table = av_calloc(res->max_entries, res->entry_size); + if (!res->table) { + av_hashtable_freep(&res); + return AVERROR(ENOMEM); + } + res->swapbuf = av_calloc(2, res->key_size_aligned + res->val_size_aligned); + if (!res->swapbuf) { + av_hashtable_freep(&res); + return AVERROR(ENOMEM); + } + *ctx = res; + return 0; +} + +static size_t hash_key(const struct AVHashtableContext *ctx, const void *key) +{ + return av_crc(ctx->crc, 0, key, ctx->key_size) % ctx->max_entries; +} + +int av_hashtable_get(const struct AVHashtableContext *ctx, const void *key, void *val) +{ + size_t hash = hash_key(ctx, key); + + if (!ctx->utilization) + return 0; + + for (size_t psl = 0; psl < ctx->max_entries; psl++) { + size_t wrapped_index = (hash + psl) % ctx->max_entries; + uint8_t *entry = ctx->table + wrapped_index * ctx->entry_size; + if (!*ENTRY_OCC(entry) || *(size_t*)ENTRY_PSL(entry) < psl) + return 0; + if (KEYS_EQUAL(ENTRY_KEY(entry), key)) { + memcpy(val, ENTRY_VAL(entry), ctx->val_size); + return 1; + } + } + return 0; +} + +int av_hashtable_set(struct AVHashtableContext *ctx, const void *key, const void *val) +{ + int swapping = 0; + size_t psl = 0; + size_t hash = hash_key(ctx, key); + uint8_t *set = ctx->swapbuf; + uint8_t *tmp = ctx->swapbuf + ctx->key_size_aligned + ctx->val_size_aligned; + + memcpy(set, key, ctx->key_size); + memcpy(set + ctx->key_size_aligned, val, ctx->val_size); + + for (size_t i = 0; i < ctx->max_entries; i++) { + size_t wrapped_index = (hash + i) % ctx->max_entries; + uint8_t *entry = ctx->table + wrapped_index * ctx->entry_size; + if (!*ENTRY_OCC(entry) || (!swapping && KEYS_EQUAL(ENTRY_KEY(entry), set))) { + if (!*ENTRY_OCC(entry)) + ctx->utilization++; + *(size_t*)ENTRY_PSL(entry) = psl; + *ENTRY_OCC(entry) = 1; + memcpy(ENTRY_KEY(entry), set, ctx->key_size_aligned + ctx->val_size); + return 1; + } + if (*(size_t*)ENTRY_PSL(entry) < psl) { + if (ctx->utilization == ctx->max_entries) + return 0; + swapping = 1; + // set needs to swap with entry + memcpy(tmp, ENTRY_KEY(entry), ctx->key_size_aligned + ctx->val_size); + memcpy(ENTRY_KEY(entry), set, ctx->key_size_aligned + ctx->val_size); + FFSWAP(uint8_t*, set, tmp); + FFSWAP(size_t, psl, *(size_t*)ENTRY_PSL(entry)); + } + psl++; + } + return 0; +} + +int av_hashtable_delete(struct AVHashtableContext *ctx, const void *key) +{ + uint8_t *next_entry; + size_t hash = hash_key(ctx, key); + + if (!ctx->utilization) + return 0; + + for (size_t psl = 0; psl < ctx->max_entries; psl++) { + size_t wrapped_index = (hash + psl) % ctx->max_entries; + uint8_t *entry = ctx->table + wrapped_index * ctx->entry_size; + if (!*ENTRY_OCC(entry) || *(size_t*)ENTRY_PSL(entry) < psl) + return 0; + if (KEYS_EQUAL(ENTRY_KEY(entry), key)) { + *ENTRY_OCC(entry) = 0; + for (psl++; psl < ctx->max_entries; psl++) { + wrapped_index = (hash + psl) % ctx->max_entries; + next_entry = ctx->table + wrapped_index * ctx->entry_size; + if (!*ENTRY_OCC(next_entry) || !*(size_t*)ENTRY_PSL(next_entry)) { + ctx->utilization--; + return 1; + } + memcpy(entry, next_entry, ctx->entry_size); + (*(size_t*)ENTRY_PSL(entry))--; + *ENTRY_OCC(next_entry) = 0; + entry = next_entry; + } + } + }; + return 0; +} + +void av_hashtable_clear(struct AVHashtableContext *ctx) +{ + memset(ctx->table, 0, ctx->entry_size * ctx->max_entries); +} + +void av_hashtable_freep(struct AVHashtableContext **ctx) +{ + if (*ctx) { + av_freep(&(*ctx)->table); + av_freep(&(*ctx)->swapbuf); + } + av_freep(ctx); +} diff --git a/libavutil/hashtable.h b/libavutil/hashtable.h new file mode 100644 index 0000000000..8ce6faafe3 --- /dev/null +++ b/libavutil/hashtable.h @@ -0,0 +1,91 @@ +/* + * Generic hashtable + * Copyright (C) 2024 Connor Worley <connorbworley@gmail.com> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVUTIL_HASHTABLE_H +#define AVUTIL_HASHTABLE_H + +#include <stddef.h> + +/* Implements a hash table using Robin Hood open addressing. + * See: https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf + */ + +struct AVHashtableContext; + +/** + * Creates a fixed-sized Robin Hood hash table. + * + * @param ctx context to allocate and initialize + * @param key_size size of key type in bytes + * @param val_size size of value type in bytes + * @param max_entries maximum number of key-value pairs to store + * + * @return zero on success, nonzero on error + */ +int av_hashtable_alloc(struct AVHashtableContext **ctx, size_t key_size, size_t val_size, size_t max_entries); + +/** + * Looks up a value from a hash table given a key. + * + * @param ctx hash table context + * @param key pointer to key data + * @param val destination pointer for value data + * + * @return 1 if the key is found, zero if the key is not found + */ +int av_hashtable_get(const struct AVHashtableContext *ctx, const void *key, void *val); + +/** + * Stores a value in a hash table given a key. + * + * @param ctx hash table context + * @param key pointer to key data + * @param val pointer for value data + * + * @return 1 if the key is written, zero if the key is not written due to the hash table reaching max capacity + */ +int av_hashtable_set(struct AVHashtableContext *ctx, const void *key, const void *val); + +/** + * Deletes a value from a hash table given a key. + * + * @param ctx hash table context + * @param key pointer to key data + * + * @return 1 if the key is deleted, zero if the key is not deleted due to not being found + */ +int av_hashtable_delete(struct AVHashtableContext *ctx, const void *key); + +/** + * Deletes all values from a hash table. + * + * @param ctx hash table context + */ +void av_hashtable_clear(struct AVHashtableContext *ctx); + +/** + * Frees a hash table. + * + * @param ctx hash table context + */ +void av_hashtable_freep(struct AVHashtableContext **ctx); + +#endif diff --git a/libavutil/tests/hashtable.c b/libavutil/tests/hashtable.c new file mode 100644 index 0000000000..1e37d7663b --- /dev/null +++ b/libavutil/tests/hashtable.c @@ -0,0 +1,110 @@ +/* + * Generic hashtable tests + * Copyright (C) 2024 Connor Worley <connorbworley@gmail.com> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> + +#include "libavutil/avassert.h" +#include "libavutil/hashtable.h" + +int main(void) +{ + struct AVHashtableContext *ctx; + uint8_t k; + uint64_t v; + + // impossibly large allocation should fail gracefully + av_assert0(av_hashtable_alloc(&ctx, -1, -1, -1) < 0); + + // hashtable can store up to 3 uint8_t->uint64_t entries + av_assert0(!av_hashtable_alloc(&ctx, sizeof(k), sizeof(v), 3)); + + // unsuccessful deletes return 0 + k = 1; + av_assert0(!av_hashtable_delete(ctx, &k)); + + // unsuccessful gets return 0 + k = 1; + av_assert0(!av_hashtable_get(ctx, &k, &v)); + + // successful sets returns 1 + k = 1; + v = 1; + av_assert0(av_hashtable_set(ctx, &k, &v)); + + // get should now contain 1 + k = 1; + v = 0; + av_assert0(av_hashtable_get(ctx, &k, &v)); + av_assert0(v == 1); + + // updating sets should return 1 + k = 1; + v = 2; + av_assert0(av_hashtable_set(ctx, &k, &v)); + + // get should now contain 2 + k = 1; + v = 0; + av_assert0(av_hashtable_get(ctx, &k, &v)); + av_assert0(v == 2); + + // fill the table + k = 2; + v = 2; + av_assert0(av_hashtable_set(ctx, &k, &v)); + k = 3; + v = 3; + av_assert0(av_hashtable_set(ctx, &k, &v)); + + // inserting sets on a full table should return 0 + k = 4; + v = 4; + av_assert0(!av_hashtable_set(ctx, &k, &v)); + + // updating sets on a full table should return 1 + k = 1; + v = 4; + av_assert0(av_hashtable_set(ctx, &k, &v)); + v = 0; + av_assert0(av_hashtable_get(ctx, &k, &v)); + av_assert0(v == 4); + + // successful deletes should return 1 + k = 1; + av_assert0(av_hashtable_delete(ctx, &k)); + + // get should now return 0 + av_assert0(!av_hashtable_get(ctx, &k, &v)); + + // sanity check remaining keys + k = 2; + v = 0; + av_assert0(av_hashtable_get(ctx, &k, &v)); + av_assert0(v == 2); + k = 3; + v = 0; + av_assert0(av_hashtable_get(ctx, &k, &v)); + av_assert0(v == 3); + + av_hashtable_freep(&ctx); + + return 0; +} -- 2.48.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".