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

H-3426: Implement folding for data type constraints #5380

Merged

Conversation

TimDiekmann
Copy link
Member

🌟 What is the purpose of this PR?

When data types are resolved the constraints should be folded and minimized. This is the groundwork and logic to be used when combining different constraints.

🔍 What does this change?

  • It's not a good idea to implement combine on Constraint as Constraint is generic and combine would not use the generic parameter. Instead, I renamed Constraint to ConstraintValidator and added a new Constraint trait. This adds a combine method which takes a mutable reference to a constraint and consumes another constraint. If they can be fully combined, None is returned, otherwise Some with the remaining constraints. If the constraints cannot be combined at all because it would be unsatisfiable it will error.
  • Add fold_intersections function to construct a vector of all intersections using the combine method. This Is used to shrink the allOf constraints in the closed data types.

This PR is still in draft as I have another PR in the queue to implement the number combination logic and need some more testing around the behavior and signatures. Probably I'm replacing the mutable reference with an owned value. Methods with mutable references returning a results need to be careful to not change the value if they return an error (side-effects), in most cases this is not desired.

Pre-Merge Checklist 🚀

🚢 Has this modified a publishable library?

This PR:

  • does not modify any publishable blocks or libraries, or modifications do not need publishing

📜 Does this require a change to the docs?

The changes in this PR:

  • are internal and do not require a docs change

🕸️ Does this require a change to the Turbo Graph?

The changes in this PR:

  • do not affect the execution graph

⚠️ Known issues

🐾 Next steps

  • H-3427: Implement folding for number constraints
  • H-3428: Implement folding for string constraints
  • H-3429: Implement folding for array constraints
  • H-3430: Implement folding for anyOf constraints

🛡 What tests cover this?

Tests will be added in the follow-ups

@TimDiekmann TimDiekmann requested a review from indietyp October 11, 2024 16:29
@TimDiekmann TimDiekmann self-assigned this Oct 11, 2024
@github-actions github-actions bot added area/apps > hash* Affects HASH (a `hash-*` app) area/libs Relates to first-party libraries/crates/packages (area) type/eng > backend Owned by the @backend team labels Oct 11, 2024
Base automatically changed from t/h-3425-provide-a-simple-is_valid-function to main October 11, 2024 17:34
Copy link
Member

@indietyp indietyp left a comment

Choose a reason for hiding this comment

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

minor question / spelling mistake

@TimDiekmann TimDiekmann requested a review from indietyp October 17, 2024 09:06
Copy link
Contributor

Benchmark results

@rust/graph-benches – Integrations

representative_read_entity

