Skip to content

Rust is 3.2x faster than Moonbit if you don't handicap it - #15

Open
bitemyapp wants to merge 1 commit into
moonbit-community:mainfrom
bitemyapp:main
Open

Rust is 3.2x faster than Moonbit if you don't handicap it#15
bitemyapp wants to merge 1 commit into
moonbit-community:mainfrom
bitemyapp:main

Conversation

@bitemyapp

@bitemyapp bitemyapp commented Sep 13, 2025

Copy link
Copy Markdown
image

I can make the Rust implementation faster again but I think this suffices. This took 5 minutes with GPT-5. I added a test against rustfft to verify the iterative cooley-tukey but didn't extensively verify otherwise.

@peter-jerry-ye-code-review

Copy link
Copy Markdown
Dependency configuration issue - rustfft should be a dev dependency, not regular dependency

Category
Correctness
Code Snippet
Cargo.toml lines 19-21:
[dev-dependencies]
rustfft = "6"
num-complex = "0.4"
Recommendation
Move rustfft and num-complex to [dev-dependencies] section only, as they are only used in tests
Reasoning
These dependencies are only needed for testing against the reference implementation, not for the actual FFT functionality. Having them as regular dependencies would unnecessarily bloat the final binary.

@bitemyapp

Copy link
Copy Markdown
Author

Dependency configuration issue - rustfft should be a dev dependency, not regular dependency

It is a dev dependency. Bad clanker, no tokens.

image

Comment thread fft/rs/src/lib.rs Outdated
@bitemyapp

bitemyapp commented Sep 13, 2025

Copy link
Copy Markdown
Author

I've made the bench runner script uv compatible so it's easier for people to run.

Here's the matplotlib from a run on Linux w/ Swift disabled

bench_avg

@bitemyapp

Copy link
Copy Markdown
Author

Rust is 3.2x faster than Moonbit now

image

@bitemyapp bitemyapp changed the title Rust is 2.3x faster than Moonbit if you don't handicap it Rust is 3.2x faster than Moonbit if you don't handicap it Sep 13, 2025
@bitemyapp

Copy link
Copy Markdown
Author

Original benchmark was a 14" Macbook Pro w/ an M3 Pro. Latest benchmarks are 9800X3D / Ubuntu 25.04.

@bitemyapp

Copy link
Copy Markdown
Author

Updated chart:

bench_avg

Add tests
the bounds-check factoring is faster if you pass target-cpu=native for auto-vectorization
throw-away rayon version for giggles, 2-2.6x faster than single-threaded Rust, 6x faster than Moonbit
@bitemyapp

Copy link
Copy Markdown
Author

I've squashed my commits for a clean merge.

@bitemyapp

Copy link
Copy Markdown
Author

One thing I want to note is that I was very conscious of not making the Rust implementation dissimilar to the other implementations.

