Skip to content

[Debuginfo] Optimize MSVC enum visualizer - #166015

Open
Walnut356 wants to merge 1 commit into
rust-lang:mainfrom
Walnut356:enum_opt
Open

Walnut356 wants to merge 1 commit into
rust-lang:mainfrom
Walnut356:enum_opt

Conversation

@Walnut356

@Walnut356 Walnut356 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

~10x's the performance of the msvc visualizers, even in conditions that aren't technically that bad. This change should have no user-facing affects aside from the speedup. Followup patch will apply the same to the gnu enum visualizers, though I suspect the speedup will be a bit smaller.

The following benchmarks were performed with this 3-variant enum definition, all times are in seconds:

enum SumType {
    Bare,
    Tuple(u8),
    Struct { a: u8, b: i32 },
}

Before:

(lldb) script import lldb_providers;import timeit
(lldb) script x = [lldb.frame.var(x) for x in ["sum_1", "sum_2", "sum_3"]]
(lldb) script print(timeit.timeit("[lldb_providers.MSVCEnumSummaryProvider(y, {}) for y in x]", globals=globals(), number=1))     
0.009645599988289177
(lldb) script print(timeit.timeit("[lldb_providers.MSVCEnumSummaryProvider(y, {}) for y in x]", globals=globals(), number=1000))
0.2783683000598103
(lldb) script print(timeit.timeit("[lldb_providers.MSVCEnumSummaryProvider(y, {}) for y in x]", globals=globals(), number=10000))
2.822943899896927
(lldb) script print(timeit.timeit("[lldb_providers.MSVCEnumSummaryProvider(y, {}) for y in x]", globals=globals(), number=100000))
29.64646710001398

After:

(lldb) script import lldb_providers;import timeit
(lldb) script x = [lldb.frame.var(x) for x in ["sum_1", "sum_2", "sum_3"]]
(lldb) script print(timeit.timeit("[lldb_providers.MSVCEnumSummaryProvider(y, {}) for y in x]", globals=globals(), number=1))
0.00016649998724460602
(lldb) script print(timeit.timeit("[lldb_providers.MSVCEnumSummaryProvider(y, {}) for y in x]", globals=globals(), number=1000)) 
0.03421209997031838
(lldb) script print(timeit.timeit("[lldb_providers.MSVCEnumSummaryProvider(y, {}) for y in x]", globals=globals(), number=10000))
0.27389169996604323
(lldb) script print(timeit.timeit("[lldb_providers.MSVCEnumSummaryProvider(y, {}) for y in x]", globals=globals(), number=100000))
2.733957500080578

Problem

SyntheticProvider.update() is called by LLDB (more or less) whenever you interact with a variable, even if it hasn't changed. Re-requesting the same variable (e.g. v var) typically also invokes an update call.

The MSVC enum providers were doing quite a lot of work within update to determine which variant to use, since we have to iterate through each possible variant and trial-and-error check a bunch of statics. Additionally, due to a (slightly lazy, slightly required) decision I made when writing them originally, the summary provider would run the enum through the entire synthetic provider again, effectively doubling the number of update calls whenever the summary is printed.

Since update was effectively O(number_of_variants), printing frames or large vecs could be noticeably slow. That slowness comes up more often because of how common enums are. Enums with lots of variants could cause issues too.

Solution

Since enum layouts don't change, there's no reason to traverse them more than once. While we could (and many providers do) handle this in __init__, that would still mean we traverse enum layouts on every SBValue object creation.

Instead, this patch implements a global cache to store enum layouts (with considerations to make sure the cache doesn't get stale or leak memory, there's a fairly lengthy comment explaining the rationale there). After storing the enum layout, retrieving any given variant is, at worst, 2 dict lookups + a few int comparisons over a list that I'm 95% sure can't be more than length 1 anyway1.

cc @Kobzol @jieyouxu (this doesn't directly involve the test suite, but it's a somewhat complex change and y'all are probably the best equipped to review it 🥲)

Footnotes

  1. it's a list of the "untagged" variants that are described by a range (i.e. DISCR_BEGIN, DISCR_END). I don't think the compiler can output a type with more than one of these? At the very least, afaik LLDB relies on that being the case for their GNU handling since that just omits the discriminant entirely if a variant needs a range of values. In any case, I made it a list just to be safe, but realistically it should just be a single element to check if it can't find if the dict lookup fails. ↩

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 16, 2026
@rustbot

rustbot commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: debugger_visualizer
  • debugger_visualizer expanded to Mark-Simulacrum, Walnut356

@jieyouxu

Copy link
Copy Markdown
Member

r? me

@rustbot rustbot assigned jieyouxu and unassigned Mark-Simulacrum Nov 16, 2026
@Walnut356

Copy link
Copy Markdown
Contributor Author

@bors try jobs=test-x86_64-msvc-1

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 16, 2026
[Debuginfo] Optimize MSVC enum visualizer


try-job: test-x86_64-msvc-1
@rust-bors

rust-bors Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 7b4ded3 (7b4ded3d8417522206a514a06a44331a9a89fafb)
Base parent: 5f4effd (5f4effd579500175af844241eb179e756190f1e7)

@rust-bors

This comment has been minimized.

@rustbot

rustbot commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Comment thread src/etc/lldb_providers.py Resolved
Comment thread src/etc/lldb_providers.py Resolved
Comment thread src/etc/lldb_providers.py Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants