Skip to content

DynMap #2484

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

Merged
merged 1 commit into from
Dec 6, 2019
Merged

DynMap #2484

merged 1 commit into from
Dec 6, 2019

Conversation

matklad
Copy link
Member

@matklad matklad commented Dec 5, 2019

Implement a DynMap a semi-dynamic, semi-static map, which helps to thread heterogeneously typed info in a uniform way. Totally inspired by https://github.com/JetBrains/kotlin/blob/df3bee30384787d8951ea548a4257c2cb52a16a3/compiler/frontend/src/org/jetbrains/kotlin/resolve/BindingContext.java.

@flodiebold wdyt? Seems like a potentially useful pattern for various source-map-like things.

@matklad
Copy link
Member Author

matklad commented Dec 5, 2019

The main idea here is that DynMap is both:

  • dynamic enough to process heterogeneous collection of items in a single action, without statically dispatching on every type
  • static enough that shooting yourself in the foot with runtime "type" error is, while not impossible, highly unlikely.

@@ -64,6 +68,12 @@ impl<N: AstNode> PartialEq for AstPtr<N> {
}
}

impl<N: AstNode> Hash for AstPtr<N> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.raw.hash(state)
Copy link
Member

Choose a reason for hiding this comment

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

what's the difference to the derived implementation here? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Derive would be impl<N: AstNode + Hash>

@@ -33,7 +33,9 @@ fn type_at_pos(db: &TestDB, pos: FilePosition) -> String {
let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap();
let fn_def = expr.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
let module = db.module_for_file(pos.file_id);
let func = module.child_from_source(db, InFile::new(pos.file_id.into(), fn_def)).unwrap();
let func = *module.child_by_source(db)[keys::FUNCTION]
.get(&InFile::new(pos.file_id.into(), fn_def))
Copy link
Member

Choose a reason for hiding this comment

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

I don't quite understand why the API is always building a whole map when it seems like all uses just immediately do get on it (maybe I missed something)

Copy link
Member Author

Choose a reason for hiding this comment

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

Building the whole map is easier: you just traverse everything and put it into the map. If you were directly looking for a specific Item, you would have first filter by item type and, in general, provide separate code for each type of looked for item. See the deleted child_from_source module, it pursued that approach.

The map should also be cache able in theory, so that repeated lookups for neighboring nodes will be fast.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe it makes sense to change () -> DynMap to (&mut DynMap) though: that way, we can change the API in the future to use a DynMap which drops unrelated keys on the floor.

}

impl<K, V, P> Key<K, V, P> {
#[allow(unused)]
Copy link
Member

Choose a reason for hiding this comment

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

this is used below, so is the allow necessary?

fn clone(&self) -> Key<K, V, P> {
*self
}
}
Copy link
Member

Choose a reason for hiding this comment

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

does derive not work for these impls?

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 won't for Copy, because K and V are generally not Copy

Copy link
Member

Choose a reason for hiding this comment

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

Aah right

@matklad
Copy link
Member Author

matklad commented Dec 6, 2019

So, @flodiebold what's your feeling here? Is this in general a worthwhile thing to try?

@flodiebold
Copy link
Member

Sure, I think it's worth a try!

@matklad
Copy link
Member Author

matklad commented Dec 6, 2019

bors r+

bors bot added a commit that referenced this pull request Dec 6, 2019
2484: WIP: DynMap r=matklad a=matklad

Implement a `DynMap` a semi-dynamic, semi-static map, which helps to thread heterogeneously typed info in a uniform way. Totally inspired by https://github.com/JetBrains/kotlin/blob/df3bee30384787d8951ea548a4257c2cb52a16a3/compiler/frontend/src/org/jetbrains/kotlin/resolve/BindingContext.java. 

@flodiebold wdyt? Seems like a potentially useful pattern for various source-map-like things.

Co-authored-by: Aleksey Kladov <[email protected]>
@matklad matklad changed the title WIP: DynMap DynMap Dec 6, 2019
This might, or might not help us to reduce boilerplate associated with
plumbing values from analysis to the IDE layer
@bors
Copy link
Contributor

bors bot commented Dec 6, 2019

Canceled

@matklad
Copy link
Member Author

matklad commented Dec 6, 2019

bors r+

bors bot added a commit that referenced this pull request Dec 6, 2019
2484: DynMap r=matklad a=matklad

Implement a `DynMap` a semi-dynamic, semi-static map, which helps to thread heterogeneously typed info in a uniform way. Totally inspired by https://github.com/JetBrains/kotlin/blob/df3bee30384787d8951ea548a4257c2cb52a16a3/compiler/frontend/src/org/jetbrains/kotlin/resolve/BindingContext.java. 

@flodiebold wdyt? Seems like a potentially useful pattern for various source-map-like things.

Co-authored-by: Aleksey Kladov <[email protected]>
@bors
Copy link
Contributor

bors bot commented Dec 6, 2019

Build succeeded

  • TypeScript
  • Rust

@bors bors bot merged commit 8c86963 into rust-lang:master Dec 6, 2019
@matklad matklad deleted the dyn-map branch December 6, 2019 20:39
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