Things I didn't do but could've and might've in a production implementation:

  • Use a library
  • explicit SoA layouts per stage instead of the Complex struct
  • Use AVX-512 or std::simd
  • Caching (something I've done for NTTs in the past)
  • Use a more clever version of Cooley-Tukey than the benchmark has implemented
  • Multi-threading: I'm getting instruction-level parallelism already, but I mean stuff like rayon. This is implemented but it isn't part of the benchmark comparison suite because I felt it would be unfair.
  • Split-radix FFT
  • Stockham autosort, skips bit-reversal pass
  • Four-/Six-step CT
  • Bluestein
  • SIMD-fused butterflies
  • Explicit chunking/fusing more generally (goes with the SIMD part)
  • Plan by cache level, goes with some of the other items that are intended to be cache-friendly for work-sizes

NTT specific:

  • Harvey butterfly
  • Montgomery
  • Radix-4/8 stages
  • Negacyclic via Harvey
  • Good–Thomas (factor with coprime)

@issazhang97

Copy link
Copy Markdown

I don't understand why a new programming language wouldn't focus on improving its ecosystem instead of raising competition.

@metdxt

metdxt commented Sep 15, 2025

Copy link
Copy Markdown

I suggest rewriting MoonBit version to use iterative approach as well.

@Xerxes-2

Copy link
Copy Markdown

Would curious to see a C# implementation too, might look into it tonight

@AXiX-official

Copy link
Copy Markdown

Would curious to see a C# implementation too, might look into it tonight

#14

@metdxt

metdxt commented Sep 15, 2025

Copy link
Copy Markdown

ACTUAL results if all langs use same iterative approach:

==========================================================================================
Benchmark Summary (execution time in ms)
==========================================================================================
Program                Input   Runs      Fastest      Slowest       Median      Average
---------------------------------------------------------------------------------------
rust                      18     10     6.398 ms     7.693 ms     6.832 ms     6.974 ms
moonbit                   18     10     7.385 ms     8.564 ms     7.663 ms     7.792 ms
swift                     18     10     7.680 ms     8.097 ms     7.792 ms     7.851 ms
go                        18     10     7.625 ms     9.392 ms     8.517 ms     8.458 ms
---------------------------------------------------------------------------------------
rust                      20     10    28.933 ms    34.256 ms    30.993 ms    31.268 ms
moonbit                   20     10    31.816 ms    41.649 ms    33.333 ms    35.327 ms
swift                     20     10    35.505 ms    40.769 ms    37.939 ms    38.111 ms
go                        20     10    36.167 ms    37.054 ms    36.398 ms    36.457 ms
---------------------------------------------------------------------------------------
rust                      22     10   157.193 ms   175.013 ms   163.395 ms   164.446 ms
moonbit                   22     10   168.938 ms   211.071 ms   181.349 ms   187.027 ms
swift                     22     10   177.935 ms   203.525 ms   179.916 ms   186.071 ms
go                        22     10   183.986 ms   219.664 ms   184.948 ms   194.611 ms
==========================================================================================

Nowhere near "3.2x faster"

bench_avg

@evrins

evrins commented Sep 15, 2025

Copy link
Copy Markdown

I run bench on my m2 pro macbook
very strange result

golang is running faster then moonbit

moon: v0.6.24+012953835
swift: 6.1.2
go: 1.25.1
rust: 1.89

==========================================================================================
Benchmark Summary (execution time in ms)
==========================================================================================
Program                Input   Runs      Fastest      Slowest       Median      Average
---------------------------------------------------------------------------------------
rust                      18     10     7.067 ms    11.483 ms     7.996 ms     8.360 ms
moonbit                   18     10    43.419 ms    57.961 ms    44.022 ms    45.367 ms
swift                     18     10    48.078 ms    49.024 ms    48.224 ms    48.448 ms
go                        18     10    18.471 ms    23.039 ms    18.677 ms    19.172 ms
---------------------------------------------------------------------------------------
rust                      20     10    31.569 ms    32.103 ms    31.674 ms    31.738 ms
moonbit                   20     10   190.741 ms   211.060 ms   192.657 ms   194.258 ms
swift                     20     10   192.656 ms   194.677 ms   193.792 ms   193.784 ms
go                        20     10    78.185 ms    81.469 ms    79.172 ms    79.535 ms
---------------------------------------------------------------------------------------
rust                      22     10   146.832 ms   169.948 ms   148.303 ms   151.060 ms
moonbit                   22     10   822.612 ms   832.871 ms   828.299 ms   828.003 ms
swift                     22     10   798.417 ms   821.914 ms   803.131 ms   804.657 ms
go                        22     10   336.877 ms   358.378 ms   341.026 ms   343.061 ms
==========================================================================================
bench_avg

@woat

woat commented Oct 24, 2025

Copy link
Copy Markdown

lol

@ShalokShalom

Copy link
Copy Markdown

I don't understand why a new programming language wouldn't focus on improving its ecosystem instead of raising competition.

This is improving the programming ecosystem, similar to how Rust improved it. 🙂

@ShalokShalom

Copy link
Copy Markdown

ACTUAL results if all langs use same iterative approach:

This looks sound. How can I replicate it to compare today's versions, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants