Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for static_init #1

Open
Cryptjar opened this issue Jan 18, 2022 · 1 comment
Open

Add support for static_init #1

Cryptjar opened this issue Jan 18, 2022 · 1 comment

Comments

@Cryptjar
Copy link
Owner

As @cofinite mentioned in cofinite/enum_properties#6 (comment), static_init might be an interesting alternative to lazy_static for lazy properties. However, since static_init uses some OS features (it does not work on bare-metal nor WebAssembly), lazy_static should remain an option.

The hope is that static_init could give essentially the same performance as const initialized properties while still allowing lazily initialized values.

@Cryptjar
Copy link
Owner Author

Cryptjar commented Jan 18, 2022

Unfortunately, my first tests with static_init do not indicate the hoped performance gains, albeit, I'm not experienced with writing Rust benchmarks. I added benchmarks comparing the duration of 1000 accesses to const initialized properties with lazily initialized properties in 06bb02a, which still uses lazy_static. On a different branch, I replaced lazy_static with static_init in d363708. These are the two commits that I used in my benchmarks.

My results are as follows:

With lazy_static (06bb02a)

test benchs::quad_1000_const_access      ... bench:         838 ns/iter (+/- 92)
test benchs::quad_1000_lazy_access       ... bench:       1,363 ns/iter (+/- 100)
test benchs::singelton_1000_const_access ... bench:         285 ns/iter (+/- 13)
test benchs::singelton_1000_lazy_access  ... bench:         657 ns/iter (+/- 23)

With static_init (using #[dynamic]) (d363708)

test benchs::quad_1000_const_access      ... bench:         818 ns/iter (+/- 38)
test benchs::quad_1000_lazy_access       ... bench:       1,353 ns/iter (+/- 51)
test benchs::singelton_1000_const_access ... bench:         281 ns/iter (+/- 11)
test benchs::singelton_1000_lazy_access  ... bench:         553 ns/iter (+/- 21)

It seems that static_init is mostly on par with lazy_static. However, static_init also supports true lazy which should compare even better with lazy_static, thus I tried that too:

With static_init (#[dynamic(lazy)])

test benchs::quad_1000_const_access      ... bench:         835 ns/iter (+/- 54)
test benchs::quad_1000_lazy_access       ... bench:       2,731 ns/iter (+/- 118)
test benchs::singelton_1000_const_access ... bench:         282 ns/iter (+/- 12)
test benchs::singelton_1000_lazy_access  ... bench:       1,349 ns/iter (+/- 53)

So, for true lazy statics, lazy_static actually outperforms static_init, however, this hardly of interest.

The bottom line is that apparently, static_init with it's #[dynamic] hardly beats lazy_static in particular the quad_1000_lazy_access run is well within the noise, just in the singelton_1000_lazy_access run static_init is a bit faster but not as fast as the true const properties. Also, I regard the singelton run as less representative, because it uses an enum with a single variant, whereas quad has four.

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

No branches or pull requests

1 participant