Skip to content

Conversation

MichaReiser
Copy link
Contributor

@MichaReiser MichaReiser commented Aug 2, 2025

This is the same as #786 but during maybe_changed_after.

validate_same_iteration over pessimistically always returns false if a cycle head is currently being checked with maybe_changed_after because maybe_changed_after never pushes the current query on the stack.

This PR addresses the issue by comparing the cycle head's iteration with the latest memo iteration for that cycle head. This is the same as we already did for a cycle that spans multiple threads.

Copy link

netlify bot commented Aug 2, 2025

Deploy Preview for salsa-rs canceled.

Name Link
🔨 Latest commit 3c06ee4
🔍 Latest deploy log https://app.netlify.com/projects/salsa-rs/deploys/6891b2bc611a5b000864597d

@@ -377,7 +377,7 @@ where
let wait_result = ingredient
.wait_for(zalsa, cycle_head.database_key_index.key_index());

if !wait_result.is_cycle_with_other_thread() {
if !wait_result.is_cycle() {
Copy link
Contributor Author

@MichaReiser MichaReiser Aug 2, 2025

Choose a reason for hiding this comment

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

Trying to remember why I explicitly added is_cycle_with_other__thread in 30f6eb4 (#882)

It suggests that it is to fix a panic but there's no failing test :( Maybe it only failed on CI?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, it reproduces on CI (but why only on CI?)

Copy link
Contributor Author

@MichaReiser MichaReiser Aug 2, 2025

Choose a reason for hiding this comment

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

I initially restricted the logic here to only apply when this leads to a cross-thread cycle because the cycle_panic test started to hang on Github actions.

Restricting this logic to multi-threaded cycle does fix the test-hang but it's over restrictive. The alternative fix that I added now (the if below the huge comment) accomplishes the same without making any assumptions about on how many threads the cycle runs.

An alternative fix is to change maybe_changed_after to push the current query in deep_verify_memo. While I think that this would have the nice added benefit that our query stack traces are more accurate (they also show queries running `maybe_changed_after), it does have the significant downside that pushing a new query isn't free and we would have to pay that cost for all queries, not just queries participating in cycles.

That's why I opted for this fix instead.

Copy link

codspeed-hq bot commented Aug 2, 2025

CodSpeed Performance Report

Merging #954 will degrade performances by 4.91%

Comparing MichaReiser:micha/runaway-maybe-changed-after (3c06ee4) with master (86ca4a9)

Summary

❌ 1 regressions
✅ 11 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
amortized[Input] 2.3 µs 2.4 µs -4.91%

Comment on lines +1057 to +1065
db.assert_logs(expect![[r#"
[
"salsa_event(WillExecute { database_key: min_panic(Id(2)) })",
"salsa_event(WillExecute { database_key: min_panic(Id(1)) })",
"salsa_event(WillExecute { database_key: min_iterate(Id(0)) })",
"salsa_event(WillIterateCycle { database_key: min_iterate(Id(0)), iteration_count: IterationCount(1), fell_back: false })",
"salsa_event(WillExecute { database_key: min_panic(Id(1)) })",
"salsa_event(WillExecute { database_key: min_panic(Id(2)) })",
]"#]]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The output before the fix:

            "salsa_event(WillExecute { database_key: min_panic(Id(1)) })",
            "salsa_event(WillExecute { database_key: min_panic(Id(2)) })",
            "salsa_event(WillExecute { database_key: min_panic(Id(2)) })", <- unnecesary
            "salsa_event(WillExecute { database_key: min_panic(Id(2)) })", <- unnecesary
            "salsa_event(WillExecute { database_key: min_iterate(Id(0)) })",
            "salsa_event(WillIterateCycle { database_key: min_iterate(Id(0)), iteration_count: IterationCount(1), fell_back: false })",
            "salsa_event(WillExecute { database_key: min_panic(Id(1)) })",
            "salsa_event(WillExecute { database_key: min_panic(Id(2)) })",

@MichaReiser MichaReiser changed the title Allow validate_same_iteration slow path for maybe_changed_after allow reuse of cached provisional memos within the same cycle iteration during maybe_changed_after Aug 2, 2025
@MichaReiser MichaReiser requested a review from carljm August 2, 2025 21:41
@MichaReiser MichaReiser marked this pull request as ready for review August 2, 2025 21:42
@MichaReiser
Copy link
Contributor Author

No idea what's up with codspeed. This code doesn't exercise any fixpoint iteration logic

@MichaReiser MichaReiser force-pushed the micha/runaway-maybe-changed-after branch from f9225e6 to 67efabe Compare August 3, 2025 08:32
@MichaReiser MichaReiser force-pushed the micha/runaway-maybe-changed-after branch from 67efabe to c2f4827 Compare August 3, 2025 09:18
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Looks good, thank you!

@MichaReiser
Copy link
Contributor Author

I believe there are a few more bugs in maybe_changed_after related to fixpoint but I'll tackle them in separate PRs:

  1. I don't think it's safe to return Unchanged if validate_same_iteration returns true. We simply don't know yet if the value has changed.
  2. We should never return Unchanged for a provisional value (here https://github.com/MichaReiser/salsa/blob/c2f4827b512b82842dbc84b1ccc4367500e301ed/src/function/maybe_changed_after.rs#L191-L193)
  3. We need to update maybe_changed_after_cold to do the same retry logic as for fetch to correctly handle multi threaded validation, see Multithreaded maybe_changed_after with fixpoint iteration. #952

@MichaReiser MichaReiser enabled auto-merge August 5, 2025 07:32
@MichaReiser MichaReiser added this pull request to the merge queue Aug 5, 2025
Merged via the queue into salsa-rs:master with commit d66fe33 Aug 5, 2025
11 of 12 checks passed
@MichaReiser MichaReiser deleted the micha/runaway-maybe-changed-after branch August 5, 2025 07:48
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.

3 participants