Categories
Tags
3d ABI Actions algorithms alignment analyze APIT Arc Architecture arm array ascii ash assembly asynchronous attribute base64 bithacks Blogging box c c23 cdylib CHAR_BIT clamp clang clang-format client clippy cmake compiler Computer concat concurrency const_fn constexpr contravariant cos covariant cpp cpu crate CS Customization cybersecurity DataStructure db debugging Demo deserialization discrete dll doc DP drawio dtruss dylib Dynamic emulator example Example FFI ffmpeg flamegraph flat_map fold format FP fsanitize Functional FunctionalProgramming functions futures Fuwari game GATs gcc gccrs generics gitignore glibc GUI hacking hashmap haskell heap hyperfine Imperative inference intel interop invariant iterator join justfile kernel LaTeX leak LFU limits linux lto MachineLearning macOS map Markdown math ML mmap mod mp3 nc nextest OnceLock optimization OS ownership panic parallels perf physics pin postgresql procedure product profiling pub radare2 rayon release reverse RPIT rust sanitizer Science science serialization server shift SIMD sin size SmallProjects so socket std strace String StringView strip strlen struct sum super surrealdb SWAR swisstable synchronous syscall tan test thread time toml tracing traits trap triangulation type uint32_t UnsafeRust utf16 utf8 vector Video vulkan wsl x86_64 xilem zig
194 words
1 minutes
251212_box_basic
Box를 써야하는 이유
- Heap에 들어가서 성능저하가 발생하는 Box는 언제 써야할까?
- 무지 큰 데이터를 쓸때 Box가 더 이득이다.
- deep copy인지 shadow copy인지 모르겠지만
- stack에 데이터를 저장을 하고 thread 로 send를 한다면 heap에서 저장하는게 더 이득
fn main() {
// 타입이 정해져있다. 정적
let a = 10;
println!("a {a}");
println!("Hello, world!");
// Use heap allocation (Box) instead of stack for large arrays
let x_big_array: Box<[[i32; 1000]; 1000]> = Box::new([[0; 1000]; 1000]);
println!("x_big_array address : {:p}", &*x_big_array);
println!("thread {:?}", std::thread::current().id());
println!("pid: {:?}", std::os::unix::process::parent_id());
println!("~~~~");
let thread01 = std::thread::spawn(move || {
let copy_data: Box<[[i32; 1000]; 1000]> = x_big_array;
println!("pid: {:?}", std::os::unix::process::parent_id());
println!("thread {:?}", std::thread::current().id());
println!("copy data first element : {:p}", &*copy_data);
});
thread01.join().unwrap();
}- result
a 10
Hello, world!
x_big_array address : 0x7728e302f010
thread ThreadId(1)
pid: 103106
~~~~
pid: 103106
thread ThreadId(2)
copy data first element : 0x7728e302f010251212_box_basic
https://younghakim7.github.io/blog/posts/251212_box_basic/