Skip to content

Commit 8b70a02

Browse files
authored
Rollup merge of #147552 - Walnut356:cleanup, r=Mark-Simulacrum
[Debugger Visualizers] Optimize lookup behavior # Background Almost all of the commands in `lldb_commands` used a regex to associate a type with the `synthetic_lookup` and `summary_lookup` python functions. When looking up a type, LLDB iterates through the commands in reverse order (so that new commands can overwrite old ones), stopping when it finds a match. These lookups are cached, but it's a shallow cache (e.g. when `Vec<T>` is matched by lldb, it will always point to `synthetic_lookup`, NOT the result of `synthetic_lookup` which would be `StdVecSyntheticProvider`). This becomes a problem because within `synthetic_lookup` and `summary_lookup` we run `classify_rust_type` which checks exact same regexes again. This causes 2 issues: 1. running the regexes via lldb commands is even more of a waste because the final check is a `.*` regex that associates with `synthetic_lookup` anyway 2. Every time lldb wants to display a value, that value must run the entirety of `synthetic_lookup` and run its type through 19 regexes + some assorted checks every single time. Those checks take between 1 and 100 microseconds depending on the type. On a 10,000 element `Vec<i32>` (which bypasses `classify_struct` and therefore the 19 regexes), ~30 milliseconds are spent on `classify_rust_type`. For a 10,000 element `Vec<UserDefinedStruct>` that jumps up to ~350 milliseconds. The salt on the wound is that some of those 19 regexes are useless (`BTreeMap` and `BTreeSet` which don't even have synthetic/summary providers so it doesn't matter if we know what type it is), and then the results of that lookup function use string-comparisons in a giant `if...elif...elif` chain. # Solution To fix all of that, the `lldb_commands` now point directly to their appropriate synthetic/summary when possible. In cases where there was extra logic, streamlined functions have been added that have much fewer types being passed in, thus only need to do one or two simple checks (e.g. `classify_hashmap` and `classify_hashset`). Some of the `lldb_commands` regexes were also consolidated to reduce the total number of commands we pass to lldb (e.g. `NonZero` An extra upshot is that `summary_lookup` could be completely removed due to being redundant.
2 parents b53da99 + 4463c58 commit 8b70a02

File tree

6 files changed

+281
-233
lines changed

6 files changed

+281
-233
lines changed

src/etc/gdb_lookup.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import re
44

55
from gdb_providers import *
6-
from rust_types import *
6+
from rust_types import RustType, classify_struct, classify_union
77

88

99
_gdb_version_matched = re.search("([0-9]+)\\.([0-9]+)", gdb.VERSION)
@@ -28,7 +28,7 @@ def classify_rust_type(type):
2828
if type_class == gdb.TYPE_CODE_UNION:
2929
return classify_union(type.fields())
3030

31-
return RustType.OTHER
31+
return RustType.Other
3232

3333

3434
def check_enum_discriminant(valobj):
@@ -85,7 +85,7 @@ def __init__(self, name):
8585

8686
def add(self, rust_type, provider):
8787
# Just use the rust_type as the name.
88-
printer = PrintByRustType(rust_type, provider)
88+
printer = PrintByRustType(rust_type.name, provider)
8989
self.type_map[rust_type] = printer
9090
self.subprinters.append(printer)
9191

@@ -99,23 +99,23 @@ def __call__(self, valobj):
9999
printer = RustPrettyPrinter("rust")
100100
# use enum provider only for GDB <7.12
101101
if gdb_version[0] < 7 or (gdb_version[0] == 7 and gdb_version[1] < 12):
102-
printer.add(RustType.ENUM, enum_provider)
103-
printer.add(RustType.STD_STRING, StdStringProvider)
104-
printer.add(RustType.STD_OS_STRING, StdOsStringProvider)
105-
printer.add(RustType.STD_STR, StdStrProvider)
106-
printer.add(RustType.STD_SLICE, StdSliceProvider)
107-
printer.add(RustType.STD_VEC, StdVecProvider)
108-
printer.add(RustType.STD_VEC_DEQUE, StdVecDequeProvider)
109-
printer.add(RustType.STD_BTREE_SET, StdBTreeSetProvider)
110-
printer.add(RustType.STD_BTREE_MAP, StdBTreeMapProvider)
111-
printer.add(RustType.STD_HASH_MAP, hashmap_provider)
112-
printer.add(RustType.STD_HASH_SET, hashset_provider)
113-
printer.add(RustType.STD_RC, StdRcProvider)
114-
printer.add(RustType.STD_ARC, lambda valobj: StdRcProvider(valobj, is_atomic=True))
115-
116-
printer.add(RustType.STD_CELL, StdCellProvider)
117-
printer.add(RustType.STD_REF, StdRefProvider)
118-
printer.add(RustType.STD_REF_MUT, StdRefProvider)
119-
printer.add(RustType.STD_REF_CELL, StdRefCellProvider)
120-
121-
printer.add(RustType.STD_NONZERO_NUMBER, StdNonZeroNumberProvider)
102+
printer.add(RustType.Enum, enum_provider)
103+
printer.add(RustType.StdString, StdStringProvider)
104+
printer.add(RustType.StdOsString, StdOsStringProvider)
105+
printer.add(RustType.StdStr, StdStrProvider)
106+
printer.add(RustType.StdSlice, StdSliceProvider)
107+
printer.add(RustType.StdVec, StdVecProvider)
108+
printer.add(RustType.StdVecDeque, StdVecDequeProvider)
109+
printer.add(RustType.StdBTreeSet, StdBTreeSetProvider)
110+
printer.add(RustType.StdBTreeMap, StdBTreeMapProvider)
111+
printer.add(RustType.StdHashMap, hashmap_provider)
112+
printer.add(RustType.StdHashSet, hashset_provider)
113+
printer.add(RustType.StdRc, StdRcProvider)
114+
printer.add(RustType.StdArc, lambda valobj: StdRcProvider(valobj, is_atomic=True))
115+
116+
printer.add(RustType.StdCell, StdCellProvider)
117+
printer.add(RustType.StdRef, StdRefProvider)
118+
printer.add(RustType.StdRefMut, StdRefProvider)
119+
printer.add(RustType.StdRefCell, StdRefCellProvider)
120+
121+
printer.add(RustType.StdNonZeroNumber, StdNonZeroNumberProvider)

