Skip to content

Use struct NodeId(u64) instead of slotmap::DefaultKey in all public APIs#426

Merged
nicoburns merged 10 commits intoDioxusLabs:mainfrom
nicoburns:use-u64-instead-of-slotmap-key-in-layouttree
Apr 10, 2023
Merged

Use struct NodeId(u64) instead of slotmap::DefaultKey in all public APIs#426
nicoburns merged 10 commits intoDioxusLabs:mainfrom
nicoburns:use-u64-instead-of-slotmap-key-in-layouttree

Conversation

@nicoburns
Copy link
Copy Markdown
Collaborator

Objective

It's not currenty possible to use the LayoutTree trait without using a SlotMap as the backing storage because it uses slotmap::DefaultKey in the API! This replaces that with u64.

Fixes #327

Notes

Benchmark result show this as perf neutral even though a small amount of work is introduced by converting the DefaultKey (backed by two u32s) into a u64.

Possible alternatives

We could make all of our code generic over the key type like in #326. However this introduces generics all over the place affecting both code readability and compile times. I figure that everyone is likely to have a u64 compatible key, and if someone doesn't then we can revisit this issue at a later date.

@nicoburns nicoburns added usability Make the library more comfortable to use breaking-change A change that breaks our public interface labels Apr 10, 2023
@nicoburns nicoburns mentioned this pull request Apr 10, 2023
13 tasks
@TimJentzsch
Copy link
Copy Markdown
Collaborator

I think I would prefer to have an explicit newtype for this (e.g. NodeId) that wraps u64 and provides From conversion methods.

That would make the code easier to read (effectively adding more documentation) and would make it easier to change the internal representation later if needed.

@nicoburns
Copy link
Copy Markdown
Collaborator Author

nicoburns commented Apr 10, 2023

I think I would prefer to have an explicit newtype for this (e.g. NodeId) that wraps u64 and provides From conversion methods.

Hmm... so something like:

struct NodeId(u64)

I can see advantages of this (for one I could implement conversion function to and from slotmap keys, which would reduce a bunch of boilerplate). Would you want that type exposed externally in the LayoutTree trait instead of u64, or would it just be for internal use?

@TimJentzsch
Copy link
Copy Markdown
Collaborator

I think I would prefer to have an explicit newtype for this (e.g. NodeId) that wraps u64 and provides From conversion methods.

Hmm... so something like:

struct NodeId(u64)

Yup, that's exactly what I had in mind :)

I can see advantages of this (for one I could implement conversion function to and from slotmap keys, which would reduce a bunch of boilerplate). Would you want that type exposed externally in the LayoutTree trait instead of u64, or would it just be for internal use?

I think it would be reasonable to export it as well, but I don't have strong opinions on this

Copy link
Copy Markdown
Collaborator

@Weibye Weibye left a comment

Choose a reason for hiding this comment

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

The change itself looks good, I do agree with @TimJentzsch about the newtype id.

@nicoburns nicoburns force-pushed the use-u64-instead-of-slotmap-key-in-layouttree branch from 025cb79 to 23612e1 Compare April 10, 2023 19:34
@nicoburns
Copy link
Copy Markdown
Collaborator Author

Ok, so I've introduced a NodeId newtype as suggested. And I've gone ahead and replaced all public uses of type taffy::node::Node = slotmap::DefaultKey with it (and then deleted taffy::node::Node). This is super nice as it makes slotmap an implementation detail that is no longer part of the public API at all.

NodeId has From/Into conversions defined for u64, usize (which are intended for external interop), and slotmap::DefaultKey (which we use internally).

Copy link
Copy Markdown
Collaborator

@TimJentzsch TimJentzsch left a comment

Choose a reason for hiding this comment

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

LGTM.

I also like the naming of NodeId more than the previous Node, which indicated that there is some data stored in Node.
This was a source of confusion for me before.

@TimJentzsch
Copy link
Copy Markdown
Collaborator

Do you want to add this to the release notes in this PR or in a future one?

@nicoburns
Copy link
Copy Markdown
Collaborator Author

Do you want to add this to the release notes in this PR or in a future one?

Release notes added :)

@nicoburns nicoburns changed the title Use u64 instead of slotmap::DefaultKey in the LayoutTree trait Use struct NodeId(u64) instead of slotmap::DefaultKey in all public APIs Apr 10, 2023
@nicoburns
Copy link
Copy Markdown
Collaborator Author

Performance seems to be roughly neutral :) Merging!

@nicoburns nicoburns merged commit e997fa3 into DioxusLabs:main Apr 10, 2023
@nicoburns nicoburns deleted the use-u64-instead-of-slotmap-key-in-layouttree branch April 10, 2023 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change A change that breaks our public interface usability Make the library more comfortable to use

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LayoutTree trait is coupled to slotmap

3 participants