runtime: make leveldb the default map hash
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 12/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- go
- 領域
- compilers, performance
調査の方向性
Start with memhash_leveldb.go and memhash_fnv.go in the runtime and the build tags that currently select the hash per target; the proposal is to make LevelDB the default everywhere and keep FNV only behind runtime_memhash_fnv. Then look at tests/mapbench on the linked branch for the random-key benchmarks that need adding. Done means the default is switched, the full test suite passes across targets, and the new benchmarks are in place; the open inlining question is separate follow-up work.
索引モデルが issue の本文から書いたものです。
説明
FNV-1a is the default map hash on every target except wasip1. wasip1 already uses the LevelDB hash.
I propose using LevelDB on every target. On everything I could measure, LevelDB is faster for keys of about 8 bytes or longer. It is 9 to 30% slower for 4 byte keys on Cortex-M chips, which is 4 to 19 cycles per hash. On AVR it is 3 to 4 times faster at every key size. It adds 80 to 180 bytes of code per binary that uses maps.
The benchmarks, the data and the charts are on this branch (commit 46c61b18).
https://github.com/dgryski/tinygo/tree/mapbench-hash-benchmarks/tests/mapbench
How I measured
Keys are random int32 and int64 values, English words of 3 to 8 letters, HTTP header names, and 39 and 128 byte strings. The old map benchmarks used sequential keys, which make FNV look better than it is (see Distribution below).
The hash benchmark calls each hash through a function value, subtracts a no-op call, and reports net CPU cycles per call. Clock speeds are the nominal speed of each board. Each number is the median of 5 runs on one board.
I used these boards.
- Arduino Uno R4 WiFi (Cortex-M4)
- Pico 2 W (Cortex-M33)
- XIAO SAMD21 and Pico (both Cortex-M0+)
- micro:bit v1 (Cortex-M0)
- Arduino Uno R3 (AVR)
I also ran the map benchmarks on native arm64 and wasip1. RISC-V 32 under qemu and ESP32-S3 under esp-emu are emulated, so I only use them to check the shape of the results.
Hash cost on hardware
Net cycles per call, shown as fnv / leveldb (change).
| Board | 4 B | 6 B | 8 B | 14 B | 39 B | 128 B |
|---|---|---|---|---|---|---|
| Cortex-M4 (Uno R4 WiFi) | 50 / 56 (+12%) | 69 / 68 (-1%) | 82 / 66 (-20%) | 133 / 92 (-31%) | 332 / 145 (-56%) | 1043 / 366 (-65%) |
| Cortex-M33 (Pico 2 W) | 36 / 40 (+11%) | 49 / 50 (+1%) | 60 / 49 (-18%) | 96 / 69 (-28%) | 247 / 123 (-50%) | 780 / 319 (-59%) |
| Cortex-M0+ (XIAO SAMD21) | 64 / 83 (+30%) | 94 / 103 (+9%) | 105 / 108 (+3%) | 180 / 156 (-13%) | 419 / 295 (-30%) | 1313 / 833 (-37%) |
| Cortex-M0+ (Pico, RP2040) | 34 / 38 (+13%) | 48 / 45 (-5%) | 59 / 53 (-10%) | 96 / 75 (-22%) | 254 / 169 (-34%) | 809 / 503 (-38%) |
| Cortex-M0 (micro:bit v1) | 62 / 67 (+9%) | 88 / 80 (-9%) | 108 / 94 (-14%) | 178 / 131 (-26%) | 470 / 296 (-37%) | 1498 / 883 (-41%) |
LevelDB has a higher fixed cost per call and a lower cost per byte. The break even is between 4 and 8 bytes on every board.


AVR
AVR has no hardware 32 bit multiply in TinyGo. Each * is a software routine of about 1180 cycles. FNV does one per input byte and LevelDB does one per four bytes.
Net cycles per call, shown as fnv / leveldb (change).
| 4 B | 8 B | 14 B | 39 B |
|---|---|---|---|
| 4724 / 1411 (-70%) | 9541 / 2542 (-73%) | 16603 / 5111 (-69%) | 46552 / 12319 (-74%) |
These numbers are from simavr. A real Uno R3 gave the same FNV numbers and LevelDB numbers within 1%.

