Skip to content

Conversation

@topolarity
Copy link
Member

Without this PR, my system spends ~1.07 seconds just running store_backedges when doing using CairoMakie

With this change, that drops to 0.641 seconds

That's still not fast enough for me, but we do call this function 236,092 times so maybe it's understandable.

@topolarity topolarity requested a review from vtjnash April 14, 2025 21:47
assert(!found && "duplicate back-edge registered");
#endif
// reuse an already cached instance of this type, if possible
// TODO: use jl_cache_type_(tt) like cache_method does, instead of this linear scan?
Copy link
Member Author

Choose a reason for hiding this comment

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

It's worth seeing if we can bypass this remaining linear scan in the MethodTable edge insertion.

If I disable it manually, the timing drops to 440 milliseconds

@topolarity topolarity force-pushed the ct/typeinfer-edge-dedupe branch from 72735ee to b8aa007 Compare April 14, 2025 21:51
src/gf.c Outdated
jl_gc_wb(callee, backedges);
}
else {
#ifndef JL_NDEBUG
Copy link
Member

Choose a reason for hiding this comment

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

This introduces a race condition into the code (because of gc, threads, etc). Probably useful to know this scan is costly on some benchmarks though

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you clarify where the race condition is?

Do you mean that inference may simultaneously try to store_backedges for the same caller CI from two different threads?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see, we might be able to now assume that each CI is unique, while in older versions the MI were not expected to be unique

@topolarity topolarity force-pushed the ct/typeinfer-edge-dedupe branch 2 times, most recently from 27c2e29 to a42a06a Compare May 7, 2025 21:08
@topolarity
Copy link
Member Author

topolarity commented May 8, 2025

This test points to a serious flaw with this approach: https://buildkite.com/julialang/julia-master/builds/47272#0196ad27-29a2-4ce6-b08d-d7a11a23125b/914-1711

An invalidated CI is likely to repeatedly add the same back-edges when it is re-inferred, unless we force CI invalidation to also perform deletion of stale back-edges. We likely need to take a different direction here.

@topolarity topolarity marked this pull request as draft May 8, 2025 12:48
@topolarity
Copy link
Member Author

Actually this should be fine (for the same reason pointed out at #58117 (comment)). It is true that we leave a number of "zombie" back-edges around to invalidated CodeInstances, but that is made no worse by this PR

This is ready to go if CI is happy.

@topolarity topolarity marked this pull request as ready for review May 8, 2025 15:37
@vtjnash
Copy link
Member

vtjnash commented May 8, 2025

That test calls Core.Compiler.store_backedges, which is typically UB anyways to do outside of the NativeInterpreter (various locks and other constraints must be met, which are not possible outside of the native compiler), so that shouldn't matter. It could happen that we infer a CI twice (once for rettype, a second time for inferred code), but that is infrequent

topolarity added 4 commits May 8, 2025 13:39
Since the caller CodeInstance is always part of the identity that we are
de-duplicating on, this makes the linear scan much faster than it is
in `gf.c`
…Table`

These are restored in their entirety by staticdata.jl, so there's no
need to serialize them. Dropping them has the additional advantage of
making it unnecessary to de-duplicate edges in `gf.c`
On my system, this saves ~500 ms when loading CairoMakie (and all
dependent packages)
@topolarity topolarity force-pushed the ct/typeinfer-edge-dedupe branch from 67232ca to 7525568 Compare May 8, 2025 18:19
@topolarity topolarity requested review from a team and StefanKarpinski and removed request for a team May 8, 2025 18:19
@topolarity topolarity merged commit 6180ca0 into JuliaLang:master May 9, 2025
7 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