-
Notifications
You must be signed in to change notification settings - Fork 88
Accelerate fetching attributes from an object handle - cleaned up version #341
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
Accelerate fetching attributes from an object handle - cleaned up version #341
Conversation
Signed-off-by: Eric Devolder <[email protected]>
…rtcard token when benchmarking Signed-off-by: Eric Devolder <[email protected]>
Signed-off-by: Eric Devolder <[email protected]>
Signed-off-by: Eric Devolder <[email protected]>
Signed-off-by: Eric Devolder <[email protected]>
Signed-off-by: Eric Devolder <[email protected]>
Signed-off-by: Eric Devolder <[email protected]>
… in scope of this PR Signed-off-by: Eric Devolder <[email protected]>
Signed-off-by: Eric Devolder <[email protected]>
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.
Pull request overview
This PR optimizes the get_attributes() method to accelerate fetching attributes from PKCS#11 object handles. The optimization reduces the number of API calls by pre-allocating buffers for attributes with known fixed sizes and using a two-pass approach for variable-length attributes.
Key changes:
- Implemented a new two-pass attribute fetching strategy that pre-allocates buffers for fixed-size attributes
- Added
fixed_size()method toAttributeTypeto identify attributes with known fixed sizes - Added
as_cptr!macro to safely handle empty vector pointer conversions - Enhanced test utilities to support library behavior simulation and handle already-initialized errors
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| cryptoki/src/session/object_management.rs | Rewrote get_attributes() to use optimized two-pass fetching with pre-allocated buffers |
| cryptoki/src/object.rs | Added as_cptr! macro for safe pointer conversion and fixed_size() method for attribute type sizing |
| cryptoki/tests/basic.rs | Added comprehensive test get_attributes_test() covering multiple attribute retrieval scenarios |
| cryptoki/tests/common/mod.rs | Added test utility functions for library behavior simulation and improved initialization error handling |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Eric Devolder <[email protected]>
Jakuje
left a comment
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.
looks good! Squashing the commit history and fixup commits eventually would be good, but ok for now for more reviews.
|
Sure, will do that once the review process is finished. BTW this can be conveniently performed at merge time, so maybe not needed from my end. |
wiktor-k
left a comment
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.
Looks good 👌 the logic is a bit heavy but I'm happy that this improves the performance 👏
cryptoki/src/object.rs
Outdated
| AttributeType::ValidationCountry => Some(size_of::<[CK_UTF8CHAR; 2]>()), | ||
|
|
||
| // Variable-length attributes (all the others) | ||
| _ => None, |
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.
🤔 What happens when someone adds a new AttributeType and forgets to add a branch here?
(That is I wonder if we should explicitly enumerate all None variants here or would that be a small concern...)
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.
I think it will just go through the slow path as before -- first query the size from the token and then query the value. I agree though that having all of them enumerate would make it more nudging for the people adding new attributes to add them correctly here.
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.
Ack! Let's see what @keldonin thinks about it, I'm fine either way 👍
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 that this is future-proof but agree that listing them all could be good!
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.
I think we can make this, no big deal. Removing the catch-all requires the user to adjust this method.
$ python3 -m this | grep Explicit
Explicit is better than implicit.
hug-dev
left a comment
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.
Very good improvement, thank you!
| | Attribute::ValidationProfile(bytes) | ||
| | Attribute::VendorDefined((_, bytes)) | ||
| | Attribute::Id(bytes) => bytes.as_ptr() as *mut c_void, | ||
| | Attribute::Id(bytes) => as_cptr!(bytes), |
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.
Thank for also updating those 🙏
|
|
||
| #[test] | ||
| #[serial] | ||
| fn get_attributes_test() -> TestResult { |
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.
Great test, thanks!
wiktor-k
left a comment
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.
Thank you! 🙇
Signed-off-by: Eric Devolder <[email protected]>
0a3089b to
68dced6
Compare
|
hmmm, seems like I am facing some erratic errors during testing - and not always with the same test suite. I can't seem to reproduce these locally. These issues seem unrelated to the change itself. Something you guys have already experienced? |
|
Weird because I don't think |
|
I think this is the test running the example from the following documentation block: This sounds quite odd, as the C_Initialize returns |
|
This sounds like intermittent/timing issue. Rerun fixed this. But filled kryoptic issue to investigate it further: latchset/kryoptic#391 |
As suggested by @Jakuje, this is a cleaned up version of PR #322, with legacy code removed and benchmark example stripped off.
See PR #322 for discussions.