Skip to content

Remove bounds and type checks from IngredientCache #937

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

Merged

Conversation

ibraheemdev
Copy link
Contributor

@ibraheemdev ibraheemdev commented Jul 18, 2025

@Veykril might need to verify my logic here, but I think this is correct given either the inventory feature or the nonce checks, as IngredientCache always loads the index directly from the database.

The safety arguments for unchecked access are less clear for other uses of lookup_ingredient. The most notable one is in DatabaseKeyIndex::maybe_changed_after, where I think it's actually possible that a function records a dependency on an ingredient from a different database, in which case we should panic. It would be nice if we could guarantee that ingredient indices are stable even with manual registration, but that sort of defeats the purpose.

Copy link

netlify bot commented Jul 18, 2025

Deploy Preview for salsa-rs canceled.

Name Link
🔨 Latest commit 9f8a376
🔍 Latest deploy log https://app.netlify.com/projects/salsa-rs/deploys/687ac11ddbf73f00084c076a

Copy link

codspeed-hq bot commented Jul 18, 2025

CodSpeed Performance Report

Merging #937 will improve performances by 5.77%

Comparing ibraheemdev:ibraheem/unchecked-ingredient-cache (9f8a376) with master (dba66f1)

Summary

⚡ 5 improvements
✅ 7 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
new[Input] 9.3 µs 8.8 µs +5.77%
new[SupertypeInput] 15.5 µs 14.8 µs +4.67%
mutating[10] 13.5 µs 12.9 µs +4.16%
mutating[20] 13.6 µs 13.1 µs +4.09%
mutating[30] 13.7 µs 13.2 µs +4.06%

@Veykril
Copy link
Member

Veykril commented Jul 19, 2025

Sounds reasonable to me

where I think it's actually possible that a function records a dependency on an ingredient from a different database

Would be interesting if we could create a weird test for this (though not necessary for this PR of course)

})
// SAFETY: `lookup_jar_by_type` returns a valid ingredient index, and the only
// ingredient created by our jar is the struct ingredient.
unsafe {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's technically possible that a salsa user implements their own Ingredient. Could a malicious Ingredient implementation invalidate the SAFETY assumption?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this just accesses the ingredient created by this jar.

/// Equivalent to the `downcast` methods on `any`.
/// Equivalent to the `downcast` methods on `Any`.
///
/// Because we do not have dyn-upcasting support, we need this workaround.
Copy link
Contributor

Choose a reason for hiding this comment

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

We could consider bumping the MSRV.

Copy link
Contributor

Choose a reason for hiding this comment

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

bumping msrv seems reasonable; rust-analyzer shouldn't have an issue with that (we're generally current-1).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Upcasting was stabilized in 1.86, which is quite recent. FWIW this is an old comment, so I'm not sure it makes sense to do in this PR (but I'm not opposed to it).

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW this is an old comment, so I'm not sure it makes sense to do in this PR (but I'm not opposed to it).

Agree, I think it's better to do in a separate PR. I only wanted to point out that bumping the MSRV is a possiblity

@MichaReiser MichaReiser added this pull request to the merge queue Jul 21, 2025
Merged via the queue into salsa-rs:master with commit 53cd6b1 Jul 21, 2025
12 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.

4 participants