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

perf: Use type ids for type checking #1725

Merged
merged 1 commit into from
Sep 24, 2024
Merged

Conversation

joshua-spacetime
Copy link
Collaborator

A RelExpr now holds type ids instead of types.
This reduces the size of RelExpr.
It avoids copying and cloning types during type checking. Comparisons are also cheaper for primitive types.

@joshua-spacetime joshua-spacetime marked this pull request as ready for review September 19, 2024 19:36

/// Add a type to the context and return a [TyId] for it.
/// The [TyId] is not guaranteed to be unique to the type.
/// However for primitive types it will be.
Copy link
Contributor

Choose a reason for hiding this comment

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

There is any problem with making it unique?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No problem, it just requires more memory and it becomes a little more expensive to generate type ids. But the benefit is that we only need type ids to test for equality.

crates/planner/src/logical/ty.rs Outdated Show resolved Hide resolved
(BinOp::And | BinOp::Or, Type::Alg(AlgebraicType::Bool)) => true,
// Comparison operators take integers or floats
(BinOp::Lt | BinOp::Gt | BinOp::Lte | BinOp::Gte, Type::Alg(t)) => t.is_integer() || t.is_float(),
// Equality supports numerics, strings, and bytes
Copy link
Contributor

Choose a reason for hiding this comment

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

Why can't the others?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mainly to avoid inconsistencies with other type systems. In the case of Rust, sums and products correspond to enum and struct types. Those types could have custom Eq and Ord implementations and as of right now, we don't have a way of representing that in SpacetimeDB.

Copy link
Contributor

@mamcx mamcx left a comment

Choose a reason for hiding this comment

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

LGTM

@joshua-spacetime joshua-spacetime changed the title perf: Use ids instead of types for type checking perf: Use type ids for type checking Sep 23, 2024
A `RelExpr` now holds type ids instead of types.
This reduces the size of `RelExpr`.
It avoids copying and cloning types during type checking.
Comparisons are also cheaper for primitive types.
@joshua-spacetime joshua-spacetime added this pull request to the merge queue Sep 24, 2024
Merged via the queue into master with commit eadea95 Sep 24, 2024
8 checks passed
@joshua-spacetime joshua-spacetime deleted the joshua/perf/type-check branch September 24, 2024 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants