Deduplicate some size/align calculations (#4658)
This commit is an effort to reduce the amount of complexity around managing the size/alignment calculations of types in the canonical ABI. Previously the logic for the size/alignment of a type was spread out across a number of locations. While each individual calculation is not really the most complicated thing in the world having the duplication in so many places was constantly worrying me. I've opted in this commit to centralize all of this within the runtime at least, and now there's only one "duplicate" of this information in the fuzzing infrastructure which is to some degree less important to deduplicate. This commit introduces a new `CanonicalAbiInfo` type to house all abi size/align information for both memory32 and memory64. This new type is then used pervasively throughout fused adapter compilation, dynamic `Val` management, and typed functions. This type was also able to reduce the complexity of the macro-generated code meaning that even `wasmtime-component-macro` is performing less math than it was before. One other major feature of this commit is that this ABI information is now saved within a `ComponentTypes` structure. This avoids recursive querying of size/align information frequently and instead effectively caching it. This was a worry I had for the fused adapter compiler which frequently sought out size/align information and would recursively descend each type tree each time. The `fact-valid-module` fuzzer is now nearly 10x faster in terms of iterations/s which I suspect is due to this caching.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/// Represents the possible sizes in bytes of the discriminant of a variant type in the component model
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum DiscriminantSize {
|
||||
/// 8-bit discriminant
|
||||
Size1,
|
||||
@@ -11,7 +11,7 @@ pub enum DiscriminantSize {
|
||||
|
||||
impl DiscriminantSize {
|
||||
/// Calculate the size of discriminant needed to represent a variant with the specified number of cases.
|
||||
pub fn from_count(count: usize) -> Option<Self> {
|
||||
pub const fn from_count(count: usize) -> Option<Self> {
|
||||
if count <= 0xFF {
|
||||
Some(Self::Size1)
|
||||
} else if count <= 0xFFFF {
|
||||
@@ -22,16 +22,21 @@ impl DiscriminantSize {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the size, in bytes, of this discriminant
|
||||
pub const fn byte_size(&self) -> u32 {
|
||||
match self {
|
||||
DiscriminantSize::Size1 => 1,
|
||||
DiscriminantSize::Size2 => 2,
|
||||
DiscriminantSize::Size4 => 4,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DiscriminantSize> for u32 {
|
||||
/// Size of the discriminant as a `u32`
|
||||
fn from(size: DiscriminantSize) -> u32 {
|
||||
match size {
|
||||
DiscriminantSize::Size1 => 1,
|
||||
DiscriminantSize::Size2 => 2,
|
||||
DiscriminantSize::Size4 => 4,
|
||||
}
|
||||
size.byte_size()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +65,7 @@ pub enum FlagsSize {
|
||||
|
||||
impl FlagsSize {
|
||||
/// Calculate the size needed to represent a value with the specified number of flags.
|
||||
pub fn from_count(count: usize) -> FlagsSize {
|
||||
pub const fn from_count(count: usize) -> FlagsSize {
|
||||
if count == 0 {
|
||||
FlagsSize::Size0
|
||||
} else if count <= 8 {
|
||||
@@ -74,7 +79,7 @@ impl FlagsSize {
|
||||
}
|
||||
|
||||
/// Divide `n` by `d`, rounding up in the case of a non-zero remainder.
|
||||
fn ceiling_divide(n: usize, d: usize) -> usize {
|
||||
const fn ceiling_divide(n: usize, d: usize) -> usize {
|
||||
(n + d - 1) / d
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user