Function Value Mean Flame graphs
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/person/v/1 $$16.6 \mathrm{ms} \pm 246 \mathrm{μs}\left({\color{red}19.6 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/uk-address/v/1 $$16.8 \mathrm{ms} \pm 247 \mathrm{μs}\left({\color{gray}0.415 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/organization/v/1 $$16.3 \mathrm{ms} \pm 199 \mathrm{μs}\left({\color{lightgreen}-18.039 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/building/v/1 $$17.3 \mathrm{ms} \pm 190 \mathrm{μs}\left({\color{lightgreen}-29.976 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/page/v/2 $$16.9 \mathrm{ms} \pm 242 \mathrm{μs}\left({\color{gray}0.783 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/book/v/1 $$17.0 \mathrm{ms} \pm 207 \mathrm{μs}\left({\color{gray}0.694 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/song/v/1 $$16.3 \mathrm{ms} \pm 166 \mathrm{μs}\left({\color{gray}-0.415 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/playlist/v/1 $$15.7 \mathrm{ms} \pm 188 \mathrm{μs}\left({\color{lightgreen}-7.527 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id entity type ID: https://blockprotocol.org/@alice/types/entity-type/block/v/1 $$16.1 \mathrm{ms} \pm 180 \mathrm{μs}\left({\color{gray}-1.490 \mathrm{\%}}\right) $$ Flame Graph

representative_read_entity_type

Function Value Mean Flame graphs
get_entity_type_by_id Account ID: d4e16033-c281-4cde-aa35-9085bf2e7579 $$1.44 \mathrm{ms} \pm 6.62 \mathrm{μs}\left({\color{gray}0.581 \mathrm{\%}}\right) $$ Flame Graph

scaling_read_entity_complete_zero_depth

Function Value Mean Flame graphs
entity_by_id 50 entities $$4.00 \mathrm{ms} \pm 17.2 \mathrm{μs}\left({\color{gray}-2.787 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 10 entities $$2.04 \mathrm{ms} \pm 14.2 \mathrm{μs}\left({\color{gray}0.144 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 25 entities $$2.53 \mathrm{ms} \pm 15.8 \mathrm{μs}\left({\color{gray}-1.799 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 1 entities $$1.87 \mathrm{ms} \pm 7.64 \mathrm{μs}\left({\color{gray}-0.467 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 5 entities $$1.92 \mathrm{ms} \pm 11.3 \mathrm{μs}\left({\color{gray}-0.079 \mathrm{\%}}\right) $$ Flame Graph

scaling_read_entity_linkless

Function Value Mean Flame graphs
entity_by_id 1000 entities $$2.76 \mathrm{ms} \pm 12.7 \mathrm{μs}\left({\color{lightgreen}-13.405 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 100 entities $$2.06 \mathrm{ms} \pm 10.7 \mathrm{μs}\left({\color{gray}0.189 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 10 entities $$1.87 \mathrm{ms} \pm 7.19 \mathrm{μs}\left({\color{gray}-1.568 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 10000 entities $$9.47 \mathrm{ms} \pm 135 \mathrm{μs}\left({\color{red}5.54 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 1 entities $$1.90 \mathrm{ms} \pm 6.43 \mathrm{μs}\left({\color{gray}-0.566 \mathrm{\%}}\right) $$ Flame Graph

representative_read_multiple_entities

Function Value Mean Flame graphs
entity_by_property depths: DT=2, PT=2, ET=2, E=2 $$60.4 \mathrm{ms} \pm 293 \mathrm{μs}\left({\color{gray}-0.917 \mathrm{\%}}\right) $$ Flame Graph
entity_by_property depths: DT=255, PT=255, ET=255, E=255 $$69.7 \mathrm{ms} \pm 278 \mathrm{μs}\left({\color{gray}-0.607 \mathrm{\%}}\right) $$ Flame Graph
entity_by_property depths: DT=0, PT=0, ET=0, E=0 $$40.8 \mathrm{ms} \pm 271 \mathrm{μs}\left({\color{gray}-0.803 \mathrm{\%}}\right) $$ Flame Graph
entity_by_property depths: DT=0, PT=0, ET=0, E=2 $$44.8 \mathrm{ms} \pm 197 \mathrm{μs}\left({\color{gray}-0.906 \mathrm{\%}}\right) $$ Flame Graph
entity_by_property depths: DT=0, PT=2, ET=2, E=2 $$55.7 \mathrm{ms} \pm 222 \mathrm{μs}\left({\color{gray}-1.021 \mathrm{\%}}\right) $$ Flame Graph
entity_by_property depths: DT=0, PT=0, ET=2, E=2 $$51.3 \mathrm{ms} \pm 201 \mathrm{μs}\left({\color{gray}-1.691 \mathrm{\%}}\right) $$ Flame Graph
link_by_source_by_property depths: DT=2, PT=2, ET=2, E=2 $$101 \mathrm{ms} \pm 605 \mathrm{μs}\left({\color{gray}-0.122 \mathrm{\%}}\right) $$ Flame Graph
link_by_source_by_property depths: DT=255, PT=255, ET=255, E=255 $$109 \mathrm{ms} \pm 455 \mathrm{μs}\left({\color{gray}-1.217 \mathrm{\%}}\right) $$ Flame Graph
link_by_source_by_property depths: DT=0, PT=0, ET=0, E=0 $$43.6 \mathrm{ms} \pm 274 \mathrm{μs}\left({\color{gray}-1.972 \mathrm{\%}}\right) $$ Flame Graph
link_by_source_by_property depths: DT=0, PT=0, ET=0, E=2 $$81.1 \mathrm{ms} \pm 494 \mathrm{μs}\left({\color{gray}-2.222 \mathrm{\%}}\right) $$ Flame Graph
link_by_source_by_property depths: DT=0, PT=2, ET=2, E=2 $$96.0 \mathrm{ms} \pm 452 \mathrm{μs}\left({\color{gray}-1.344 \mathrm{\%}}\right) $$ Flame Graph
link_by_source_by_property depths: DT=0, PT=0, ET=2, E=2 $$92.1 \mathrm{ms} \pm 691 \mathrm{μs}\left({\color{gray}-0.754 \mathrm{\%}}\right) $$ Flame Graph

scaling_read_entity_complete_one_depth

Function Value Mean Flame graphs
entity_by_id 50 entities $$276 \mathrm{ms} \pm 1.79 \mathrm{ms}\left({\color{lightgreen}-81.893 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 10 entities $$53.0 \mathrm{ms} \pm 268 \mathrm{μs}\left({\color{red}63.9 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 25 entities $$75.9 \mathrm{ms} \pm 459 \mathrm{μs}\left({\color{gray}-1.571 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 1 entities $$20.4 \mathrm{ms} \pm 125 \mathrm{μs}\left({\color{gray}0.242 \mathrm{\%}}\right) $$ Flame Graph
entity_by_id 5 entities $$25.7 \mathrm{ms} \pm 178 \mathrm{μs}\left({\color{gray}1.25 \mathrm{\%}}\right) $$ Flame Graph

@TimDiekmann TimDiekmann enabled auto-merge October 17, 2024 14:08
@TimDiekmann TimDiekmann added this pull request to the merge queue Oct 17, 2024
Merged via the queue into main with commit b0fa599 Oct 17, 2024
102 checks passed
@TimDiekmann TimDiekmann deleted the t/h-3426-implement-folding-for-data-type-constraints branch October 17, 2024 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/apps > hash* Affects HASH (a `hash-*` app) area/libs Relates to first-party libraries/crates/packages (area) type/eng > backend Owned by the @backend team
Development

Successfully merging this pull request may close these issues.

2 participants