Skip to content

Conversation

@qarmin
Copy link

@qarmin qarmin commented Nov 11, 2025

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

EndrII commented Nov 11, 2025

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

Copy link

@EndrII EndrII left a comment

Choose a reason for hiding this comment

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

good job

@CryZe
Copy link

CryZe commented Nov 11, 2025

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

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.

CryZe and 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

EndrII commented Nov 11, 2025

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

EndrII commented Nov 11, 2025

   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

Copy link

@EndrII EndrII left a comment

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
Author

qarmin commented Nov 11, 2025

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

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.

Copy link

@EndrII EndrII left a comment

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 )

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.

4 participants