Map operations on native arm64 and wasip1
LevelDB relative to FNV.
| Benchmark | native | wasip1 |
|---|---|---|
| StringShortGet | +7% | +12% |
| IntGet | -13% | +9% |
| CompositeGet | +17% | +18% |
| HeaderGet | -12% | -5% |
| StringLongGet (39 B) | -41% | -39% |
| BigKeyGet (256 B) | -45% | -38% |
| BigKeySet | -50% | -28% |
| Clone, Grow, Clear | about the same | about the same |
The native numbers are single 200 ms runs. The wasip1 numbers are medians of 8 runs and the geometric mean is -0.4%. On these two targets the result is mixed. LevelDB wins clearly on long keys and loses 7 to 18% on very short keys and small structs.
Code size
| Target | runtime.hash32 fnv to leveldb | Program that uses a map |
|---|---|---|
| Cortex-M4 | 36 to 120 bytes | 6296 to 6456 (+160) |
| Cortex-M33 | 36 to 120 bytes | 9216 to 9376 (+160) |
| Cortex-M0+ | 36 to 140 bytes | 9544 to 9720 (+176) |
| RISC-V 32 | 40 to 174 bytes | 10928 to 11152 (+224) |
| wasip1 | 12608 to 12918 (+310) | |
| AVR | 160 to 284 bytes | 1463 to 1591 (+128) |
Distribution
With random ints, words and header names, FNV and LevelDB both match an ideal random hash. Overflow buckets, maximum bucket fill, 32 bit collisions and tophash clashes are all at the random level. For example 87.6 and 87.8 overflow buckets against 87.8 for random at n=16384.
They differ on structured keys.
- FNV gives zero overflow buckets for sequential ints at every size, which is better than random. LevelDB behaves like random. This is why sequential keys make FNV look good in Clone and Grow.
- For sequential strings like
key-N, FNV has a heavy tail across seeds (p95 of 166 overflow buckets against about 103 for random at n=16384). LevelDB is fine at n=16384 but weak at n=1024, with a maximum bucket fill of 15.1 against 10.6 for random. - For very short words at n=64, LevelDB's tophash clash rate is about 2.5 times random. I did not measure the time cost.
Keys chosen by an attacker
I tested whether keys that collide under one seed still collide under another, using header name keys.
- FNV is weak. The bucket index depends only on the low bits of the seed. A bucket flooding set found under one seed transfers fully to any seed with the same low 12 bits. 16.6% of 32 bit collision pairs survive under some other seed.
- LevelDB is much better. A flooding set does not transfer. 0.2% of collision pairs survive, each under 2 to 13% of seeds.
- tsip32 is the best. Nothing transferred in about 6000 pairs tested under 2000 seeds each.
The runtime seed is only 32 bits for all three, so none of them helps if an attacker can learn the seed.
Alternatives
Keep FNV and add a finalizer. I tried h ^= h>>15; h *= 0x2c1b3c6d; h ^= h>>12. It fixes the seed bit and sequential key behavior. It adds a fixed cost per call. That is 14 to 28 cycles on the Cortex-M boards, which is +41 to 53% for 4 byte keys and +14 to 20% for header names. On AVR it adds about 1180 cycles. It does not fix FNV's slow long key hashing or the collisions that survive across seeds.
Make tsip the default. It is the strongest against collisions but costs 1.7 to 2.5 times LevelDB in hash time and about 3 times the code. With runtime_memhash_tsip the build time evaluator also stops folding the unicode package maps. A small program that imports time grows from 9 KB to 43 KB of code and from 6 KB to 91 KB of RAM on RP2350. It does not link on boards with 32 KB of RAM. I do not know why. On AVR tsip32 is the fastest hash, 6 to 20 times faster than FNV, but it costs about 1.5 KB of flash.
Keep FNV on small cores or RISC-V 32. The data does not support this. LevelDB wins from about 8 bytes up on all three M0 and M0+ chips and in the RISC-V 32 emulator.
Proposed change
- Make
memhash_leveldb.gothe default on all targets. Selectmemhash_fnv.goonly withruntime_memhash_fnv. - Add the random key benchmarks from the branch above to
tests/mapbench.
Open items
- LevelDB's word loads must be inlined to get these numbers. In my benchmark copies of the hashes, TinyGo did not inline
encoding/binaryword loads on Cortex-M0+. Each load was a function call and that inflated the measured cost of LevelDB on M0 and M0+ by 18 to 32%, and of tsip32 and sip13 by up to about 30%. All numbers here use word loads written inline, which is how the runtime'sinternal/byteordercode compiles. I have not found why TinyGo does not inline theencoding/binaryversion. It reproduces in a full hash function but not in small programs. This is still an outstanding issue and could affect other code that reads words withencoding/binaryin a hot loop on these chips. - I have not measured Xtensa or RISC-V 32 on real hardware, Cortex-M3, Cortex-M7 or x86-64.
- I have not run the full test suite with LevelDB as the default on every target.
- Clock speeds are nominal and each board was measured once.
AI Assisted. My company policy requires this label on AI assisted work that is posted publicly.
- 主要言語
- Go
- スター
- 17.8k
- フォーク
- 1.1k
- 平均マージ
- 1日 4時間
- マージ済み PR(30日)
- 380
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
tinygo-org/tinygo のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
tinygo-org/tinygo#6727 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
tinygo-org/tinygo#6711 ·
メンテナーはふだん 1 日以内に返信
-
Builds are not reproducible: cgo snippet paths in debug info対応中かも @tridev-tej が 1 日前に担当しました。 オープンenhancement good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
tinygo-org/tinygo#6619 ·
メンテナーはふだん 1 日以内に返信
-
Fix the targets that tinygo build -target rejects対応中かも @narguis が 2 日前に担当しました。 オープンbug good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
tinygo-org/tinygo#6617 · コメント 5 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 62/100
tinygo-org/tinygo#6312 ·
メンテナーはふだん 1 日以内に返信
tinygo-org/tinygo の issue をすべて見る
似ている issue
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
sresarehumantoo/cc-artex#456 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
ollama/ollama#20021 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
factory:subtask
難易度 1/5 1時間未満 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
release
難易度 1/5 1時間未満 初心者へのやさしさ 75/100
github/gh-aw-mcpg#17603 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
bboerst/stratum-work#351 ·
メンテナーはふだん 1 日以内に返信