Skip to content

Implement Data for Vec<Data>#911

Merged
xStrom merged 2 commits intolinebender:masterfrom
chris-zen:master
May 9, 2020
Merged

Implement Data for Vec<Data>#911
xStrom merged 2 commits intolinebender:masterfrom
chris-zen:master

Conversation

@chris-zen
Copy link
Contributor

This PR implements the Data trait for a Vec<T> where T: Data, which solves the following case without having to use #[data(same_fn = "PartialEq::eq")]:

#[derive(Data)]
struct A {
  // ...
}

#[derive(Data)]
struct B {
  v: Vec<A>
  // ...
}

Copy link
Member

@xStrom xStrom left a comment

Choose a reason for hiding this comment

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

This seems useful. Thanks!

@xStrom xStrom merged commit b5dcedf into linebender:master May 9, 2020
@cmyr
Copy link
Member

cmyr commented May 10, 2020

so long-term we don't want data for vec; we want data-aware collection types that are cheap to clone; and currently, if you're using a vec for data, it should probably be in an Arc.

@xStrom
Copy link
Member

xStrom commented May 10, 2020

Should this be reverted? 🤔

@luleyleo
Copy link
Collaborator

Well, I think it is nice for ergonomics, as Arc<Vec<T>> is quite annoying, especially when ending up with nesting Arc<Vec<Arc<Vec<T>>>>.

But some good collection types, maybe similar to the Im ones would be a much better solution, as using Vec<T> could lead to surprisingly* bad performance.

Thus I'm not to happy about this change as it pretty much will have to be reverted at some point.

*Mostly for people new to druid

@xStrom
Copy link
Member

xStrom commented May 10, 2020

Somewhat tangential, but I'm also wondering under what scenario the data goes on multiple threads that it needs an Arc instead of Rc. 🤔 I haven't looked at the code too deeply with this question in mind, but most of the platform code which generates events seems pretty single-threaded.

@luleyleo
Copy link
Collaborator

luleyleo commented May 10, 2020

Yes, I think there is a note somewhere about the possibility of using Rc but that there is no guarantee for it to work .
And something like 'I haven't looked at the code too deeply' even was the reasoning for the note, I believe.

@xStrom
Copy link
Member

xStrom commented May 10, 2020

After giving it some further thought, even if the underlying platform is single-threaded, druid could potentially do its own event handling in a fan-out way. Global events could be ran on a separate thread per window. Even a single window could split up the widget tree, running the event on half of the tree on one thread and the other half on another.

It's not a simple change though and it's an open question how big a widget tree would have to be for multi-threaded work to be faster. This deserves its seperate topic, probably on zulip to start with.

@cmyr
Copy link
Member

cmyr commented May 11, 2020

Yea, the choice of Arc is that it should be no slower than Rc in the uncontested case, and it sets us up for a future where we're doing multi-threaded stuff. 🤷. (I'm imagining more passing things back and forth explicitly to offload work).

My gut feeling is that this should be reverted, but maybe also that we should speed up work on adding some immutable collection types; we've also had performance issues with people using String in their Data.

medium term I'd like to discourage use of String (and bring over the rope from xi) and I'd also like good general purpose immutable set and list collections; ideally this would be based on some ecosystem crate like im.rs.

@cmyr
Copy link
Member

cmyr commented May 11, 2020

Looking at source, it seems like the im crate has ptr_eq methods on its collections, so it should be trivial to implement Data for these types, and encourage their use in druid? We could even reexport them if we wanted, since we'll need to import them anyway?

@richard-uk1
Copy link
Collaborator

I think some notes in the docs would be good, as people will instinctively use Vec for their Data impls. Maybe it could go in the Data section of the book?

@chris-zen
Copy link
Contributor Author

Thanks for the discussion, I understand now why I should avoid direct use of Vec. I'll update my code to use a clone-friendly data structure. Feel free to revert.
Keep the great work 💪

@cmyr
Copy link
Member

cmyr commented May 22, 2020

@chris-zen Thanks for actually lighting the fire to get us to bring in some druid-appropriate collections. ;)

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.

5 participants