Skip to content

Conversation

@eytan-starkware
Copy link
Contributor

@eytan-starkware eytan-starkware commented Dec 1, 2025

TL;DR

First step in adding support for measuring heap memory usage of compiler data structures.

What changed?

  • Implemented a HeapSize trait in cairo-lang-utils
  • Created a derive macro for HeapSize in cairo-lang-proc-macros
  • Added derive implementations for key data structures
  • Added attribute macros for automatically adding heap_size to salsa::interned and salsa::tracked
  • DIDNT support all the salsa structs yet

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@eytan-starkware eytan-starkware marked this pull request as ready for review December 1, 2025 05:36
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feature_created_a_heapsize_trait_and_a_macro_to_derive_it branch from e20efee to fc3340e Compare December 1, 2025 08:03
Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed 15 of 21 files at r1, all commit messages.
Reviewable status: 15 of 21 files reviewed, 3 unresolved discussions (waiting on @eytan-starkware and @TomerStarkware)


crates/cairo-lang-utils/Cargo.toml line 36 at r1 (raw file):

[features]
default = ["std", "tracing"]
env_logger = ["tracing"]

?

Code quote:

env_logger = ["tracing"]

crates/cairo-lang-debug/src/heap_size.rs line 1 at r1 (raw file):

pub trait HeapSize {

doc trait as well.


crates/cairo-lang-debug/src/heap_size.rs line 14 at r1 (raw file):

}

pub fn heap_size<T: HeapSize>(t: &T) -> usize {

why? and doc.

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feature_created_a_heapsize_trait_and_a_macro_to_derive_it branch 3 times, most recently from c9d4de7 to 9187d52 Compare December 2, 2025 12:48
Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed 3 of 45 files at r2, all commit messages.
Reviewable status: 4 of 46 files reviewed, 4 unresolved discussions (waiting on @eytan-starkware and @TomerStarkware)


.github/workflows/ci.yml line 204 at r2 (raw file):

      - uses: actions/checkout@v6
        with:
          fetch-depth: "0"

revert.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed 37 of 45 files at r2.
Reviewable status: 41 of 46 files reviewed, 7 unresolved discussions (waiting on @eytan-starkware and @TomerStarkware)


crates/cairo-lang-utils/src/heap_size.rs line 397 at r2 (raw file):

}

pub fn heap_size<T: HeapSize>(t: &T) -> usize {

check if deletable.


crates/cairo-lang-proc-macros/tests/heap_size.rs line 44 at r2 (raw file):

    let test_enum3 = TestEnum::Variant3;
    let size3 = test_enum3.heap_size();
    assert_eq!(size3, 0);

Suggestion:

    let test_struct =
        TestStruct { a: vec![1, 2, 3], b: "hello".to_string(), c: Arc::new("world".to_string()) };

    assert_eq!(test_struct.a.heap_size(), 12);
    assert_eq!(test_struct.b.heap_size(), 5);
    assert_eq!(test_struct.c.heap_size(), 0);
    assert_eq!(test_struct.heap_size(), test_struct.a.heap_size() + test_struct.b.heap_size() + test_struct.c.heap_size());

    ...
    let test_enum1 = TestEnum::Variant1 { a: vec![1, 2, 3], b: String::from("test") };
    let size1 = test_enum1.heap_size();
    assert!(size1 > 0);

    let test_enum2 = TestEnum::Variant2("hello".to_string(), Arc::new(vec![1, 2, 3]));
    let size2 = test_enum2.heap_size();
    assert!(size2 > 0);

    let test_enum3 = TestEnum::Variant3;
    let size3 = test_enum3.heap_size();
    assert_eq!(size3, 0);

crates/cairo-lang-lowering/Cargo.toml line 33 at r2 (raw file):

[dev-dependencies]

revert

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feature_created_a_heapsize_trait_and_a_macro_to_derive_it branch from 9187d52 to 55163f0 Compare December 2, 2025 15:07
Copy link
Contributor Author

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 41 of 46 files reviewed, 7 unresolved discussions (waiting on @orizi and @TomerStarkware)


.github/workflows/ci.yml line 204 at r2 (raw file):

Previously, orizi wrote…

revert.

Done.


crates/cairo-lang-lowering/Cargo.toml line 33 at r2 (raw file):

Previously, orizi wrote…

revert

Done.


crates/cairo-lang-utils/Cargo.toml line 36 at r1 (raw file):

Previously, orizi wrote…

?

Unrelated (it is from the move to tracing to be backward compatible)


crates/cairo-lang-utils/src/heap_size.rs line 397 at r2 (raw file):

Previously, orizi wrote…

check if deletable.

Done.


crates/cairo-lang-debug/src/heap_size.rs line 1 at r1 (raw file):

Previously, orizi wrote…

doc trait as well.

Done.


crates/cairo-lang-debug/src/heap_size.rs line 14 at r1 (raw file):

Previously, orizi wrote…

why? and doc.

Done.


crates/cairo-lang-proc-macros/tests/heap_size.rs line 44 at r2 (raw file):

    let test_enum3 = TestEnum::Variant3;
    let size3 = test_enum3.heap_size();
    assert_eq!(size3, 0);

Done.

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feature_created_a_heapsize_trait_and_a_macro_to_derive_it branch from 55163f0 to 4b8ee1d Compare December 2, 2025 19:13
Copy link
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomerStarkware reviewed 1 of 45 files at r2, 12 of 12 files at r3, all commit messages.
Reviewable status: all files reviewed, 8 unresolved discussions (waiting on @eytan-starkware and @orizi)


crates/cairo-lang-utils/src/heap_size.rs line 168 at r3 (raw file):

impl HeapSize for smol_str::SmolStr {
    fn heap_size(&self) -> usize {
        0

SmolStr can be allocated on the heap,

Copy link
Contributor Author

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 8 unresolved discussions (waiting on @orizi and @TomerStarkware)


crates/cairo-lang-utils/src/heap_size.rs line 168 at r3 (raw file):

Previously, TomerStarkware wrote…

SmolStr can be allocated on the heap,

SmolStr uses an Arc, and can allow memory deduplication, which will cause us to double count so currently it is not counted.

Copy link
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 7 unresolved discussions (waiting on @orizi)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants