Skip to content

refactor declaration.rs to change conventions#425

Open
jesse-shopify wants to merge 5 commits intomainfrom
jj-refactor-declarations
Open

refactor declaration.rs to change conventions#425
jesse-shopify wants to merge 5 commits intomainfrom
jj-refactor-declarations

Conversation

@jesse-shopify
Copy link
Contributor

@jesse-shopify jesse-shopify commented Jan 8, 2026

  • remove macros
  • remove individual classes as they currently do not have data or behavior that differs from their shared components - when/if they do they can be re-added
  • use enum Declaration to separate types
  • implement helper methods to get at useful structs on enum so match statements don't proliferate. can use:
    • as_simple
    • as_simple_mut
    • as_namespace
    • as_namespace_mut

// TODO: temporary hack to avoid crashing on `Struct.new`, `Class.new` and `Module.new`
}
_ => panic!("Tried to add member to a declaration that isn't a namespace"),
if let Some(namespace) = declaration.as_namespace_mut() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

cleaner, I think

}
decl.as_namespace()
.expect("Tried to get members for a declaration that isn't a namespace")
.members()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

cleaner

#[derive(Debug)]
pub struct NamespaceDeclaration {
/// The base simple declaration fields
simple: SimpleDeclaration,
Copy link
Contributor

Choose a reason for hiding this comment

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

I tried this design earlier in the project and having this extra field for all definitions resulted in a lot more memory usage.

Could you compare main against your branch with --stats on a few corpus sizes?

You can produce test corpuses with https://github.com/Shopify/saturn/blob/main/utils/generate-corpus#L11.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The data is below. There was no change in memory or runtime performance.

Copy link
Contributor

@Morriar Morriar Jan 15, 2026

Choose a reason for hiding this comment

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

In terms of API, I'm not a fan of having to go through simple depending on the field I want to access.

On the other hand, I do like the simplification around handling namespace related fields and avoiding the matches/panic. Could we get the same result with nested enums instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The thing about Rust is that there are many ways to get to the same place and the only different is ergonomics. Nested enums could work. Another option to improve ergonomics might be Using Deref / DerefMut traits. Another option would be to have a SimpleDeclaration that has an optional, boxed NamespaceDeclaration, though I suppose that would add an extra 8 bytes to SimpleDeclaration but then Declaration might not need to box which could be a performance and memory win.

Copy link
Contributor

Choose a reason for hiding this comment

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

I tried nested enums in #470, it's not horrible.

@jesse-shopify jesse-shopify self-assigned this Jan 12, 2026
@jesse-shopify jesse-shopify force-pushed the jj-refactor-declarations branch from decd579 to 4942fc6 Compare January 13, 2026 18:13
Copy link
Contributor Author

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

@jesse-shopify
Copy link
Contributor Author

Memory usage using huge corpus:

Main

----------------------------------------
Indexed 100000 files
Found 13998368 names
Found 14007331 definitions
Found 100000 URIs
----------------------------------------
Maximum Resident Set Size: 13260619776 bytes (12646.31 MB)
Peak Memory Footprint:     10639665448 bytes (10146.77 MB)
Execution Time:            33.55 seconds

This Branch

----------------------------------------
Indexed 100000 files
Found 13998368 names
Found 14007331 definitions
Found 100000 URIs
----------------------------------------
Maximum Resident Set Size: 12421169152 bytes (11845.75 MB)
Peak Memory Footprint:     10664568888 bytes (10170.52 MB)
Execution Time:            33.99 seconds

No significant change in memory usage.

test_utils = ["dep:tempfile"]

[dependencies]
parking_lot = "0.12"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added due to better lock API

@jesse-shopify
Copy link
Contributor Author

main stats


Query statistics
  Total declarations:         13998368
  With documentation:         1643532 (11.7%)
  Without documentation:      12354836 (88.3%)
  Total documentation size:   1897179 bytes
  Multi-definition names:     8401 (0.1%)

Definition breakdown:
  Method               10000000
  InstanceVariable     2000446
  Constant             1000000
  Class                599649
  Module               300627
  SingletonClass        99724
  ClassVariable          5410
  GlobalVariable          920

Timing breakdown
  Listing             0.284s (  0.8%)
  Indexing            8.987s ( 24.6%)
  Resolution         22.851s ( 62.5%)
  Querying            4.451s ( 12.2%)
  Cleanup             0.000s (  0.0%)
  Total:             36.574s

Maximum RSS: 14091059200 bytes (13438.28 MB)

Indexed 100000 files
Found 13998368 names
Found 14007331 definitions
Found 100000 URIs

branch stats

Query statistics
  Total declarations:         13998368
  With documentation:         1643532 (11.7%)
  Without documentation:      12354836 (88.3%)
  Total documentation size:   1897179 bytes
  Multi-definition names:     8401 (0.1%)

Definition breakdown:
  Method               10000000
  InstanceVariable     2000446
  Constant             1000000
  Class                599649
  Module               300627
  SingletonClass        99724
  ClassVariable          5410
  GlobalVariable          920

Timing breakdown
  Listing             0.282s (  0.7%)
  Indexing            9.905s ( 25.8%)
  Resolution         23.626s ( 61.6%)
  Querying            4.546s ( 11.9%)
  Cleanup             0.000s (  0.0%)
  Total:             38.359s

Maximum RSS: 13771784192 bytes (13133.80 MB)

Indexed 100000 files
Found 13998368 names
Found 14007331 definitions
Found 100000 URIs

@jesse-shopify jesse-shopify marked this pull request as ready for review January 14, 2026 19:29
@jesse-shopify jesse-shopify requested a review from a team as a code owner January 14, 2026 19:29
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.

2 participants