src/etc/lldb_commands

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,85 @@
1+
# LLDB iterates through these in reverse order to discover summaries/synthetics that means the top
2+
# of the list can be "overwritten" by items lower on the list. Be careful when reordering items.
3+
14
# Forces test-compliant formatting to all other types
25
type synthetic add -l lldb_lookup.synthetic_lookup -x ".*" --category Rust
36
# Std String
47
type synthetic add -l lldb_lookup.StdStringSyntheticProvider -x "^(alloc::([a-z_]+::)+)String$" --category Rust
5-
type summary add -F lldb_lookup.StdStringSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)String$" --category Rust
8+
type summary add -F lldb_lookup.StdStringSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)String$" --category Rust
9+
610
# Std str
7-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?str$" --category Rust
8-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?str$" --category Rust
11+
type synthetic add -l lldb_lookup.StdSliceSyntheticProvider -x "^&(mut )?str$" --category Rust
12+
type summary add -F lldb_lookup.StdStrSummaryProvider -e -x -h "^&(mut )?str$" --category Rust
13+
914
## MSVC
1015
type synthetic add -l lldb_lookup.MSVCStrSyntheticProvider -x "^ref(_mut)?\$<str\$>$" --category Rust
1116
type summary add -F lldb_lookup.StdStrSummaryProvider -e -h -x "^ref(_mut)?\$<str\$>$" --category Rust
12-
# Array
13-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?\\[.+\\]$" --category Rust
14-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?\\[.+\\]$" --category Rust
15-
# Slice
17+
18+
# Array/Slice
19+
type synthetic add -l lldb_lookup.StdSliceSyntheticProvider -x "^&(mut )?\\[.+\\]$" --category Rust
20+
type summary add -F lldb_lookup.SizeSummaryProvider -e -x -h "^&(mut )?\\[.+\\]$" --category Rust
21+
1622
## MSVC
1723
type synthetic add -l lldb_lookup.MSVCStdSliceSyntheticProvider -x "^ref(_mut)?\$<slice2\$<.+> >" --category Rust
1824
type summary add -F lldb_lookup.StdSliceSummaryProvider -e -x -h "^ref(_mut)?\$<slice2\$<.+> >" --category Rust
25+
1926
# OsString
20-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::ffi::([a-z_]+::)+)OsString$" --category Rust
21-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::ffi::([a-z_]+::)+)OsString$" --category Rust
27+
type summary add -F lldb_lookup.StdOsStringSummaryProvider -e -x -h "^(std::ffi::([a-z_]+::)+)OsString$" --category Rust
28+
2229
# Vec
23-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)Vec<.+>$" --category Rust
24-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Vec<.+>$" --category Rust
30+
type synthetic add -l lldb_lookup.StdVecSyntheticProvider -x "^(alloc::([a-z_]+::)+)Vec<.+>$" --category Rust
31+
type summary add -F lldb_lookup.SizeSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)Vec<.+>$" --category Rust
32+
2533
# VecDeque
26-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)VecDeque<.+>$" --category Rust
27-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)VecDeque<.+>$" --category Rust
28-
# BTreeSet
29-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)BTreeSet<.+>$" --category Rust
30-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)BTreeSet<.+>$" --category Rust
31-
# BTreeMap
32-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)BTreeMap<.+>$" --category Rust
33-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)BTreeMap<.+>$" --category Rust
34+
type synthetic add -l lldb_lookup.StdVecDequeSyntheticProvider -x "^(alloc::([a-z_]+::)+)VecDeque<.+>$" --category Rust
35+
type summary add -F lldb_lookup.SizeSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)VecDeque<.+>$" --category Rust
36+
3437
# HashMap
35-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::collections::([a-z_]+::)+)HashMap<.+>$" --category Rust
36-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::collections::([a-z_]+::)+)HashMap<.+>$" --category Rust
38+
type synthetic add -l lldb_lookup.classify_hashmap -x "^(std::collections::([a-z_]+::)+)HashMap<.+>$" --category Rust
39+
type summary add -F lldb_lookup.SizeSummaryProvider -e -x -h "^(std::collections::([a-z_]+::)+)HashMap<.+>$" --category Rust
40+
3741
# HashSet
38-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::collections::([a-z_]+::)+)HashSet<.+>$" --category Rust
39-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::collections::([a-z_]+::)+)HashSet<.+>$" --category Rust
42+
type synthetic add -l lldb_lookup.classify_hashset -x "^(std::collections::([a-z_]+::)+)HashSet<.+>$" --category Rust
43+
type summary add -F lldb_lookup.SizeSummaryProvider -e -x -h "^(std::collections::([a-z_]+::)+)HashSet<.+>$" --category Rust
44+
4045
# Rc
41-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)Rc<.+>$" --category Rust
42-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Rc<.+>$" --category Rust
46+
type synthetic add -l lldb_lookup.StdRcSyntheticProvider -x "^(alloc::([a-z_]+::)+)Rc<.+>$" --category Rust
47+
type summary add -F lldb_lookup.StdRcSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)Rc<.+>$" --category Rust
48+
4349
# Arc
44-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)Arc<.+>$" --category Rust
45-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Arc<.+>$" --category Rust
50+
type synthetic add -l lldb_lookup.arc_synthetic -x "^(alloc::([a-z_]+::)+)Arc<.+>$" --category Rust
51+
type summary add -F lldb_lookup.StdRcSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)Arc<.+>$" --category Rust
52+
4653
# Cell
47-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)Cell<.+>$" --category Rust
48-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)Cell<.+>$" --category Rust
54+
type synthetic add -l lldb_lookup.StdCellSyntheticProvider -x "^(core::([a-z_]+::)+)Cell<.+>$" --category Rust
55+
4956
# RefCell
50-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)Ref<.+>$" --category Rust
51-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)RefMut<.+>$" --category Rust
52-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)RefCell<.+>$" --category Rust
53-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)Ref<.+>$" --category Rust
54-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)RefMut<.+>$" --category Rust
55-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)RefCell<.+>$" --category Rust
57+
type synthetic add -l lldb_lookup.StdRefSyntheticProvider -x "^(core::([a-z_]+::)+)Ref(Cell|Mut)?<.+>$" --category Rust
58+
type summary add -F lldb_lookup.StdRefSummaryProvider -e -x -h "^(core::([a-z_]+::)+)Ref(Cell|Mut)?<.+>$" --category Rust
59+
5660
# NonZero
57-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)NonZero<.+>$" --category Rust
58-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^core::num::([a-z_]+::)*NonZero.+$" --category Rust
59-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)NonZero<.+>$" --category Rust
60-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^core::num::([a-z_]+::)*NonZero.+$" --category Rust
61+
type summary add -F lldb_lookup.StdNonZeroNumberSummaryProvider -e -x -h "^(core::([a-z_]+::)+)NonZero(<.+>|I\d{0,3}|U\d{0,3})$" --category Rust
62+
6163
# PathBuf
62-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::([a-z_]+::)+)PathBuf$" --category Rust
63-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::([a-z_]+::)+)PathBuf$" --category Rust
64+
type summary add -F lldb_lookup.StdPathBufSummaryProvider -e -x -h "^(std::([a-z_]+::)+)PathBuf$" --category Rust
65+
6466
# Path
65-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?(std::([a-z_]+::)+)Path$" --category Rust
66-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?(std::([a-z_]+::)+)Path$" --category Rust
67+
type summary add -F lldb_lookup.StdPathSummaryProvider -e -x -h "^&(mut )?(std::([a-z_]+::)+)Path$" --category Rust
68+
6769
# Enum
6870
# type summary add -F lldb_lookup.ClangEncodedEnumSummaryProvider -e -h "lldb_lookup.is_sum_type_enum" --recognizer-function --category Rust
6971
## MSVC
7072
type synthetic add -l lldb_lookup.MSVCEnumSyntheticProvider -x "^enum2\$<.+>$" --category Rust
7173
type summary add -F lldb_lookup.MSVCEnumSummaryProvider -e -x -h "^enum2\$<.+>$" --category Rust
74+
7275
## MSVC Variants
7376
type synthetic add -l lldb_lookup.synthetic_lookup -x "^enum2\$<.+>::.*$" --category Rust
74-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^enum2\$<.+>::.*$" --category Rust
77+
7578
# Tuple
76-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^\(.*\)$" --category Rust
77-
type summary add -F lldb_lookup.TupleSummaryProvider -e -x -h "^\(.*\)$" --category Rust
79+
type synthetic add -l lldb_lookup.TupleSyntheticProvider -x "^\(.*\)$" --category Rust
80+
7881
## MSVC
7982
type synthetic add -l lldb_lookup.MSVCTupleSyntheticProvider -x "^tuple\$<.+>$" --category Rust
8083
type summary add -F lldb_lookup.TupleSummaryProvider -e -x -h "^tuple\$<.+>$" --category Rust
84+
8185
type category enable Rust

0 commit comments

Comments
 (0)