-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description This eliminates the unused weakref count in `std::sync::Arc`, saving 64 bits per unique `SharedReference`. I noticed there's additional potential optimizations we could do here too (not included in this PR), of increasing levels of difficulty: - We can deduplicate `ValueTypeId` by moving it inside the `Arc`. - We can build a mapping of `std::any::TypeId` to `ValueTypeId`, and avoid storing the `ValueTypeId` entirely. - We can deduplicate the fat pointer's layout metadata by also storing it inside the `Arc` using the nightly `ptr_metadata` feature, similar to `triomphe::ThinArc` (but that only works for slices of non-dst elements, presumably because they don't want to depend on nightly). ### Testing Instructions Using dhat for measuring the max heap size (vercel/next.js/67166)... Do a release build ``` PACK_NEXT_COMPRESS=objcopy-zstd pnpm pack-next --release --features __internal_dhat-heap ``` Start the dev server in shadcn-ui, and try to load the homepage: ``` pnpm i pnpm --filter=www dev --turbo curl http://localhost:3003/ ``` After curl exits, kill the dev server. ``` pkill -INT next-server ``` **Before (2 Runs):** ``` [dhat]: Teardown profiler dhat: Total: 3,106,545,900 bytes in 20,113,580 blocks dhat: At t-gmax: 731,860,693 bytes in 4,924,028 blocks dhat: At t-end: 731,860,693 bytes in 4,924,028 blocks ``` ``` [dhat]: Teardown profiler dhat: Total: 3,108,036,858 bytes in 20,111,694 blocks dhat: At t-gmax: 730,059,664 bytes in 4,923,002 blocks dhat: At t-end: 730,059,536 bytes in 4,923,001 blocks ``` **After (2 Runs):** ``` [dhat]: Teardown profiler dhat: Total: 3,093,298,170 bytes in 20,127,818 blocks dhat: At t-gmax: 727,258,939 bytes in 4,923,863 blocks dhat: At t-end: 727,155,146 bytes in 4,923,901 blocks ``` ``` [dhat]: Teardown profiler dhat: Total: 3,102,661,690 bytes in 20,124,408 blocks dhat: At t-gmax: 728,190,876 bytes in 4,924,856 blocks dhat: At t-end: 728,124,976 bytes in 4,924,236 blocks ``` This is [a 0.4426% reduction](https://www.wolframalpha.com/input?i=Percent+change+from+%28731%2C860%2C693%2B730%2C059%2C664%29%2F2+to+%28727%2C258%2C939%2B728%2C190%2C876%29%2F2).
- Loading branch information
Showing
14 changed files
with
108 additions
and
51 deletions.
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 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 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 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 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 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 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 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 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 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,33 @@ | ||
use std::any::Any; | ||
|
||
use unsize::Coercion; | ||
|
||
/// Attempt to downcast a `triomphe::Arc<dyn Any + Send + Sync>` to a concrete | ||
/// type. | ||
/// | ||
/// Ported from [`std::sync::Arc::downcast`] to [`triomphe::Arc`]. | ||
pub fn downcast_triomphe_arc<T: Any + Send + Sync>( | ||
this: triomphe::Arc<dyn Any + Send + Sync>, | ||
) -> Result<triomphe::Arc<T>, triomphe::Arc<dyn Any + Send + Sync>> { | ||
if (*this).is::<T>() { | ||
unsafe { | ||
// Get the pointer to the offset (*const T) inside of the ArcInner. | ||
let ptr = triomphe::Arc::into_raw(this); | ||
// SAFETY: The negative offset from the data (ptr) in an Arc to the start of the | ||
// data structure is fixed regardless of type `T`. | ||
// | ||
// SAFETY: Casting from a fat pointer to a thin pointer is safe, as long as the | ||
// types are compatible (they are). | ||
Ok(triomphe::Arc::from_raw(ptr.cast())) | ||
} | ||
} else { | ||
Err(this) | ||
} | ||
} | ||
|
||
/// [`Coerce::to_any`] except that it coerces to `dyn Any + Send + Sync` as | ||
/// opposed to `dyn Any`. | ||
pub fn coerce_to_any_send_sync<T: Any + Send + Sync>() -> Coercion<T, dyn Any + Send + Sync> { | ||
// SAFETY: The signature of this function guarantees the coercion is valid | ||
unsafe { Coercion::new(|x| x) } | ||
} |
This file contains 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 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 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.