-
-
Notifications
You must be signed in to change notification settings - Fork 77
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
Post: digging into struct initialization performance #827
Post: digging into struct initialization performance #827
Conversation
✅ Deploy Preview for crystal-website ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I left a few tips to improve it.
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Beta Ziliani <[email protected]>
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
IIRC this was first posted before class Digest
module ClassMethods
def digest(& : self ->) : Bytes
context_buf = uninitialized ReferenceStorage(self)
context = unsafe_construct(pointerof(context_buf))
begin
yield context
context.final
ensure
context.finalize if context.responds_to?(:finalize)
end
end
end
end
class Digest::Blake3 < ::Digest
extend ::Digest::ClassMethods
end
struct Blake3Hasher
# ditto
end
def blake3_hexstring(data)
hasher = Blake3Hasher.new
# ...
end
def blake3_hexstring_init(data)
hasher = uninitialized Blake3Hasher
hasher.init
# ...
end
Benchmark.ips do |x|
bytes = Random::Secure.random_bytes(32)
x.report("SHA256") { Digest::SHA256.hexdigest(bytes) }
x.report("Blake3 Digest") { Digest::Blake3.hexdigest(bytes) }
x.report("Blake3 new") { blake3_hexstring(bytes) }
x.report("Blake3 init") { blake3_hexstring_init(bytes) }
end
The remaining overhead most likely came from the mandatory memory zeroing for reference types. Without redefining
|
@HertzDevil Nice! I was thinking about this. A good demonstration of the experimental methods, and that RFC 4 could significantly improve performance! |
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Johannes Müller <[email protected]>
Co-authored-by: Johannes Müller <[email protected]>
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
_posts/2024-09-05-digging-into-struct-initialization-performance.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Johannes Müller <[email protected]>
@beta-ziliani I let you decide when to publish :) |
I also have slow structs and fast tuples here crystal-lang/crystal#14225 (comment), |
Issue: the kramdown gfm doesn't support the
> [!TIP]
annotation for callouts. I'm not sure how to replace these?