Skip to content

Use Slice literals in Crypto::Bcrypt#15781

Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom
HertzDevil:refactor/bcrypt-slice-literal
May 23, 2025
Merged

Use Slice literals in Crypto::Bcrypt#15781
straight-shoota merged 2 commits intocrystal-lang:masterfrom
HertzDevil:refactor/bcrypt-slice-literal

Conversation

@HertzDevil
Copy link
Contributor

These constants are all private and thus are allowed to go from StaticArray to Slice transparently.

@straight-shoota straight-shoota added this to the 1.17.0 milestone May 16, 2025
@s = uninitialized UInt32[1024]

@p.to_slice.copy_from(P.to_slice)
@s.to_slice.copy_from(S.to_slice)
Copy link
Collaborator

@ysbaddaden ysbaddaden May 16, 2025

Choose a reason for hiding this comment

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

Suggestion: I guess we should do the same for CYPHER_TEXT in Crypto::Bcrypt#hash_password, to avoid allocating a Slice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's just 24 bytes, I don't think that would have any observable impact on build or run times

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's still a pointless call to the GC at runtime.

Copy link
Member

@straight-shoota straight-shoota May 19, 2025

Choose a reason for hiding this comment

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

I'd consider this a regression.

Previously, CIPHER_TEXT was a static array, so dup didn't have any effect. It would already be copied to the stack anyway.
Now that we change CIPHER_TEXT to a slice, dup would introduce a new (and unnecessary) heap allocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could probably leave this one as a StaticArray then. We could even construct the array in the method body itself to remove the constant initializer overhead.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indeed, we don't need the constant.

Copy link
Member

Choose a reason for hiding this comment

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

We don't need it. But it's still nice to put explicit names on things. And to extract the data definition out of the algorithm.

We technically don't need P and S either. But I don't think we would like to inline them.

The constant initializer overhead should go away with true slice literals.

@straight-shoota straight-shoota removed this from the 1.17.0 milestone May 19, 2025
@straight-shoota straight-shoota added this to the 1.17.0 milestone May 21, 2025
@straight-shoota straight-shoota merged commit 7d4f904 into crystal-lang:master May 23, 2025
34 checks passed
@straight-shoota straight-shoota changed the title Use slice literals in Crypto::Bcrypt Use Slice literals in Crypto::Bcrypt May 23, 2025
@HertzDevil HertzDevil deleted the refactor/bcrypt-slice-literal branch May 23, 2025 09:00
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.

3 participants