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

refactor: Use QualifiedName for Imported::call_path #10214

Merged
merged 4 commits into from
Mar 6, 2024

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Mar 3, 2024

Summary

When you try to remove an internal representation leaking into another type and end up rewriting a simple version of smallvec.

The goal of this PR is to replace the Box<[&'a str]> with Box<QualifiedName> to avoid that the internal QualifiedName representation leaks (and it gives us a nicer API too). However, doing this when QualifiedName uses SmallVec internally gives us all sort of funny lifetime errors. I was lost but @BurntSushi came to rescue me. He figured out that smallvec has a variance problem which is already tracked in servo/rust-smallvec#146

To fix the variants problem, I could use the smallvec-2-alpha-4 or implement our own smallvec. I went with implementing our own small vec for this specific problem. It obviously isn't as sophisticated as smallvec (only uses safe code), e.g. it doesn't perform any size optimizations, but it does its job.

Other changes:

  • Removed Imported::qualified_name (the version that returns a String). This can be replaced by calling ToString on the qualified name.
  • Renamed Imported::call_path to qualified_name and changed its return type to &QualifiedName.
  • Renamed QualifiedName::imported to user_defined which is the more common term when talking about builtins vs the rest/user defined functions.

Test plan

cargo test

@MichaReiser MichaReiser changed the base branch from main to rename-call-path March 3, 2024 18:56
Base automatically changed from rename-call-path to main March 4, 2024 09:06
@MichaReiser MichaReiser force-pushed the qualified-name-lifetimes branch 3 times, most recently from bd43f3d to 293adf4 Compare March 4, 2024 20:22
@MichaReiser MichaReiser added the internal An internal refactor or improvement label Mar 4, 2024
Copy link

codspeed-hq bot commented Mar 4, 2024

CodSpeed Performance Report

Merging #10214 will not alter performance

Comparing qualified-name-lifetimes (9b04828) with main (d441338)

Summary

✅ 30 untouched benchmarks

Copy link
Contributor

github-actions bot commented Mar 4, 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.

@MichaReiser MichaReiser force-pushed the qualified-name-lifetimes branch 3 times, most recently from 89a3488 to 41b8fd2 Compare March 4, 2024 22:22
@MichaReiser MichaReiser changed the title qualified name lifetimes refactor: Use QualifiedName for Imported::call_path Mar 4, 2024
@MichaReiser MichaReiser force-pushed the qualified-name-lifetimes branch 2 times, most recently from ee0c9d8 to 87260fb Compare March 5, 2024 09:00
Self {
segments: iter.into_iter().collect(),
impl Display for QualifiedName<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Copy link
Member Author

Choose a reason for hiding this comment

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

Inlined from format_qualified_name_segments. We no longer need format_qualified_name_segments because the code that used Box<[&'astr]> can now call into this display implementation.

w.write_char('.')?;
impl<'a> UnqualifiedName<'a> {
/// Convert an `Expr` to its [`UnqualifiedName`] (like `["typing", "List"]`).
pub fn from_expr(expr: &'a Expr) -> Option<Self> {
Copy link
Member Author

Choose a reason for hiding this comment

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

Inlined from collect_segments (see separate commit)

Comment on lines 287 to 303
let mut segments = Vec::with_capacity(SMALL_LEN * 2);

let mut current = &*attr8.value;

loop {
current = match current {
Expr::Attribute(attr) => {
segments.push(attr.attr.as_str());
&*attr.value
}
Expr::Name(nodes::ExprName { id, .. }) => {
segments.push(id.as_str());
break;
}
_ => break,
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I rewrote this to no longer require recursion (which creates new SegmentsVecs internally, which seems unnecessary when we know that we need a Vec anyway).

fn call_path(&self) -> &[&'a str] {
self.qualified_name.as_ref()
fn qualified_name(&self) -> &QualifiedName<'a> {
&self.qualified_name
}

/// For example, given `import foo.bar`, returns `["foo"]`.
fn module_name(&self) -> &[&'a str] {
Copy link
Member Author

Choose a reason for hiding this comment

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

It would be nice if we could return QualifiedNameRef or similar from here because the module name is a qualified name too. But I consider this out of the scope of this PR.

@MichaReiser MichaReiser marked this pull request as ready for review March 5, 2024 09:08
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.

w00t! LGTM!

struct SegmentsStack<'a> {
segments: [&'a str; SMALL_LEN],
len: usize,
}
Copy link
Member

Choose a reason for hiding this comment

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

It's possible arrayvec could simplify things a bit here, although it may have the same issue as smallvec.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, the arrayvec crate doesn't support the most complex operation, extending from an Iterator. That's why I think its use is limited. The main advantage I see is that it avoids initializing unused memory.

Copy link
Member Author

Choose a reason for hiding this comment

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

I gave it a quick try. Funny enough, it has a variance issue, but it manifests differently than smallvec

heap.as_slice(),
&["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]
);
}
Copy link
Member

Choose a reason for hiding this comment

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

Nice tests. :)

@MichaReiser MichaReiser merged commit 8ea5b08 into main Mar 6, 2024
17 checks passed
@MichaReiser MichaReiser deleted the qualified-name-lifetimes branch March 6, 2024 08:56
nkxxll pushed a commit to nkxxll/ruff that referenced this pull request Mar 10, 2024
)

## Summary

When you try to remove an internal representation leaking into another
type and end up rewriting a simple version of `smallvec`.

The goal of this PR is to replace the `Box<[&'a str]>` with
`Box<QualifiedName>` to avoid that the internal `QualifiedName`
representation leaks (and it gives us a nicer API too). However, doing
this when `QualifiedName` uses `SmallVec` internally gives us all sort
of funny lifetime errors. I was lost but @BurntSushi came to rescue me.
He figured out that `smallvec` has a variance problem which is already
tracked in servo/rust-smallvec#146

To fix the variants problem, I could use the smallvec-2-alpha-4 or
implement our own smallvec. I went with implementing our own small vec
for this specific problem. It obviously isn't as sophisticated as
smallvec (only uses safe code), e.g. it doesn't perform any size
optimizations, but it does its job.

Other changes:

* Removed `Imported::qualified_name` (the version that returns a
`String`). This can be replaced by calling `ToString` on the qualified
name.
* Renamed `Imported::call_path` to `qualified_name` and changed its
return type to `&QualifiedName`.
* Renamed `QualifiedName::imported` to `user_defined` which is the more
common term when talking about builtins vs the rest/user defined
functions.


## Test plan

`cargo test`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants