From patchwork Thu Feb 22 01:09:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Connor Worley X-Patchwork-Id: 46427 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:26a3:b0:19e:cdac:8cce with SMTP id h35csp55083pze; Wed, 21 Feb 2024 17:09:41 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCVgqzq6oWO/HbbH8BumAz6/CS2cJxhFSsTz/rHl4KbMeEjQOrJkgmIaIszDUir9dDx7hfTQrIfOfGzuBBB0BKm9b0y5LROo3gj/cg== X-Google-Smtp-Source: AGHT+IFm7qzmeukpyPiGjCuHhAWBJCzn7LpRR45kfzrw53tRxdDxwQxqy/h4C8Gngqpk/zTCPT3A X-Received: by 2002:aa7:c68f:0:b0:564:d664:aca4 with SMTP id n15-20020aa7c68f000000b00564d664aca4mr4120887edq.16.1708564181207; Wed, 21 Feb 2024 17:09:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708564181; cv=none; d=google.com; s=arc-20160816; b=cjSAM8Kzc/mBvVz3IF29aCwT9cjYyLSpLq8fWaq2qlTc9Xws6PJZ3Tq6xllNRc0w2M bbObSh5RR4fwoSIPtDWpP2Z5yMgPoNUpLJLtj1n9XO/QQlsrDMR6ajYeZY1aUycY/EKx iC3k7ltmEeoNO3VDhLUkKf55/Mz0SEx21w91DU0F701YLcRKgKYu9lqE2eXkv+ROm7Cx uCzLA7rg0E4UYm6I1lB8bm/25pXzCpXmt6225TC6Zk3eaRmtUv2F0L+5vQwUcf8lE6YP 4lYNt5tvpUs7TkTT383p8QW4cCP4F/32DFi+vjbDkwKcqF+IctWyFO+qxKCQtuIaXomo f3/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=SD6Ob6C7IpShgcxh/JjabP2rYRRnz8m6t+6mAETr7Vs=; fh=4o9WedBEbOrq7GvNs0s+TOKeyzmDZRM3O7oe6ZP6vYM=; b=VGmy+/NaHuCBVUeo/EQuT8kns6DiD2vYJiSQzJnhobSybDtzhX0bvFOdwbYZZuasqN hAnNhErTsPgpKDxv6IvQG0/s+UjTJ1tNm7B1LQqMUyigWrJlEbuUrLLt9nzSRYkB4n2c DJUvF79X/Xzubty/v2NTB85iJtN45AzE0sRD3+CRpPhN7XRK34nAom8nvEigrLdbGKY5 ARyYA+tQuD+IZRmUa6gcuIIKsdrppBeaDKZqB6RZgpvKp7IojWNvayMscoQrKWNx9QRx Oy/rIELTCo1xji3FXfrsyUAoflsaDRJyNHzh+bq1ncmqr9CmTDK0VNKuVi9Jotrvd80R CikQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=WssujUZ3; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id t3-20020a056402524300b005642a2ec16dsi4559622edd.455.2024.02.21.17.09.40; Wed, 21 Feb 2024 17:09:41 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=WssujUZ3; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 28F8468D1A8; Thu, 22 Feb 2024 03:09:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oo1-f49.google.com (mail-oo1-f49.google.com [209.85.161.49]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8A3F268CE03 for ; Thu, 22 Feb 2024 03:09:30 +0200 (EET) Received: by mail-oo1-f49.google.com with SMTP id 006d021491bc7-59a8ee08c23so2735819eaf.2 for ; Wed, 21 Feb 2024 17:09:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708564168; x=1709168968; 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=Mqhnfpex1rV/yb8wsUXnd1rWsZQQxfcvtePLUIrjTkM=; b=WssujUZ30gAkMDe1WNcTYSTwHguFcXBgC4lEjBPBtFFazm2xzSdIcnjB+jR1UAUqb+ tF6cGKQp4vDoyJosO5UvEyiYCZFG7q4VN9uFXFUAlHB907ChGzxVvi+b+6Mn8er9IcVF HTAAj27klUKgWIVkGng2fcltG0sN0fwx5x6DfYGIXj87aI0zEBBcy6rra/HmsYfNRpf7 2kAd3r7Nm16ZKEvQOSr1O/iZvfyySmQE/FSELK/Ai6lyn9Ivx30QKDanQAUkLfptB12d ZbuBelUh9q14HGOam+YYQuWNuguGo/u0rfc0DJmu0D6LD3vXFNxVWcECq4C9YXh/7nT5 ZSYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708564168; x=1709168968; 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=Mqhnfpex1rV/yb8wsUXnd1rWsZQQxfcvtePLUIrjTkM=; b=d+1srj06JYxpyYDUWEfkARFFxrba3T+JRMIqcEATRcX+fC3q9Gvnmtyxc7QMKoHM8n Jz6lQOE71gCBg6hT4UknhCwm8VX4/G8ny7WYmzRjH1gVNF3FhFxg51xRP8a1HKkeUsKc eXyqBD/ELypG8zxC9PuAc977ZfiDv830f9dFTviJl5WDwedmiq3HjZOuAU8eX9JxpM44 FQBYmP1Z0XQ03+4gWFNWbxnUdkiGntUpZukALPEZID8ZNWa2N91gazXljm9DV0P2FR18 1wwaB/glUaf6CaD1GlPlp5D9BqihZKch+DtkR5yneq/sMeZfDSKt/LWS5p5mihaFnb4c Qb4g== X-Gm-Message-State: AOJu0YxrNdKKIGk+RXN4SWqlrSkTusfgH67bU8ZO3bEq4oERHlA7MIS3 cWnBkCF3YMUGsNl43JRGjL61Gr00tezSVAl4bG6EgY67KIF16ipXDk1Xfjs4 X-Received: by 2002:a4a:a8cc:0:b0:59f:8464:b004 with SMTP id r12-20020a4aa8cc000000b0059f8464b004mr16476761oom.0.1708564168065; Wed, 21 Feb 2024 17:09:28 -0800 (PST) Received: from localhost ([2607:fb90:9f8a:d954:c542:ca9a:41d4:eb18]) by smtp.gmail.com with UTF8SMTPSA id p4-20020a9d4544000000b006ddd0bb5a56sm1813674oti.52.2024.02.21.17.09.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 21 Feb 2024 17:09:27 -0800 (PST) From: Connor Worley To: ffmpeg-devel@ffmpeg.org Date: Wed, 21 Feb 2024 17:09:21 -0800 Message-Id: <20240222010922.67688-1-connorbworley@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 1/2] 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Connor Worley Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: jXI5ZrzfjYOh Signed-off-by: Connor Worley --- libavutil/Makefile | 2 + libavutil/hashtable.c | 192 ++++++++++++++++++++++++++++++++++++ libavutil/hashtable.h | 40 ++++++++ libavutil/tests/hashtable.c | 108 ++++++++++++++++++++ 4 files changed, 342 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 e7709b97d0..be75d464fc 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -138,6 +138,7 @@ OBJS = adler32.o \ fixed_dsp.o \ frame.o \ hash.o \ + hashtable.o \ hdr_dynamic_metadata.o \ hdr_dynamic_vivid_metadata.o \ hmac.o \ @@ -251,6 +252,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 + * + * 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 +#include + +#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..f14086867e --- /dev/null +++ b/libavutil/hashtable.h @@ -0,0 +1,40 @@ +/* + * Generic hashtable + * Copyright (C) 2024 Connor Worley + * + * 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 + +/* Implements a hash table using Robin Hood open addressing. + * See: https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf + */ + +struct AVHashtableContext; + +int av_hashtable_alloc(struct AVHashtableContext **ctx, size_t key_size, size_t val_size, size_t max_entries); +int av_hashtable_get(const struct AVHashtableContext *ctx, const void *key, void *val); +int av_hashtable_set(struct AVHashtableContext *ctx, const void *key, const void *val); +int av_hashtable_delete(struct AVHashtableContext *ctx, const void *key); +void av_hashtable_clear(struct AVHashtableContext *ctx); +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..0b38b34c5c --- /dev/null +++ b/libavutil/tests/hashtable.c @@ -0,0 +1,108 @@ +/* + * Generic hashtable tests + * Copyright (C) 2024 Connor Worley + * + * 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 "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; +} From patchwork Thu Feb 22 01:09:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Connor Worley X-Patchwork-Id: 46428 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:26a3:b0:19e:cdac:8cce with SMTP id h35csp55155pze; Wed, 21 Feb 2024 17:09:51 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCWi/2xHjkkWayej1QzhfsIaJbffARiAMQavNqT9qZo4CUMZQ1Dqez91IZsYA+7OfDVytUkyglB888sMCbG10Kl2JtR2jHWqnabR6A== X-Google-Smtp-Source: AGHT+IFTWd34IYmAFyTbHm87+Ki2v+Xkv0zgJObQUEdGAIaJsHxhQJOW63MP6nqWBiC9sYNM3dzo X-Received: by 2002:a17:906:3055:b0:a3f:11d5:2b64 with SMTP id d21-20020a170906305500b00a3f11d52b64mr3723929ejd.7.1708564190957; Wed, 21 Feb 2024 17:09:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708564190; cv=none; d=google.com; s=arc-20160816; b=gGU9DYMcUD6DNXo798Mi+CROxXsm0Z5+2XI6UiDQbEbl1m7GGItdURjLjiOAuHR/M6 7B0icpDjJWmSvVCBeDDtJQjLoqcwtWghrGpiOUq6r4n3n91AiZlP4BXtHe9BaHXXLOc/ qTJRj/0bo/9+gAf8j/SB+igBRyyMhOSndtbe0Q2yEaZPKnximu/Ovs/tWhsmjq4LVNru UC8n21ZITsFrrKYLkYGgTQ9B20sChbaDXLIOrDwDRS0ULt5zZXDcTQCLA1OVx8FdtUk3 KZNOIlI9okPexWXqMb6sjF7AlnhL06ffUW8+hRPBe7PpI3smKq4xc2Gjh8yOD/pEakip sS9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=toVuaj/5S8m5MmC83TvZ8MuFgfFHf9CoxnUPjKhRXss=; fh=4o9WedBEbOrq7GvNs0s+TOKeyzmDZRM3O7oe6ZP6vYM=; b=Ve8CBXkN21y9qkkrwRYx2lzwnFgcYY6O1GAg9fYSF+MTppV09FgEImt+8DwctuheMC DPmT8jU345QXRiXwIv1ulT3FX4bynKGbqnjOdKGyHPcoHxnXlu0JLgd/dwokgUMD9Ivp Btj5ryaThtnF++XLLwPmdt/clOrm7pQCs3QSAnlaeqvUsim/i62ElQJ1DcwFFJLCKy9l mvQRt87QM0O/in8E/eX3xen16WxdIhMUyHEWuU7yAp46X4pniePudrBdqrpX22FXt7Xh Fa+opVVDU1HGd3UgxDzMcn1W61XvIs8beV+BJmuWcZe9SnJNVSxYti4YEzP18ybdR8yC jStA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=OXuddUom; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id kl10-20020a170907994a00b00a3e73c92596si3630730ejc.171.2024.02.21.17.09.50; Wed, 21 Feb 2024 17:09:50 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=OXuddUom; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 372A868D1BC; Thu, 22 Feb 2024 03:09:40 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oa1-f51.google.com (mail-oa1-f51.google.com [209.85.160.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3788D68CE03 for ; Thu, 22 Feb 2024 03:09:34 +0200 (EET) Received: by mail-oa1-f51.google.com with SMTP id 586e51a60fabf-21e9589d4ffso687259fac.1 for ; Wed, 21 Feb 2024 17:09:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708564172; x=1709168972; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vKIzIPnaF74hTEBo6hTGII7uZnsVG1nelUp8vw/Df9M=; b=OXuddUomU/ziCGUIMZeTNzu2fZj8D0sMhvarnfpu3n9nt0v/L34/HCJEdcZ3VOMaRY 54IkV2h3RbEVFZmk0ZSamRjEwZZ8ts3j6CY6HDJrQ5y2XbAxlVYC591Z42yMTyzFOvGF CYyCHY9umPeBPNVSZWrVSe2epVucL3D2zX8cOt72Z1CulKi3YgwA0u/VO+DEuvMtp4cK Tzxl77eIAx/6teke1uVGoydWa2KGAGf/ckI366fBUqjQoZvhQpV224yZMSHLAz3EgwM5 sCdJbPbYJO0J6namoOo0u2zbEUpo1yesp79M8IC8rcTbHjPDWtxaW1P29nf18yLG5gT3 041A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708564172; x=1709168972; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vKIzIPnaF74hTEBo6hTGII7uZnsVG1nelUp8vw/Df9M=; b=PhdtYzKkR7MxyFn1npD6xa13+e1RbBo0zCP6FHFiTM/0m/ulYJJGkfxEOXmGxzZz3F fozyGiVfvlsb1ewLIqzXRFgcZD+kPNimbao+l7sL9i4Ngo1x+MOsB36u07C+HI0ycW3H Exmgf68NbPS7oBu+7w6GdCgEYdthvl+uSGVAXJbNEZzA4zIHEzoSASUbEPgIvJqup8zc 39vUkKJCd164x3Hu+OguEdkKKvOLk/Qx5liLOXlwGn0znwHW0heC5UUELLYCzb38RPsg ikMeKPujqQS4F8LmwBpBOFLj+u36VMBNVTPNkfMM+UAMv+9p8j0oddqWxFkmkqsy+Y7X mgDQ== X-Gm-Message-State: AOJu0YxrC63wznqpiEZqZef9qgw+MGsUp6BAnLnImjzP76E4Cl7Tm8x0 JnYtuH957WE/8FsNYZPAtRTx9sUJUIo33Lrvrk/Q3/RRxdOOS1q9QaAGjc3+ X-Received: by 2002:a05:6870:c48:b0:21e:45ff:120d with SMTP id lf8-20020a0568700c4800b0021e45ff120dmr16175392oab.44.1708564172037; Wed, 21 Feb 2024 17:09:32 -0800 (PST) Received: from localhost ([2607:fb90:9f8a:d954:c542:ca9a:41d4:eb18]) by smtp.gmail.com with UTF8SMTPSA id gq19-20020a056870d91300b0021e8424e466sm2615303oab.25.2024.02.21.17.09.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 21 Feb 2024 17:09:31 -0800 (PST) From: Connor Worley To: ffmpeg-devel@ffmpeg.org Date: Wed, 21 Feb 2024 17:09:22 -0800 Message-Id: <20240222010922.67688-2-connorbworley@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240222010922.67688-1-connorbworley@gmail.com> References: <20240222010922.67688-1-connorbworley@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 2/2] lavc/dxvenc: migrate DXT1 encoder to lavu hashtable X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Connor Worley Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: o3+DXJFegFI2 Offers a modest performance gain due to the switch from naive linear probling to robin hood. Signed-off-by: Connor Worley --- libavcodec/dxvenc.c | 121 ++++++++++++-------------------------------- 1 file changed, 33 insertions(+), 88 deletions(-) diff --git a/libavcodec/dxvenc.c b/libavcodec/dxvenc.c index 1ce2b1d014..980269657d 100644 --- a/libavcodec/dxvenc.c +++ b/libavcodec/dxvenc.c @@ -21,7 +21,7 @@ #include -#include "libavutil/crc.h" +#include "libavutil/hashtable.h" #include "libavutil/imgutils.h" #include "libavutil/opt.h" @@ -38,72 +38,9 @@ * appeared in the decompressed stream. Using a simple hash table (HT) * significantly speeds up the lookback process while encoding. */ -#define LOOKBACK_HT_ELEMS 0x40000 +#define LOOKBACK_HT_ELEMS 0x20202 #define LOOKBACK_WORDS 0x20202 -typedef struct HTEntry { - uint32_t key; - uint32_t pos; -} HTEntry; - -static void ht_init(HTEntry *ht) -{ - for (size_t i = 0; i < LOOKBACK_HT_ELEMS; i++) { - ht[i].pos = -1; - } -} - -static uint32_t ht_lookup_and_upsert(HTEntry *ht, const AVCRC *hash_ctx, - uint32_t key, uint32_t pos) -{ - uint32_t ret = -1; - size_t hash = av_crc(hash_ctx, 0, (uint8_t*)&key, 4) % LOOKBACK_HT_ELEMS; - for (size_t i = hash; i < hash + LOOKBACK_HT_ELEMS; i++) { - size_t wrapped_index = i % LOOKBACK_HT_ELEMS; - HTEntry *entry = &ht[wrapped_index]; - if (entry->key == key || entry->pos == -1) { - ret = entry->pos; - entry->key = key; - entry->pos = pos; - break; - } - } - return ret; -} - -static void ht_delete(HTEntry *ht, const AVCRC *hash_ctx, - uint32_t key, uint32_t pos) -{ - HTEntry *removed_entry = NULL; - size_t removed_hash; - size_t hash = av_crc(hash_ctx, 0, (uint8_t*)&key, 4) % LOOKBACK_HT_ELEMS; - - for (size_t i = hash; i < hash + LOOKBACK_HT_ELEMS; i++) { - size_t wrapped_index = i % LOOKBACK_HT_ELEMS; - HTEntry *entry = &ht[wrapped_index]; - if (entry->pos == -1) - return; - if (removed_entry) { - size_t candidate_hash = av_crc(hash_ctx, 0, (uint8_t*)&entry->key, 4) % LOOKBACK_HT_ELEMS; - if ((wrapped_index > removed_hash && (candidate_hash <= removed_hash || candidate_hash > wrapped_index)) || - (wrapped_index < removed_hash && (candidate_hash <= removed_hash && candidate_hash > wrapped_index))) { - *removed_entry = *entry; - entry->pos = -1; - removed_entry = entry; - removed_hash = wrapped_index; - } - } else if (entry->key == key) { - if (entry->pos <= pos) { - entry->pos = -1; - removed_entry = entry; - removed_hash = wrapped_index; - } else { - return; - } - } - } -} - typedef struct DXVEncContext { AVClass *class; @@ -120,10 +57,8 @@ typedef struct DXVEncContext { DXVTextureFormat tex_fmt; int (*compress_tex)(AVCodecContext *avctx); - const AVCRC *crc_ctx; - - HTEntry color_lookback_ht[LOOKBACK_HT_ELEMS]; - HTEntry lut_lookback_ht[LOOKBACK_HT_ELEMS]; + struct AVHashtableContext *color_ht; + struct AVHashtableContext *lut_ht; } DXVEncContext; /* Converts an index offset value to a 2-bit opcode and pushes it to a stream. @@ -158,27 +93,32 @@ static int dxv_compress_dxt1(AVCodecContext *avctx) DXVEncContext *ctx = avctx->priv_data; PutByteContext *pbc = &ctx->pbc; uint32_t *value; - uint32_t color, lut, idx, color_idx, lut_idx, prev_pos, state = 16, pos = 2, op = 0; + uint32_t color, lut, idx, color_idx, lut_idx, prev_pos, state = 16, pos = 0, op = 0; - ht_init(ctx->color_lookback_ht); - ht_init(ctx->lut_lookback_ht); + av_hashtable_clear(ctx->color_ht); + av_hashtable_clear(ctx->lut_ht); bytestream2_put_le32(pbc, AV_RL32(ctx->tex_data)); + av_hashtable_set(ctx->color_ht, ctx->tex_data, &pos); + pos++; bytestream2_put_le32(pbc, AV_RL32(ctx->tex_data + 4)); - - ht_lookup_and_upsert(ctx->color_lookback_ht, ctx->crc_ctx, AV_RL32(ctx->tex_data), 0); - ht_lookup_and_upsert(ctx->lut_lookback_ht, ctx->crc_ctx, AV_RL32(ctx->tex_data + 4), 1); + av_hashtable_set(ctx->lut_ht, ctx->tex_data + 4, &pos); + pos++; while (pos + 2 <= ctx->tex_size / 4) { idx = 0; + color_idx = 0; + lut_idx = 0; color = AV_RL32(ctx->tex_data + pos * 4); - prev_pos = ht_lookup_and_upsert(ctx->color_lookback_ht, ctx->crc_ctx, color, pos); - color_idx = prev_pos != -1 ? pos - prev_pos : 0; + if (av_hashtable_get(ctx->color_ht, &color, &prev_pos)) + color_idx = pos - prev_pos; + av_hashtable_set(ctx->color_ht, &color, &pos); + if (pos >= LOOKBACK_WORDS) { uint32_t old_pos = pos - LOOKBACK_WORDS; - uint32_t old_color = AV_RL32(ctx->tex_data + old_pos * 4); - ht_delete(ctx->color_lookback_ht, ctx->crc_ctx, old_color, old_pos); + if (av_hashtable_get(ctx->color_ht, ctx->tex_data + old_pos * 4, &prev_pos) && prev_pos <= old_pos) + av_hashtable_delete(ctx->color_ht, ctx->tex_data + old_pos * 4); } pos++; @@ -187,13 +127,14 @@ static int dxv_compress_dxt1(AVCodecContext *avctx) idx = color_idx; } else { idx = 0; - prev_pos = ht_lookup_and_upsert(ctx->lut_lookback_ht, ctx->crc_ctx, lut, pos); - lut_idx = prev_pos != -1 ? pos - prev_pos : 0; + if (av_hashtable_get(ctx->lut_ht, &lut, &prev_pos)) + lut_idx = pos - prev_pos; + av_hashtable_set(ctx->lut_ht, &lut, &pos); } if (pos >= LOOKBACK_WORDS) { uint32_t old_pos = pos - LOOKBACK_WORDS; - uint32_t old_lut = AV_RL32(ctx->tex_data + old_pos * 4); - ht_delete(ctx->lut_lookback_ht, ctx->crc_ctx, old_lut, old_pos); + if (av_hashtable_get(ctx->lut_ht, ctx->tex_data + old_pos * 4, &prev_pos) && prev_pos <= old_pos) + av_hashtable_delete(ctx->lut_ht, ctx->tex_data + old_pos * 4); } pos++; @@ -305,11 +246,12 @@ static av_cold int dxv_init(AVCodecContext *avctx) return AVERROR(ENOMEM); } - ctx->crc_ctx = av_crc_get_table(AV_CRC_32_IEEE); - if (!ctx->crc_ctx) { - av_log(avctx, AV_LOG_ERROR, "Could not initialize CRC table.\n"); - return AVERROR_BUG; - } + ret = av_hashtable_alloc(&ctx->color_ht, sizeof(uint32_t), sizeof(uint32_t), LOOKBACK_HT_ELEMS); + if (ret < 0) + return ret; + ret = av_hashtable_alloc(&ctx->lut_ht, sizeof(uint32_t), sizeof(uint32_t), LOOKBACK_HT_ELEMS); + if (ret < 0) + return ret; return 0; } @@ -320,6 +262,9 @@ static av_cold int dxv_close(AVCodecContext *avctx) av_freep(&ctx->tex_data); + av_hashtable_freep(&ctx->color_ht); + av_hashtable_freep(&ctx->lut_ht); + return 0; }