Skip to content

Commit bdda4da

Browse files
committed
add a general section for the reasoning behind const generics
1 parent fa5908e commit bdda4da

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- [👋🏽 Welcome](./README.md)
44
- [📜 Charter](./CHARTER.md)
55
- [🔮 The vision](./vision.md)
6+
- [🤔 Why compile time evaluation](./vision/why-compile-time-evaluation.md)
67
- [🙋‍♀️ Cast of characters](./vision/characters.md)
78
- [🔀 Skill Tree](./skill-tree.md)
89
- [😱 Status quo](./vision/status_quo.md)

vision/why-compile-time-evaluation.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Why compile time evaluation?
2+
3+
This section is, in many ways, the most important. It aims to clarify the reasoning about any future decisions about const generics and const evaluation.
4+
5+
## Why do we want to evaluate things at compile time?
6+
7+
People already precompute certain data structures, lookup tables, and so on, at compile time.
8+
For example the crate `regex` could be able to compile a regex at compile time,
9+
improving the performance when using `regex` and even removing the need for runtime allocations.
10+
11+
Without good const evaluation support, is often easier to just use a `build.rs` file for this or compute it externally and generate the result outside of the ordinary compilation process. If that isn't possible or sensible, one often just requires the computation to happen at runtime, negatively impacting startup performance.
12+
13+
Computing things outside without using const eval makes it at lot more cumbersome to use and
14+
prevents the input for that computation to simply be a generic or function parameter.
15+
16+
## Why const generics?
17+
18+
In most cases, const evaluation by itself is good enough, so const generics isn't needed. An exception to this is
19+
if the value should influence the type system.
20+
21+
This is needed when influencing the layout of types, most often by using arrays.
22+
Using arrays instead of dynamic allocations or slices can improve the performance, reduces the dynamic memory footprint
23+
and can remove the runtime checks and implicit invariants.
24+
25+
Another reason is to be generic over configurations or to move certain conditions to compile time. Consider [image handles](https://github.com/EmbarkStudios/rust-gpu/blob/1431c18b9db70feafc64e5096a64e5fefffbed18/crates/spirv-std/src/image.rs#L31) in [`rust-gpu`](https://github.com/EmbarkStudios/rust-gpu) or TODO: example for state machines.
26+
27+
## What are our goals?
28+
29+
### Consistent: "just add `const`"
30+
31+
Writing code that works at compile time should at most require manually adding the needed `const` annotations at suggested places (or even automatically using `rustfix` or IDE support)
32+
until everything compiles. One should not be required to avoid large parts of the language to do this.
33+
Adding and using const parameters should be straight forward without weird restrictions or a lot required knowledge.
34+
Using const generic items from different libraries should work without issues.
35+
36+
### Approachable: "what is `[u8; my_expr]:,` and why is it needed"
37+
38+
Reading or writing code which can be used at compile time should ideally not be more complex
39+
than writing other code. Requiring a lot of weird trait bounds or other hacks should be avoided as much as possible.
40+
Supporting compile time evaluation should not worsen the experience of users, especially for beginners using this code.
41+
42+
## Reliable: "if it passes `cargo check`, it works"
43+
44+
Const evaluation failures should be detected as early as possible, even when using `cargo check`. When writing library code, incorrect constants
45+
should cause warnings and errors whenever possible.

0 commit comments

Comments
 (0)