Skip to content

Reduce size of Expr from 80 to 64 bytes#9900

Merged
charliermarsh merged 3 commits into
mainfrom
charlie/box
Feb 9, 2024
Merged

Reduce size of Expr from 80 to 64 bytes#9900
charliermarsh merged 3 commits into
mainfrom
charlie/box

Conversation

@charliermarsh
Copy link
Copy Markdown
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.

@codspeed-hq
Copy link
Copy Markdown

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
Copy Markdown
Member Author

That's a bit better...

@charliermarsh
Copy link
Copy Markdown
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
Copy Markdown
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).

@charliermarsh charliermarsh marked this pull request as ready for review February 8, 2024 21:08
@charliermarsh charliermarsh added the performance Potential performance improvement label Feb 8, 2024
pub args: Vec<Expr>,
pub keywords: Vec<Keyword>,
pub args: Box<[Expr]>,
pub keywords: Box<[Keyword]>,
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread crates/ruff_python_ast/src/nodes.rs Outdated
// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread crates/ruff_python_parser/src/function.rs
@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
@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
Copy Markdown
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

@github-actions
Copy link
Copy Markdown
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