Skip to content

Commit b62bd29

Browse files
committed
[SOL] Allocate less space on stack for drift sort (anza-xyz#127)
1 parent 0d2f038 commit b62bd29

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

compiler/rustc_target/src/target_features.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,11 @@ static WASM_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
649649
const BPF_FEATURES: &[(&str, Stability, ImpliedFeatures)] =
650650
&[("alu32", Unstable(sym::bpf_target_feature), &[])];
651651

652-
const SBF_FEATURES: &[(&str, Stability, ImpliedFeatures)] =
653-
&[("alu32", Unstable(sym::sbf_target_feature), &[]), ("static-syscalls", Stable, &[])];
652+
const SBF_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
653+
("alu32", Unstable(sym::sbf_target_feature), &[]),
654+
("static-syscalls", Stable, &[]),
655+
("dynamic-frames", Stable, &[]),
656+
];
654657

655658
const CSKY_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
656659
// tidy-alphabetical-start

library/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ check-cfg = [
4444
'cfg(target_has_reliable_f128_math)',
4545
'cfg(target_family, values("solana"))',
4646
'cfg(target_arch, values("sbf"))',
47+
'cfg(target_feature, values("dynamic-frames"))',
4748
]

library/core/src/slice/sort/stable/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,18 @@ fn driftsort_main<T, F: FnMut(&T, &T) -> bool, BufT: BufGuard<T>>(v: &mut [T], i
118118

119119
// For small inputs 4KiB of stack storage suffices, which allows us to avoid
120120
// calling the (de-)allocator. Benchmarks showed this was quite beneficial.
121-
let mut stack_buf = AlignedStorage::<T, 4096>::new();
121+
let mut stack_buf;
122+
#[cfg(any(target_feature = "dynamic-frames", not(target_family = "solana")))]
123+
{
124+
stack_buf = AlignedStorage::<T, 4096>::new();
125+
}
126+
127+
#[cfg(all(target_family = "solana", not(target_feature = "dynamic-frames")))]
128+
{
129+
// Allocating 4096 bytes on SBPFv0 overflows the stack
130+
stack_buf = AlignedStorage::<T, 2048>::new();
131+
}
132+
122133
let stack_scratch = stack_buf.as_uninit_slice_mut();
123134
let mut heap_buf;
124135
let scratch = if stack_scratch.len() >= alloc_len {

0 commit comments

Comments
 (0)