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

Use taffy for tables #129

Merged
merged 15 commits into from
May 31, 2023
Merged

Use taffy for tables #129

merged 15 commits into from
May 31, 2023

Conversation

trimental
Copy link
Collaborator

Really excited for this PR. Should make future additions to table layout logic a lot simpler to deal with (for images and nesting etc).

Right now this doesn't cache layouts and forces text_cache and font_system to become an Arc<Mutex<>> but why fight the borrow checker when you can let it have its way with you. Plus premature optimization is the root of all evil so let's just enjoy properly sized tables.

Big thanks to @nicoburns for his help with this.

@CosmicHorrorDev CosmicHorrorDev linked an issue May 26, 2023 that may be closed by this pull request
@CosmicHorrorDev CosmicHorrorDev added C-enhancement Category: New feature or request A-table Area: Dealing with markdown tables labels May 26, 2023
@CosmicHorrorDev
Copy link
Collaborator

I'll be free to give this a look tomorrow if ya want 👍

Copy link
Collaborator

@CosmicHorrorDev CosmicHorrorDev left a comment

Choose a reason for hiding this comment

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

Only a couple tiny nits that can just have the suggestions approved to make things easy. Other than that everything looks good :)

Having to wrap the text cache and font system in a Mutex is a bit unfortunate since we don't ever try to access either simultaneously, but it looks like Measurable requires a Sync bound, so we can't use a RefCell. At the very least locking doesn't even show up when I profile things (probably because there's never any contention)

We could switch to parking_lot::Mutex just to avoid having to .unwrap() every time we acquire the lock. It's already in our dep tree in several places, so it doesn't actually add a dependency

src/text.rs Outdated
Comment on lines 205 to 206
text_cache: &Arc<Mutex<TextCache>>,
font_system: &Arc<Mutex<FontSystem>>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
text_cache: &Arc<Mutex<TextCache>>,
font_system: &Arc<Mutex<FontSystem>>,
text_cache: &Mutex<TextCache>,
font_system: &Mutex<FontSystem>,

The Arc wrappers can be dropped here because of deref magic

src/table.rs Outdated
Comment on lines 90 to 96
let max_columns = self.rows.iter().fold(self.headers.len(), |max, row| {
if row.len() > max {
row.len()
} else {
max
}
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
let max_columns = self.rows.iter().fold(self.headers.len(), |max, row| {
if row.len() > max {
row.len()
} else {
max
}
});
let max_columns = self
.rows
.iter()
.fold(self.headers.len(), |max, row| std::cmp::max(row.len(), max));

The if statement here can just be replaced with std::cmp::max(row.len(), max) (suggestion includes reformatting from rustfmt)

Copy link
Contributor

@nicoburns nicoburns left a comment

Choose a reason for hiding this comment

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

This looks good to me. There are some refactors that could be done, but if Taffy's scope within Inlyne is going to be expanded in future (e.g. to do the main vertical layout of the page) then it probably makes more sense to make those changes then.

@nicoburns
Copy link
Contributor

Right now this doesn't cache layouts

This should be quite easy to do. Just keep the Taffy instance around and reuse it (this would work particularly well for things like resizing where you wouldn't need to update any of the styles and could just pass in different top-level bounds). I imagine a typical markdown file is small enough that it'll be fast without any caching though :)

Having to wrap the text cache and font system in a Mutex is a bit unfortunate since we don't ever try to access either simultaneously, but it looks like Measurable requires a Sync bound, so we can't use a RefCell. At the very least locking doesn't even show up when I profile things (probably because there's never any contention)

This is definitely fixable in Taffy. It just requires us to be generic over the Measurable type and some juggling of type bounds (because some people need the Taffy struct to be Send+Sync). I've put up a PR in that direction here DioxusLabs/taffy#490

@trimental trimental merged commit 341db9c into main May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-table Area: Dealing with markdown tables C-enhancement Category: New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wide tables will clip off the right side
3 participants