Skip to content

Improve rust version performance - #29

Open
qarmin wants to merge 4 commits into
KdotJPG:masterfrom
qarmin:improve_rust_speed
Open

Improve rust version performance#29
qarmin wants to merge 4 commits into
KdotJPG:masterfrom
qarmin:improve_rust_speed

Conversation

@qarmin

Copy link
Copy Markdown

The main issue was that instead of using OnceLock (which was stabilized about half a year after the initial version was merged), a custom implementation was used.
That implementation relied on unwrap in a critical section, which significantly reduced performance.

Additionally, it caused the following warnings in Rust 1.91.0:

warning: creating a shared reference to mutable static
--> rust/smooth.rs:831:9
|
831 | / STATIC_DATA.0.call_once(|| {
832 | | STATIC_DATA.1 = Some(initStaticData());
833 | | });
| |__________^ shared reference to mutable static
|
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives
warning: creating a shared reference to mutable static
--> rust/smooth.rs:834:9
|
834 | STATIC_DATA.1.as_ref().unwrap()
| ^^^^^^^^^^^^^^^^^^^^^^ shared reference to mutable static
|
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives
warning: `opensimplex2` (lib) generated 4 warnings

I’ve also added simple stability tests to prevent accidental changes to the function output in the future.
An additional example was included to make it easier to verify performance.

Old implementation - ~640ms

old

New implementation - ~490ms

new

@EndrII

Copy link
Copy Markdown

Nice work, now is really faster !
But still slower than C., i think The Rust is good balance choose, but performance choose it should be C.

MarcoCiaramella C Impl 2D: 623 msec
Deprecated C Impl 2D: 617 msec
Rust Impl 2D: 686 msec

@EndrIIEndrII left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job

@CryZe

CryZe commented Nov 11, 2025

Copy link
Copy Markdown

Based on this PR I made some further improvements by precomputing the lookup tables entirely at compile time: CryZe@cebabc3

Feel free to pull it into this PR.

@gabrielcfvg

Copy link
Copy Markdown

Based on this PR I made some further improvements by precomputing the lookup tables entirely at compile time: CryZe@cebabc3

Feel free to pull it into this PR.

I was working in a similar path, making the gradients to be initialized at compile time so that don't need to initialize the context at runtime, and more important, remove the synchronization/verification that happens in every access to the gradients.

@CryZe did just that and better than I would be able to. With his improvements, I got a 7% speedup over the original pull request implementation and 6.5% over the deprecated C implementation that was benchmarked by @EndrII.

CryZeand others added 2 commits November 11, 2025 18:42
This precomputes all lookup tables at compile time, so there's no
runtime performance hit anymore at all. As a side effect, this also
means the library is now entirely `no_std` compatible.
@EndrII

Copy link
Copy Markdown

and 6.5% over the deprecated C implementation that was benchmarked by @EndrII.

hm looks good, but i want to test it before merging

@EndrII

Copy link
Copy Markdown
 Compiling opensimplex2 v1.1.0 (/###/OpenSimplex2)
error: `#[panic_handler]` function required, but not found
error: could not compile `opensimplex2` (lib) due to 1 previous error

looks as optimistaions broken this library

@EndrIIEndrII left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build is failed

@qarmin

Copy link
Copy Markdown
Author

Library is now no_std by default, so --features std needs to be added to run/build command, to be able to use std functions like println

@gabrielcfvg

Copy link
Copy Markdown

and 6.5% over the deprecated C implementation that was benchmarked by @EndrII.

hm looks good, but i want to test it before merging

Be careful when profiling it, since the gradients are now constant, the used functions are pure, meaning that if the returned value is not used in some way like it wasn't in the original benchmarks, the compiler probably will wipe the function call away, making the timings be zero.

@EndrIIEndrII left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, amassing work !

MarcoCiaramella C Impl 2D: 626 msec
Deprecated C Impl 2D: 617 msec
Rust Impl 2D: 602 msec

@CryZe@qarmin@gabrielcfvg Thank you very much!!!
today i will integrate this library to my project )

@EndrIIEndrII mentioned this pull request Nov 12, 2025
Sign up for freeto 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.

4 participants

@qarmin@EndrII@CryZe@gabrielcfvg