To clarify again, I never say to change the default. The idea is simply to print a clear message on screen as a way to communicate with the user, so that those who aren't aware of it will be informed and can start analyzing the situation. Why is printing a message repeatedly being interpreted as changing the default? It's not, it's just adding a visible note. Using Clippy doesn't reach all users
As for the general audience, I'd actually say that the majority of users, especially those who have recently driven Rust's growth in popularity are drawn to it primarily for its speed. The safety aspect is something they've already experienced with GC based languages. Rust needs to differentiate itself from high performance GC languages. If Rust's performance ends up on par with them, it will be seen as not worth the added complexity of using it. And compared to non GC languages that are more flexible but still provide comprehensive memory debugging tools, Rust will also struggle to stand out if its performance falls short
If you know the correct way to benchmark this, I'd genuinely encourage you to go ahead and try it. That would save the time, since I'm concerned that if I do it, I might get it wrong again and slow things down further
That shouldn't happen because, as I understand it, memset already guarantees that bytes are touched sequentially, making it impossible to skip over the guard page and overwrite other memory
The problem is that the stack grows downwards, not upwards, so if memset writes from low to high, it could start way past the guard page and then write closer and closer to it until it hits the guard page and aborts. Another thread may in the mean time be trying to use the memory that has now been corrupted by the memset that is currently being executed.
For example if the current memory layout is
0x1000-0x2000 buffer of thread 1
0x2000-0x3000 guard page of stack of thread 2
0x3000-0x4000 stack of thread 2
stack pointer = 0x3800
then allocating a 0x2000 bytes buffer on the stack and writing to it with a memset would write from 0x1800 to 0x3800, which would overwrite part of the buffer before the guard page is hit.
C standard memset does not guarantee to touch bytes sequentially - it only guarantees that after it's returned, all bytes in the destination supplied will, in future, be observed as-if they've been set to the given value.
It's thus allowed to write in random order - whatever is most suited to the machine at hand. Practically, you're most likely to see either sequential, counting up (from dest to dest + count - 1), or sequential, counting down (from dest + count - 1 to dest), but even that's not required. It is entirely possible that a memset would be implemented such that the writes are ordered in a way that minimizes cache traffic and memory controller work (e.g. by using platform knowledge to write complete rows first, then complete DDR bursts, then partial writes last) rather than a programmer-sensible order.
Yeah right, I get it now. I also just read that even LLVM's built in memset provides no guarantee on byte access order. So if an allocation exceeds 8 MB on Linux and memset fills it starting from a lower address, it is already overwriting adjacent memory in the first fill. So that's why Rust uses a manual loop
But I still see room for improvement. The idea would be to process that 4 KB chunks (or if the size is, say, 3 KB, become chunk into the largest multiple of 8 that fits), then offload each 4 KB chunk to SIMD. Any remainder that doesn't divide evenly by 8 can be handled with the normal loop. This would still guarantee that the guard page is touched, since execution never suddenly jumps beyond that 4 KB boundary (or whatever the chunk size is). The current approach uses a one by one loop, which is slow and scales as O(n) relative to the allocation size
Zig doesn't use libc, it has its own wrappers for OS syscalls. Yeah, I was just browsing around and it turns out LLVM's built in memset also has no ordering guarantee
You might want to read the Eager Drop notes, use that to find previous discussions on the topic, and then write a pre-RFC using the RFC template that you post here, if you're interested in working on earlier drops (including conditional eager dropping).
Note that memory savings from earlier drop are always going to be related to cases where you can drop a heap allocation - stack frames are usually fixed in size at compilation time, not variable size at run time.
I have finished reading through the list above. All of the concerns raised stem from doing a uniform application, whereas this is not uniform, it is a fine grained application, meaning that asap drop is only active for variables declared with the asap keyword
To make it easier to visualize, Arc only takes effect for variables wrapped inside Arc. Similarly, asap only takes effect for variables declared with the asap keyword. Types such as Mutex are not declared with asap, so they continue to use scope based drop as usual
It also already handles the case where a variable is borrowed by another variable, the drop is deferred until the last borrow ends
The &'static str is not the point, it is simply there to hold a debug message that gets printed by the Drop implementation, so I can verify whether the drop order matches what is expected. The key thing being observed is the order of the drop messages
This is not ready for a pre RFC yet :v Edit: not a bug, I forgot to restore the asap macro calls after temporarily switching them to scope based drop
use asap_macro::asap;
struct Tracked {
name: &'static str,
}
impl Tracked {
fn new(name: &'static str) -> Self {
println!("CREATE {name}");
Tracked { name }
}
fn use_it(&self) {
println!("USE {}", self.name);
}
}
impl Drop for Tracked {
fn drop(&mut self) {
println!("DROP {}", self.name);
}
}
impl Clone for Tracked {
fn clone(&self) -> Self {
Tracked::new("variable asap, 3rd, clone")
}
}
fn borrow(a: &Tracked) {
println!("BORROW {} by other function", a.name);
}
fn moved(a: Tracked) {
println!("MOVE {} to other function", a.name);
}
fn main() {
let normal = Tracked::new("variable scope based");
let early = asap!(Tracked::new("variable asap"));
normal.use_it();
early.use_it();
drop(normal);
let early = asap!(Tracked::new("variable asap, 2nd"));
let cloned = early.clone();
borrow(&cloned);
borrow(&early);
moved(early);
let early = asap!(Tracked::new("variable asap, 4th, branching"));
if 1 == 12 {
borrow(&early);
} else {
moved(early);
}
println!("after branching");
let early = asap!(Tracked::new("variable asap, 5th, borrowed by other variable"));
println!("before borrow");
let borrowed = &early;
println!("before used by borrow function");
borrow(borrowed);
println!("after borrow function");
}
CREATE variable scope based
CREATE variable asap
USE variable scope based
USE variable asap
DROP variable asap
DROP variable scope based
CREATE variable asap, 2nd
CREATE variable asap, 3rd, clone
BORROW variable asap, 3rd, clone by other function
DROP variable asap, 3rd, clone
BORROW variable asap, 2nd by other function
MOVE variable asap, 2nd to other function
DROP variable asap, 2nd
CREATE variable asap, 4th, branching
MOVE variable asap, 4th, branching to other function
DROP variable asap, 4th, branching
after branching
CREATE variable asap, 5th, borrowed by other variable
before borrow
before used by borrow function
BORROW variable asap, 5th, borrowed by other variable by other function
DROP variable asap, 5th, borrowed by other variable
after borrow function
This is extremely dangerous because raw pointers can be left pointing at the thing. Furthermore, implementation-wise, even normal references can be hard to know of. I don't think this will ever happen.
Raw pointers are inherently unsafe, there's no guard against dangling pointers, and that's nothing new. The demonstration above already shows normal references, please don't skip over that. Instead, provide a concrete code example that actually breaks it, not something vague like "can be hard to know" or anything similarly hand wavy. If there is a breakage, the next step is simply to find the fix. I'm expecting a response that is actionable and productive, for example "this breaks code pattern A", something clear, so that in moving forward has a clear path to do
some_ffi_function(CString::new(foo).as_ptr()) would break if we drop as early as possible as that would mean the CString would be dropped right after the .as_ptr() and before the some_ffi_function call.
Yeah that is good clear catch. Then don't use it in FFI as FFI code touchs raw pointer which has no lifetime info.
The answer isn't to blindly use it everywhere like uniformly, just like any other Rust feature, unless somehow Rust makes pointer has lifetime. The code above is already susceptible to dangling pointers because the CString can be dropped by the scope while the poonter is still being used. It is the inherent behavior of raw pointers, which carry no lifetime information. Just that the asap drop works using NLL style method, it is not compatible with raw pointer that bypass it. It doesn't violates because raw pointer is already under unsafe { } block
Raw pointers are also non deterministic, it can dangle, it can null deref, it can missalign, etc, which is exactly why you don't use them blindly everywhere, only where necessary. In safe code, using references. The same applies to this asap drop, it's meant to be used in safe Rust code, not in unsafe pointer FFI code because pointer bypass NLL system. The amount of safe Rust code far outweighs unsafe pointer FFI code. If needed, it can be extended with :
let a = asap!(data)
let ptr = a.as_ptr()
pointer operation
let _ = &a
But back to the point I made in the paragraph above
I have already demonstrated the proof of concept above showing that the reference approach works. Perhaps try providing code that actually breaks it, only then does it become actionable so can move to find a solution. Try to stop being vague like you have been this whole time. "Hard to implement", hard in exactly what way? What specifically is the problem?
They're hard. They're neither non-deterministic nor impossible to use. With this, they are.
It seems you don't listen. You didn't provide a patch to rustc to implement it. You provided a code example how it will work, which, even if not underspecified, says nothing about the difficulty to implement this.
To know why it's hard to implement you need to know rustc (or try to implement it), but in short: because lifetimes are erased in codegen.