runtime: make leveldb the default map hash

Đang mở
#6,347 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
12/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
go
Lĩnh vực
compilers, performance

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

hash cycles

cost relative to leveldb

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%.

avr cycles

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

  1. Make memhash_leveldb.go the default on all targets. Select memhash_fnv.go only with runtime_memhash_fnv.
  2. 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/binary word 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's internal/byteorder code compiles. I have not found why TinyGo does not inline the encoding/binary version. 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 with encoding/binary in 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.

Ngôn ngữ chính
Go
Star
17.8k
Fork
1.1k
Merge trung bình
1 ngày 4 giờ
Pull request đã merge (30 ngày)
380

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của tinygo-org/tinygo

Tất cả issue của tinygo-org/tinygo

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.