Skip to content

[Discuss][C++] Replace MemoTable with a SwissTable implementation #38372

Open

Description

@js8544

Describe the enhancement requested

This is a follow up of #36059 (comment). There are many cases where we use a MemoTable, e.g. set lookup functions, vector hash functions, the count_distinct aggregate function, dictionary unification etc. Their performance can be boosted with a SwissTable.

We can either:

  1. Use an existing swiss table library. This requires some work on dependency management. I recommend absl::flat_hash_map since they are the original author of swiss tables and we already has absl in our 3rd party toolchain.
  2. Write one by ourselves. The current one in acero is too customized for the join node and it seems hard to extract a general hashtable from it. If I were to write one I would probably follow the structure of absl's and replace things like memory management and bit tweaking with Arrow ones.

What do you think? @pitrou @westonpace

Component(s)

C++

Activity

  1. pitrou commented on Oct 20, 2023

    @pitrou
    Member

    I don't know what a "Swiss table" is. Can you point to a general description?

  2. pitrou commented on Oct 20, 2023

    @pitrou
    Member

    More generally, I'm all for a faster associative table implementation.

  3. mapleFU commented on Oct 20, 2023

    @mapleFU
    Member

    Maybe we should first having an memory-usage / performance benchmark on this?

  4. js8544 commented on Oct 20, 2023

    @js8544
    ContributorAuthor

    There's an Abseil's article and a CppCon talk that provide a good intro. The key idea is, when looking up a key via linear probing, it can check multiple slots at once.

  5. pitrou commented on Oct 20, 2023

    @pitrou
    Member

    Ah, so it's a hash table with SIMD-optimized lookup?

  6. js8544 commented on Oct 20, 2023

    @js8544
    ContributorAuthor

    Maybe we should first having an memory-usage / performance benchmark on this?

    Okay, I'll try to draft a proof of concept with the is_in kernel.

  7. pitrou commented on Oct 20, 2023

    @pitrou
    Member

    If you're going to use SIMD lookups, then I would ask that it relies on xsimd instead of intrinsics.

  8. js8544 commented on Oct 20, 2023

    @js8544
    ContributorAuthor

    Ah, so it's a hash table with SIMD-optimized lookup?

    Well it's a speedup even without SIMD instructions. To put it in a oversimplified term each slot occupies 1 byte so with int64 we can already check 8 slots at the same time. But SIMD can definitely make it even faster.

    If you're going to use SIMD lookups, then I would ask that it relies on xsimd instead of intrinsics.

    Sure, if we decide to write our own version.

  9. pitrou commented on Oct 20, 2023

    @pitrou
    Member

    Well it's a speedup even without SIMD instructions. To put it in a oversimplified term each slot occupies 1 byte so with int64 we can already check 8 slots at the same time. But SIMD can definitely make it even faster.

    I see, thanks for the explanation.

    Sure, if we decide to write our own version.

    Well, we don't want to depend on Abseil, so our own version it should probably be :-)

  10. llama90 commented on Oct 20, 2023

    @llama90
    Contributor

    There's an Abseil's article and a CppCon talk that provide a good intro. The key idea is, when looking up a key via linear probing, it can check multiple slots at once.

    Thank you for explaining. I was also curious about what Swiss Join is too. :)

  11. js8544 commented on Oct 24, 2023

    @js8544
    ContributorAuthor

    I wrote a simple proof of concept for IndexIn [1] with a slightly tweaked version of absl::flat_hash_map. (Tweak is [2], a couple of operations can be skipped because our MemoTable doesn't need to support deletion). And the existing IndexIn benchmarks (results below) show about 20% ~ 60% improvement depending on the value_set size, the larger the better.

    The benchmarks are run on my Mac M1 so the results may vary on a x86 machine. But I think the diff is compelling enough for us to proceed with a Swiss table implementation.

    [1] https://github.com/apache/arrow/compare/main...js8544:jinshang/swiss_proof_of_concept?diff=split#diff-d288c253c3a5959aa1b6840a649a8dafe42d14ac49d1837b19dd3a44c58f7fd9R93
    [2] 7d14d10

    Benchmark Results

    Before

    image

    After

    image

  12. pitrou commented on Oct 24, 2023

    @pitrou
    Member

    Can you post benchmark numbers on string values? While hashing integers is nice, I'm not sure that's a dominant use case.

  13. js8544 commented on Oct 24, 2023

    @js8544
    ContributorAuthor

    Sure. I chose Int32 because it was the only one tested with a large value set. I just wrote a similar set of benchmarks for strings and here's the result.

    Before

    image

    After

    image
  14. pitrou commented on Oct 24, 2023

    @pitrou
    Member

    Ok. I agree we could replace MemoTable with something better. However, we don't want to have a dependency on Abseil, so it will have to be reimplemented.

  15. js8544 commented on Oct 24, 2023

    @js8544
    ContributorAuthor

    Ok. I agree we could replace MemoTable with something better. However, we don't want to have a dependency on Abseil, so it will have to be reimplemented.

    Yes. I'll implement one using xsimd as you suggested. Also the swiss table may be worse than MemoTable on super small tables, since it checks 8 slots at once and reserves at least 8 slots on construction, as shown in the first two string benchmarks. We'll need further benchmarking to decide which hashtable to use in each individual use cases, or maybe with a dynamic dispatching mechanism.

  16. mapleFU commented on Mar 8, 2024

    @mapleFU
    Member

    I guess this might benifit the parquet dictionary encoding, which is widely used in parquet. If nobody move forward, I'll have a try next month

  17. js8544 commented on Mar 8, 2024

    @js8544
    ContributorAuthor

    Yeah I'm afraid I won't have time to do this recently. Please have a try if you wish. Thanks!

  18. zanmato1984 commented on Mar 8, 2024

    @zanmato1984
    Contributor

    Will the swiss table currently being used in acero's hash join (aka. swiss join) help on this?

  19. js8544 commented on Mar 8, 2024

    @js8544
    ContributorAuthor

    The swiss table for hash join is very specialized and kind of hard to generalize. It can't be directly used for memotable.

  20. SGZW commented on Mar 12, 2024

    @SGZW
    Contributor

    Could I try to solve this issue?

  21. pitrou commented on Mar 12, 2024

    @pitrou
    Member

    @SGZW Anyone can work on an issue without asking for permission. Feel free to experiment and submit a PR when ready.

  22. added a commit that references this issue on Mar 31, 2024
  23. added
    Status: stale-warning
    on Feb 18, 2026
  24. thisisnic commented on Feb 18, 2026

    @thisisnic
    Member

    This issue has been marked as stale because it has had no activity in the past 2 years. Please remove the stale label or comment below, or this issue will be closed in 14 days.

  25. SGZW commented on Feb 24, 2026

    @SGZW
    Contributor

    This issue has been marked as stale because it has had no activity in the past 2 years. Please remove the stale label or comment below, or this issue will be closed in 14 days.

    i will finish this issue soon

  26. removed
    Status: stale-warning
    on Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions