diff --git a/Cargo.lock b/Cargo.lock index 2d05054..85b51d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,6 +44,20 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "box-str-dhat" +version = "0.1.0" +dependencies = [ + "dhat", +] + +[[package]] +name = "box-str-program" +version = "0.1.0" +dependencies = [ + "dhat", +] + [[package]] name = "cc" version = "1.0.73" @@ -77,13 +91,6 @@ version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" -[[package]] -name = "heap-program" -version = "0.1.0" -dependencies = [ - "dhat", -] - [[package]] name = "instant" version = "0.1.12" @@ -258,10 +265,25 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" +[[package]] +name = "stack-and-heap-program" +version = "0.1.0" + [[package]] name = "stack-program" version = "0.1.0" +[[package]] +name = "string-dhat" +version = "0.1.0" +dependencies = [ + "dhat", +] + +[[package]] +name = "string-program" +version = "0.1.0" + [[package]] name = "syn" version = "1.0.96" diff --git a/Cargo.toml b/Cargo.toml index 60fa435..33da0a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["stack-program", "heap-program"] +members = ["stack-program", "stack-and-heap-program", "string-program", "box-str-program", "string-dhat", "box-str-dhat"] diff --git a/heap-program/Cargo.toml b/box-str-dhat/Cargo.toml similarity index 86% rename from heap-program/Cargo.toml rename to box-str-dhat/Cargo.toml index 53d8fe1..e4a2d4b 100644 --- a/heap-program/Cargo.toml +++ b/box-str-dhat/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "heap-program" +name = "box-str-dhat" version = "0.1.0" edition = "2021" diff --git a/box-str-dhat/src/main.rs b/box-str-dhat/src/main.rs new file mode 100644 index 0000000..c997a12 --- /dev/null +++ b/box-str-dhat/src/main.rs @@ -0,0 +1,10 @@ +#[global_allocator] +static ALLOC: dhat::Alloc = dhat::Alloc; + +fn main() { + let _profiler = dhat::Profiler::new_heap(); + + let boxed_str: Box = "hello".into(); + + println!("boxed_str: {}", boxed_str); +} diff --git a/box-str-program/Cargo.toml b/box-str-program/Cargo.toml new file mode 100644 index 0000000..37a7b70 --- /dev/null +++ b/box-str-program/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "box-str-program" +version = "0.1.0" +edition = "2021" + +[dependencies] +dhat = "0.3.0" + +[features] +dhat-heap = [] # if you are doing heap profiling diff --git a/box-str-program/src/main.rs b/box-str-program/src/main.rs new file mode 100644 index 0000000..13f9177 --- /dev/null +++ b/box-str-program/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + let boxed_str: Box = "hello".into(); + + println!("boxed_str: {}", boxed_str); + println!("size of boxed_str on stack: {}", std::mem::size_of_val(&boxed_str)); +} diff --git a/dhat-heap.json b/dhat-heap.json new file mode 100644 index 0000000..2019fc4 --- /dev/null +++ b/dhat-heap.json @@ -0,0 +1,93 @@ +{ +"dhatFileVersion": 2, +"mode": "rust-heap", +"verb": "Allocated", +"bklt": true, +"bkacc": false, +"tu": "µs", +"Mtu": "s", +"tuth": 10, +"cmd": "target/debug/box-str-dhat", +"pid": 76713, +"tg": 177, +"te": 209, +"pps": [ +{ +"tb": 5, +"tbk": 1, +"tl": 63, +"mb": 5, +"mbk": 1, +"gb": 5, +"gbk": 1, +"eb": 0, +"ebk": 0, +"fs": [ +1, +2, +3, +4, +5, +6 +] +}, +{ +"tb": 1024, +"tbk": 1, +"tl": 32, +"mb": 1024, +"mbk": 1, +"gb": 1024, +"gbk": 1, +"eb": 1024, +"ebk": 1, +"fs": [ +7, +8, +9, +10, +11, +12, +13, +14, +15, +16, +17, +18, +19, +20, +21, +22, +23, +24 +] +} +], +"ftbl": [ +"[root]", +"0x1047ca668: ::allocate (alloc/src/alloc.rs:231:9)", +"0x1047c97bc: alloc::raw_vec::RawVec::allocate_in (alloc/src/raw_vec.rs:185:45)", +"0x1047c9d5c: alloc::raw_vec::RawVec::with_capacity_in (alloc/src/raw_vec.rs:132:9)", +"0x104792bf8: alloc::raw_vec::RawVec::with_capacity (alloc/src/raw_vec.rs:93:9)", +"0x104793128: as core::convert::From<&[T]>>::from (alloc/src/boxed.rs:1422:19)", +"0x1047928e4: as core::convert::From<&str>>::from (alloc/src/boxed.rs:1464:44)", +"0x1048b5920: ::allocate (alloc/src/alloc.rs:231:9)", +"0x1048b5920: alloc::raw_vec::RawVec::allocate_in (alloc/src/raw_vec.rs:185:45)", +"0x1048b5920: alloc::raw_vec::RawVec::with_capacity_in (alloc/src/raw_vec.rs:132:9)", +"0x1048b5920: alloc::vec::Vec::with_capacity_in (src/vec/mod.rs:613:20)", +"0x1048b5920: alloc::vec::Vec::with_capacity (src/vec/mod.rs:470:9)", +"0x1048b5920: std::io::buffered::bufwriter::BufWriter::with_capacity (io/buffered/bufwriter.rs:115:33)", +"0x1048b5920: std::io::buffered::linewriter::LineWriter::with_capacity (io/buffered/linewriter.rs:109:29)", +"0x1048b5920: std::io::buffered::linewriter::LineWriter::new (io/buffered/linewriter.rs:89:9)", +"0x1048b5920: std::io::stdio::stdout::{{closure}} (src/io/stdio.rs:609:58)", +"0x1048b5920: std::lazy::SyncOnceCell::get_or_init_pin::{{closure}} (std/src/lazy.rs:375:25)", +"0x1048b5920: std::sync::once::Once::call_once_force::{{closure}} (src/sync/once.rs:334:40)", +"0x1048d28fc: std::sync::once::Once::call_inner (src/sync/once.rs:434:21)", +"0x1048b3378: std::sync::once::Once::call_once_force (src/sync/once.rs:334:9)", +"0x1048b3378: std::lazy::SyncOnceCell::get_or_init_pin (std/src/lazy.rs:374:9)", +"0x1048b3378: std::io::stdio::stdout (src/io/stdio.rs:608:16)", +"0x1048b3378: std::io::stdio::print_to (src/io/stdio.rs:1016:21)", +"0x1048b3378: std::io::stdio::_print (src/io/stdio.rs:1029:5)", +"0x104792d58: box_str_dhat::main (box-str-dhat/src/main.rs:9:5)" +] +} \ No newline at end of file diff --git a/heap-program/src/main.rs b/heap-program/src/main.rs deleted file mode 100644 index ee8eae7..0000000 --- a/heap-program/src/main.rs +++ /dev/null @@ -1,35 +0,0 @@ -use std::io::{self, Read}; - -fn stdin_boxed_str() -> Box { - let mut buff = [0; 10]; - let mut stdin = io::stdin(); // We get `Stdin` here. - stdin.read(&mut buff).unwrap(); - let s = std::str::from_utf8(&buff).unwrap(); - - s.into() -} - -fn stdin_string() -> String { - let mut buff = String::new(); - std::io::stdin().read_line(&mut buff).unwrap(); - - buff -} - -fn my_simple_program(a: i32) -> i32 { - let b = 10; - a + b -} - - -fn main() { - println!("{}", my_simple_program(5)); - - let boxed_str = stdin_boxed_str(); - - println!("boxed_str: {}", boxed_str); - - let stdin_str = stdin_string(); - - println!("stdin_str: {}", stdin_str); -} diff --git a/stack-and-heap-program/Cargo.lock b/stack-and-heap-program/Cargo.lock new file mode 100644 index 0000000..091761d --- /dev/null +++ b/stack-and-heap-program/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "stack-and-heap-program" +version = "0.1.0" diff --git a/stack-and-heap-program/Cargo.toml b/stack-and-heap-program/Cargo.toml new file mode 100644 index 0000000..2a1b53b --- /dev/null +++ b/stack-and-heap-program/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "stack-and-heap-program" +version = "0.1.0" +edition = "2021" diff --git a/stack-and-heap-program/src/main.rs b/stack-and-heap-program/src/main.rs new file mode 100644 index 0000000..90755ac --- /dev/null +++ b/stack-and-heap-program/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + let a = Box::new(5_u8); + let b = 10_u8; + println!("{}, {}", a, b); +} diff --git a/string-dhat/Cargo.lock b/string-dhat/Cargo.lock new file mode 100644 index 0000000..01724dd --- /dev/null +++ b/string-dhat/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "string-dhat" +version = "0.1.0" diff --git a/string-dhat/Cargo.toml b/string-dhat/Cargo.toml new file mode 100644 index 0000000..b0104b2 --- /dev/null +++ b/string-dhat/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "string-dhat" +version = "0.1.0" +edition = "2021" + +[dependencies] +dhat = "0.3.0" diff --git a/string-dhat/src/main.rs b/string-dhat/src/main.rs new file mode 100644 index 0000000..8488611 --- /dev/null +++ b/string-dhat/src/main.rs @@ -0,0 +1,9 @@ +#[global_allocator] +static ALLOC: dhat::Alloc = dhat::Alloc; + +fn main() { + let _profiler = dhat::Profiler::new_heap(); + + let s = String::from("hello"); + println!("{}", s); +} diff --git a/string-program/Cargo.lock b/string-program/Cargo.lock new file mode 100644 index 0000000..272f9af --- /dev/null +++ b/string-program/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "string-program" +version = "0.1.0" diff --git a/string-program/Cargo.toml b/string-program/Cargo.toml new file mode 100644 index 0000000..50c3ff6 --- /dev/null +++ b/string-program/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "string-program" +version = "0.1.0" +edition = "2021" diff --git a/string-program/src/main.rs b/string-program/src/main.rs new file mode 100644 index 0000000..f08a91d --- /dev/null +++ b/string-program/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + let s = String::from("hello!"); + println!("{}", s); + println!("size of string on stack: {}", std::mem::size_of_val(&s)); +}