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

Add CSS calc #668

Closed
wants to merge 29 commits into from
Closed

Add CSS calc #668

wants to merge 29 commits into from

Conversation

kokoISnoTarget
Copy link

@kokoISnoTarget kokoISnoTarget commented Jun 21, 2024

Objective

Fixes: #225

Context

#232 (comment)
https://drafts.csswg.org/css-values/#funcdef-calc

Feedback wanted

Todo

  • Change RELEASES.md
  • Testing
    • Finish Parser for tests
    • Add tests
    • Test on other systems because of fun pointer stuff:
      • 32bit systems
      • Little and Big-endian systems
      • Non x86 platforms (Arm and whatever)
      • Windows and macOS
    • Miri
  • Add basic remaining CSS math functions
  • Documentation (not the best)
  • Performance
    • Don't be 15% slower
  • Clean things up
  • Impl Serde

Copy link
Collaborator

@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 broadly good to me. I wouldn't bother with a no_std implementation (alloc should be trivial I think). What I would ideally like is for this to be feature-flagged behind a calc feature. Possibly the best way to deal with feature flagging all the .clone()s is to define a new method on Dimension, etc which calls .clone() if the feature flag is enabled and consumes and returns self otherwise. That way the conditional compilation is largely restricted to one place.

However we have a bit of a problem, which is that I am seeing benchmark regressions (run cargo xbench from the root or cargo bench from the benchmarks directory) averaging around +50%. Which is rather a large overhead! And this is on benchmarks without any actual Calc values too. There are two obvious culprits for this:

  1. The increase in size of the style types
  2. Calls to .clone() not being able to be as easily optimised as memcopy's.

As a quick test I did a find/replace on Taffy's main branch, replacing all f32's with f64s, and this seemed to result in roughly a 20% performance hit. This suggests to me that it might be a bit of both.

My suggestions for resolving this would be either:

  1. The combination of:
    a. Using a "tagged pointer" representation (a union) to squash a Dimension down to 64 bits
    b. Adding a DimensionRef type or similar that borrows Dimension to cut down on clones where possible. Methods like resolve_or_zero also ought to possible to make to work without cloning.
    c. As Taffy never holds persistent copies of style types, we could also consider playing fast-and-loose with the borrow checking rules and storing a raw pointer (which could be Copy!)
  2. Creating a new global (per-TaffyTree instance) Vec/Slotmap of Calcs. And expecting users of Taffy to register their Calc values there before using the index/id in Dimension (which could potentially be made 32 bits if we did this). Resolving of Calcs would then be done via the LayoutPartialTree trait or similar.

src/style/dimension.rs Outdated Show resolved Hide resolved
src/style/mod.rs Outdated Show resolved Hide resolved
@nicoburns
Copy link
Collaborator

@kokoISnoTarget I've been reading some of your commits, and regarding the "union", I suspect you may actually want a newtype around a u32 (or u64 if you need 64 bits). You can then use type casts and bit manipulation operators to access the tag and the value(s).

@kokoISnoTarget
Copy link
Author

kokoISnoTarget commented Jul 3, 2024

@nicoburns I'm not quite sure what the problem of my current approach is, but I appreciate the feedback.

@alice-i-cecile alice-i-cecile added the enhancement New feature or request label Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Calc val
3 participants