-
Notifications
You must be signed in to change notification settings - Fork 2
Optimize Rule memory layout with packed permissions and inline pattern storage #32
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b25c183
Added: Internal Hash64 type, backed by ahash
Sewer56 11441c6
Added: Hash63 newtype wrapper for 63-bit hash values
Sewer56 c18d388
'Public API' -> 'Internal Public API'
Sewer56 f097e7d
Added: PackedPermission for bit-packed permission storage
Sewer56 e379743
Changed: Optimize Rule layout with packed permission and inline TinyS…
Sewer56 b738be4
Added: Link to PR for permissions
Sewer56 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| //! 63-bit hash type. | ||
| //! | ||
| //! A 63-bit hash value representing the upper 63 bits of an original hash. | ||
| //! More specifically, a 64-bit hash value which has been `>> 1`. | ||
| //! i.e. 64th bit is always 0. | ||
|
|
||
| /// A 63-bit hash value representing the upper 63 bits of an original hash. | ||
| /// Result of right shifting a hash `>> 1`. | ||
| /// | ||
| /// # Bit Layout | ||
| /// - Bits 0-62: Original hash bits 1-63 (upper 63 bits) | ||
| /// - Bit 63: Always 0 | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
| pub(crate) struct Hash63(u64); | ||
|
|
||
| impl Hash63 { | ||
| /// Creates a new Hash63 from a raw u64 value. | ||
| /// | ||
| /// The caller is responsible for ensuring bit 63 is 0. | ||
| #[inline] | ||
| #[allow(dead_code)] // internal public API | ||
| pub(crate) const fn from_u64(value: u64) -> Self { | ||
| Self(value) | ||
| } | ||
|
|
||
| /// Returns the underlying u64 value. | ||
| #[inline] | ||
| #[allow(dead_code)] // internal public API | ||
| pub(crate) const fn as_u64(&self) -> u64 { | ||
| self.0 | ||
| } | ||
|
|
||
| /// Creates a Hash63 from a Hash64 by extracting upper 63 bits. | ||
| #[inline] | ||
| pub(crate) fn from_hash64(hash: crate::internal::hash64::Hash64) -> Self { | ||
| Self(hash.as_u64() >> 1) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| //! Wrapper for an internal 64-bit hash. | ||
| //! | ||
| //! Currently uses ahash64 under the hood, based on performance requirements | ||
| //! (handling short strings, while also scaling well); but given this is an | ||
| //! internal type, that's an implementation detail. | ||
|
|
||
| /// A 64-bit hash value using the ahash algorithm. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
| pub(crate) struct Hash64(u64); | ||
|
|
||
| impl Hash64 { | ||
| /// Creates a new Hash64 from a raw u64 value. | ||
| #[inline] | ||
| #[allow(dead_code)] // internal public API | ||
| pub(crate) fn from_u64(value: u64) -> Self { | ||
| Self(value) | ||
| } | ||
|
|
||
| /// Returns the underlying u64 value. | ||
| #[inline] | ||
| #[allow(dead_code)] // internal public API | ||
| pub(crate) fn as_u64(&self) -> u64 { | ||
| self.0 | ||
| } | ||
| } | ||
|
|
||
| /// Hashes a string to Hash64 using ahash64. | ||
| #[inline(always)] | ||
| #[allow(dead_code)] // internal public API | ||
| pub(crate) fn hash_u64(s: &str) -> Hash64 { | ||
| hash_u64_bytes(s.as_bytes()) | ||
| } | ||
|
|
||
| /// Hashes raw bytes to Hash64 using ahash64. | ||
| #[inline(always)] | ||
| #[allow(dead_code)] // internal public API | ||
| pub(crate) fn hash_u64_bytes(bytes: &[u8]) -> Hash64 { | ||
| Hash64(ahash::RandomState::with_seed(0xDEAD_CAFE).hash_one(bytes)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn hash_is_deterministic() { | ||
| let hash1 = hash_u64("bash"); | ||
| let hash2 = hash_u64("bash"); | ||
| assert_eq!(hash1, hash2); | ||
| } | ||
|
|
||
| #[test] | ||
| fn different_inputs_produce_different_hashes() { | ||
| let h1 = hash_u64("bash"); | ||
| let h2 = hash_u64("read"); | ||
| let h3 = hash_u64("write"); | ||
| assert_ne!(h1, h2); | ||
| assert_ne!(h1, h3); | ||
| assert_ne!(h2, h3); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| //! Internal implementation details. | ||
| //! | ||
| //! This module contains private implementation details that are not part of | ||
| //! the public API. Items here may change without notice. | ||
|
|
||
| pub mod hash63; | ||
| pub mod hash64; | ||
| pub mod packed_permission; |
76 changes: 76 additions & 0 deletions
76
src/llm-coding-tools-core/src/internal/packed_permission.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| //! Bit-packed permission storage. | ||
| //! | ||
| //! Packs permission hash and action into a single u64. | ||
| //! - Lower 63 bits: hash63 (upper 63 bits of original hash) | ||
| //! - Upper 1 bit (bit 63): PermissionAction | ||
|
|
||
| use crate::internal::hash63::Hash63; | ||
| use crate::internal::hash64::Hash64; | ||
| use crate::permissions::PermissionAction; | ||
|
|
||
| /// Action bit mask - highest bit (bit 63). | ||
| const ACTION_MASK: u64 = 1u64 << 63; | ||
|
|
||
| /// Hash mask - lower 63 bits. | ||
| const HASH_MASK: u64 = !ACTION_MASK; | ||
|
|
||
| // Compile-time assertion: PermissionAction must be 1 byte for bit-packing | ||
| const _: () = assert!( | ||
| std::mem::size_of::<PermissionAction>() == 1, | ||
| "PermissionAction must be 1 byte for bit-packing" | ||
| ); | ||
|
|
||
| /// A u64 containing both permission hash and action. | ||
| /// | ||
| /// Layout: | ||
| /// - Bits 0-62: hash63 (upper 63 bits of original hash) | ||
| /// - Bit 63: PermissionAction | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
| pub(crate) struct PackedPermission(u64); | ||
|
|
||
| impl PackedPermission { | ||
| /// Creates a packed permission from hash and action. | ||
| #[inline] | ||
| pub(crate) fn new(hash: Hash64, action: PermissionAction) -> Self { | ||
| let hash63 = Hash63::from_hash64(hash); | ||
| let action_bit = (action as u64) << 63; | ||
| Self(hash63.as_u64() | action_bit) | ||
| } | ||
|
|
||
| /// Returns the hash portion (lower 63 bits) as a [`Hash63`]. | ||
| /// Use `Hash63::from_hash64()` to compare with an original Hash64. | ||
| #[inline] | ||
| pub(crate) fn hash(&self) -> Hash63 { | ||
| Hash63::from_u64(self.0 & HASH_MASK) | ||
| } | ||
|
|
||
| /// Returns the PermissionAction stored in bit 63. | ||
| #[inline] | ||
| pub(crate) fn action(&self) -> PermissionAction { | ||
| unsafe { std::mem::transmute(((self.0 >> 63) & 1) as u8) } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::internal::hash63::Hash63; | ||
| use crate::internal::hash64::Hash64; | ||
|
|
||
| #[test] | ||
| fn pack_unpack_roundtrip() { | ||
| // Use distinctive pattern: 0x1122334455667788 (easily detect if bits are lost) | ||
| let hash = Hash64::from_u64(0x1122334455667788u64); | ||
| let hash_shifted = Hash63::from_hash64(hash); | ||
|
|
||
| // Test roundtrip for Allow | ||
| let packed_allow = PackedPermission::new(hash, PermissionAction::Allow); | ||
| assert_eq!(packed_allow.hash(), hash_shifted); | ||
| assert_eq!(packed_allow.action(), PermissionAction::Allow); | ||
|
|
||
| // Test roundtrip for Deny | ||
| let packed_deny = PackedPermission::new(hash, PermissionAction::Deny); | ||
| assert_eq!(packed_deny.hash(), hash_shifted); | ||
| assert_eq!(packed_deny.action(), PermissionAction::Deny); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.