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

New database design #538

Merged
merged 29 commits into from
Aug 4, 2024
Merged

New database design #538

merged 29 commits into from
Aug 4, 2024

Conversation

nikomatsakis
Copy link
Member

@nikomatsakis nikomatsakis commented Jul 27, 2024

Status: Ready

Prototype new database design. See the final commit for a write-up. This is ready to land but I was planning to do a bit more work, in particular removing the use of thread-local state.

Update: I've removed the thread-local state now.

KEY CHANGES

  • No more Handle -- just clone the database; when you try to make mutation, it will cancel other clones
  • No more thread-local state
  • You can use salsa::DatabaseImpl as a default database if you don't need customizations
  • The use of salsa::db is unchanged, you can still define your own database traits and types

Copy link

netlify bot commented Jul 27, 2024

Deploy Preview for salsa-rs canceled.

Name Link
🔨 Latest commit 1bce41f
🔍 Latest deploy log https://app.netlify.com/sites/salsa-rs/deploys/66af3a088db6e50008713708

@nikomatsakis nikomatsakis force-pushed the spindle2 branch 6 times, most recently from f28dbb0 to 5fd60e4 Compare July 28, 2024 10:59
We only ever need to upcast to shared references.

This change isn't necessary, just dead code
cleanup.
Also, move to a blanket impl'd trait.
Overall cleaner approach.
The traits are now quite simple:

* Database is the external trait
* ZalsaDatabase is the internal one, implemented
  by `#[salsa::db]`. It adds two methods,
  `zalsa` and `zalsa_mut`. Those give access
  to our internal methods.

For now I've hidden the methods behind
`&dyn Zalsa`. This is nice and clean but it may
be worth later refactoring to a `struct Zalsa`.
The distinction is dumb and should go away.
But we still need it for a bit.
Creating events if nobody is listening has
always bugged me.
Under this design, *all* databases are a
`DatabaseImpl<U>`, where the `U` implements
`UserData` (you can use `()` if there is none).

Code would default to `&dyn salsa::Database` but
if you want to give access to the userdata, you
can define a custom database trait
`MyDatabase: salsa::Databse` so long as you

* annotate `MyDatabase` trait definition of
  impls of `MyDatabase` with `#[salsa::db]`
* implement `MyDatabase` for `DatabaseImpl<U>`
  where `U` is your userdata (this could be a
  blanket impl, if you don't know the precise
  userdata type).

The `tests/common/mod.rs` shows the pattern.
Separate handles are no longer needed.
Each clone gets an independent local state.
Copy link

codspeed-hq bot commented Jul 28, 2024

CodSpeed Performance Report

Merging #538 will not alter performance

Comparing nikomatsakis:spindle2 (1bce41f) with master (7bdf51c)

Summary

✅ 1 untouched benchmarks

components/salsa-macro-rules/src/setup_input_struct.rs Outdated Show resolved Hide resolved
src/function/accumulated.rs Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
src/database.rs Outdated Show resolved Hide resolved
src/zalsa.rs Show resolved Hide resolved
examples/lazy-input/main.rs Outdated Show resolved Hide resolved
@nikomatsakis
Copy link
Member Author

@MichaReiser I've been AFK for a bit...hopefully will be getting more reliable internet access soon. I've been thinking it over and I think I can produce a variation of this branch where we have the old setup (i.e., you define a struct that contains the Storage), which should address the various concerns you've raised.

@MichaReiser
Copy link
Contributor

No worries. I hope you're having a great time. I don't have a strong preference over either approach. I think both is fine. The only real blocking item right now is that I don't know how to implement Upcast.

@nikomatsakis
Copy link
Member Author

@MichaReiser I retooled it now, take a look. I think you convinced me that the old approach was better. We now have:

  • For simple uses, the built-in Database trait and DatabaseImpl struct.
  • For more complex uses, you can define your own database trait and structs using #[salsa::db]; the struct must have storage: salsa::Storage<Self> as one of its fields.

This should really be synchronized with the
codespaces and github configuration but...
I'm not clever enough to do all that.
and add more comments
@nikomatsakis
Copy link
Member Author

I went ahead and pushed some other commits I had that make things miri-safe again (at least I think they do, let's see what CI has to say about it).

@nikomatsakis nikomatsakis added this pull request to the merge queue Aug 4, 2024
Merged via the queue into salsa-rs:master with commit 7bdf51c Aug 4, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants