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

Optimized Implementation of Array#fill for Zero Values #8903

Merged
merged 2 commits into from
Mar 20, 2020

Conversation

toddsundsted
Copy link
Contributor

See discussion here:

https://forum.crystal-lang.org/t/fastest-and-iconic-way-to-zero-an-array/1799/16

This PR provides a high performance (~10x) means to initialize arrays of integers to zero by optimizing that case in Array#fill.

Using compiled compiler at .build/crystal
          fill(0)   2.50M (400.01ns) (± 1.82%)  0.0B/op   1.43× slower
       fill(0, 0)   2.52M (397.50ns) (± 2.39%)  0.0B/op   1.42× slower
fill(0, 0, 1_000)   3.57M (279.82ns) (± 2.95%)  0.0B/op        fastest
      fill(0, ..)   2.48M (403.21ns) (± 3.81%)  0.0B/op   1.44× slower
Using compiled compiler at .build/crystal
          fill(0)  23.72M ( 42.16ns) (± 4.14%)  0.0B/op   1.15× slower
       fill(0, 0)  25.42M ( 39.34ns) (± 2.81%)  0.0B/op   1.08× slower
fill(0, 0, 1_000)  27.29M ( 36.65ns) (± 5.10%)  0.0B/op   1.00× slower
      fill(0, ..)  27.37M ( 36.54ns) (± 3.41%)  0.0B/op        fastest

@asterite
Copy link
Member

Thank you for this! ❤️

Using compiled compiler at .build/crystal

Just a note: when making changes to the standard library (not the compiler) there's no need to compiler the compiler to try things out. Just using bin/crystal will pick the new standard library (while crystal will use the installed standard library).

Copy link
Member

@asterite asterite left a comment

Choose a reason for hiding this comment

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

Looks great!

@toddsundsted
Copy link
Contributor Author

toddsundsted commented Mar 13, 2020

6769781 adds support for arrays for floating point numbers, as well (positive zero). this should cover arrays of all non-union numeric types, and should be suitable for most computational purposes.

src/array.cr Show resolved Hide resolved
@asterite asterite added this to the 0.34.0 milestone Mar 20, 2020
@asterite asterite merged commit aee6cee into crystal-lang:master Mar 20, 2020
@RX14
Copy link
Contributor

RX14 commented Mar 20, 2020

Didn't even have time to merge it myself :)

@toddsundsted toddsundsted deleted the patch-fill branch March 20, 2020 20:37
@toddsundsted
Copy link
Contributor Author

woo! my first legit contribution!

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

Successfully merging this pull request may close these issues.

4 participants