Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce size of Expr from 80 to 64 bytes #9900

Merged
merged 3 commits into from
Feb 9, 2024
Merged

Reduce size of Expr from 80 to 64 bytes #9900

merged 3 commits into from
Feb 9, 2024

Conversation

charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Feb 8, 2024

Summary

This PR reduces the size of Expr from 80 to 64 bytes, by reducing the sizes of...

  • ExprCall from 72 to 56 bytes, by using boxed slices for Arguments.
  • ExprCompare from 64 to 48 bytes, by using boxed slices for its various vectors.

In testing, the parser gets a bit faster, and the linter benchmarks improve quite a bit.

Copy link

codspeed-hq bot commented Feb 8, 2024

CodSpeed Performance Report

Merging #9900 will improve performances by 4.49%

Comparing charlie/box (708ef73) with main (bd8123c)

Summary

⚡ 1 improvements
✅ 29 untouched benchmarks

Benchmarks breakdown

Benchmark main charlie/box Change
linter/all-with-preview-rules[large/dataset.py] 100.2 ms 95.9 ms +4.49%

@charliermarsh
Copy link
Member Author

That's a bit better...

@charliermarsh
Copy link
Member Author

I think I can do even better though.

@charliermarsh charliermarsh force-pushed the charlie/box branch 2 times, most recently from b9f9008 to 1a64522 Compare February 8, 2024 20:53
@charliermarsh
Copy link
Member Author

Okay, there's no longer any parser regression, and there are parser and linter improvements across the board (though many don't meet our threshold).

pub args: Vec<Expr>,
pub keywords: Vec<Keyword>,
pub args: Box<[Expr]>,
pub keywords: Box<[Keyword]>,
Copy link
Member Author

Choose a reason for hiding this comment

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

We could make these Option<Box<...>> to avoid allocating when empty...

Copy link
Member

Choose a reason for hiding this comment

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

It should be possible to use Option without increasing the struct size, and avoiding the allocation could be nice. although It doesn't seem that the empty slice allocates

https://godbolt.org/z/E9cxqE9P3 CC: @BurntSushi

Copy link
Member Author

Choose a reason for hiding this comment

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

Gasp...

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that is interesting. I actually couldn't find how this was being done in std specifically. So it could just be an optimization in the code generator somewhere. Or perhaps I missed a call to dangling.

pub ops: Vec<CmpOp>,
pub comparators: Vec<Expr>,
pub ops: Box<[CmpOp]>,
pub comparators: Box<[Expr]>,
Copy link
Member Author

Choose a reason for hiding this comment

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

These are never empty so zero-length slice is not a problem.

// static_assertions::assert_eq_size!(ExprTuple, [u8; 40]);
// static_assertions::assert_eq_size!(ExprUnaryOp, [u8; 24]);
// static_assertions::assert_eq_size!(ExprYield, [u8; 16]);
// static_assertions::assert_eq_size!(ExprYieldFrom, [u8; 16]);
Copy link
Member Author

Choose a reason for hiding this comment

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

@MichaReiser - I missed your comment when we talked in-person -- did you want me to include these as assert!?

Copy link
Member

Choose a reason for hiding this comment

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

Ah sorry. I don't mind keeping them. I do find them useful when analyzing the type's size in the future.

Copy link
Member

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

Master of the double-star.

@charliermarsh charliermarsh force-pushed the charlie/box branch 3 times, most recently from 0863c48 to 3d93f59 Compare February 9, 2024 02:44
@charliermarsh charliermarsh enabled auto-merge (squash) February 9, 2024 02:44
@charliermarsh charliermarsh merged commit 49fe1b8 into main Feb 9, 2024
17 checks passed
@charliermarsh charliermarsh deleted the charlie/box branch February 9, 2024 02:53
pub args: Vec<Expr>,
pub keywords: Vec<Keyword>,
pub args: Box<[Expr]>,
pub keywords: Box<[Keyword]>,
Copy link
Member

Choose a reason for hiding this comment

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

It should be possible to use Option without increasing the struct size, and avoiding the allocation could be nice. although It doesn't seem that the empty slice allocates

https://godbolt.org/z/E9cxqE9P3 CC: @BurntSushi

Copy link
Contributor

github-actions bot commented Feb 9, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

nkxxll pushed a commit to nkxxll/ruff that referenced this pull request Mar 10, 2024
## Summary

This PR reduces the size of `Expr` from 80 to 64 bytes, by reducing the
sizes of...

- `ExprCall` from 72 to 56 bytes, by using boxed slices for `Arguments`.
- `ExprCompare` from 64 to 48 bytes, by using boxed slices for its
various vectors.

In testing, the parser gets a bit faster, and the linter benchmarks
improve quite a bit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Potential performance improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants