Skip to content

Commit 3d00472

Browse files
committed
Auto merge of #147552 - Walnut356:cleanup, r=Mark-Simulacrum,jieyouxu
[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 733108b + 2e8e618 commit 3d00472

File tree

5 files changed

+265
-227
lines changed

5 files changed

+265
-227
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: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +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
3-
type summary add -F _ -e -x -h "^.*$" --category Rust
6+
47
# Std String
58
type synthetic add -l lldb_lookup.StdStringSyntheticProvider -x "^(alloc::([a-z_]+::)+)String$" --category Rust
6-
type summary add -F lldb_lookup.StdStringSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)String$" --category Rust
9+
type summary add -F lldb_lookup.StdStringSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)String$" --category Rust
10+
711
# Std str
8-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?str$" --category Rust
9-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?str$" --category Rust
12+
type synthetic add -l lldb_lookup.StdSliceSyntheticProvider -x "^&(mut )?str$" --category Rust
13+
type summary add -F lldb_lookup.StdStrSummaryProvider -e -x -h "^&(mut )?str$" --category Rust
14+
1015
## MSVC
1116
type synthetic add -l lldb_lookup.MSVCStrSyntheticProvider -x "^ref(_mut)?\$<str\$>$" --category Rust
1217
type summary add -F lldb_lookup.StdStrSummaryProvider -e -h -x "^ref(_mut)?\$<str\$>$" --category Rust
13-
# Array
14-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?\\[.+\\]$" --category Rust
15-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?\\[.+\\]$" --category Rust
16-
# Slice
18+
19+
# Array/Slice
20+
type synthetic add -l lldb_lookup.StdSliceSyntheticProvider -x "^&(mut )?\\[.+\\]$" --category Rust
21+
type summary add -F lldb_lookup.SizeSummaryProvider -e -x -h "^&(mut )?\\[.+\\]$" --category Rust
22+
1723
## MSVC
1824
type synthetic add -l lldb_lookup.MSVCStdSliceSyntheticProvider -x "^ref(_mut)?\$<slice2\$<.+> >" --category Rust
1925
type summary add -F lldb_lookup.StdSliceSummaryProvider -e -x -h "^ref(_mut)?\$<slice2\$<.+> >" --category Rust
26+
2027
# OsString
21-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::ffi::([a-z_]+::)+)OsString$" --category Rust
22-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::ffi::([a-z_]+::)+)OsString$" --category Rust
28+
type summary add -F lldb_lookup.StdOsStringSummaryProvider -e -x -h "^(std::ffi::([a-z_]+::)+)OsString$" --category Rust
29+
2330
# Vec
24-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)Vec<.+>$" --category Rust
25-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Vec<.+>$" --category Rust
31+
type synthetic add -l lldb_lookup.StdVecSyntheticProvider -x "^(alloc::([a-z_]+::)+)Vec<.+>$" --category Rust
32+
type summary add -F lldb_lookup.SizeSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)Vec<.+>$" --category Rust
33+
2634
# VecDeque
27-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)VecDeque<.+>$" --category Rust
28-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)VecDeque<.+>$" --category Rust
29-
# BTreeSet
30-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)BTreeSet<.+>$" --category Rust
31-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)BTreeSet<.+>$" --category Rust
32-
# BTreeMap
33-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)BTreeMap<.+>$" --category Rust
34-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)BTreeMap<.+>$" --category Rust
35+
type synthetic add -l lldb_lookup.StdVecDequeSyntheticProvider -x "^(alloc::([a-z_]+::)+)VecDeque<.+>$" --category Rust
36+
type summary add -F lldb_lookup.SizeSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)VecDeque<.+>$" --category Rust
37+
3538
# HashMap
36-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::collections::([a-z_]+::)+)HashMap<.+>$" --category Rust
37-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::collections::([a-z_]+::)+)HashMap<.+>$" --category Rust
39+
type synthetic add -l lldb_lookup.classify_hashmap -x "^(std::collections::([a-z_]+::)+)HashMap<.+>$" --category Rust
40+
type summary add -F lldb_lookup.SizeSummaryProvider -e -x -h "^(std::collections::([a-z_]+::)+)HashMap<.+>$" --category Rust
41+
3842
# HashSet
39-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::collections::([a-z_]+::)+)HashSet<.+>$" --category Rust
40-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::collections::([a-z_]+::)+)HashSet<.+>$" --category Rust
43+
type synthetic add -l lldb_lookup.classify_hashset -x "^(std::collections::([a-z_]+::)+)HashSet<.+>$" --category Rust
44+
type summary add -F lldb_lookup.SizeSummaryProvider -e -x -h "^(std::collections::([a-z_]+::)+)HashSet<.+>$" --category Rust
45+
4146
# Rc
42-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)Rc<.+>$" --category Rust
43-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Rc<.+>$" --category Rust
47+
type synthetic add -l lldb_lookup.StdRcSyntheticProvider -x "^(alloc::([a-z_]+::)+)Rc<.+>$" --category Rust
48+
type summary add -F lldb_lookup.StdRcSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)Rc<.+>$" --category Rust
49+
4450
# Arc
45-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(alloc::([a-z_]+::)+)Arc<.+>$" --category Rust
46-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)Arc<.+>$" --category Rust
51+
type synthetic add -l lldb_lookup.arc_synthetic -x "^(alloc::([a-z_]+::)+)Arc<.+>$" --category Rust
52+
type summary add -F lldb_lookup.StdRcSummaryProvider -e -x -h "^(alloc::([a-z_]+::)+)Arc<.+>$" --category Rust
53+
4754
# Cell
48-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)Cell<.+>$" --category Rust
49-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)Cell<.+>$" --category Rust
55+
type synthetic add -l lldb_lookup.StdCellSyntheticProvider -x "^(core::([a-z_]+::)+)Cell<.+>$" --category Rust
56+
5057
# RefCell
51-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)Ref<.+>$" --category Rust
52-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)RefMut<.+>$" --category Rust
53-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)RefCell<.+>$" --category Rust
54-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)Ref<.+>$" --category Rust
55-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)RefMut<.+>$" --category Rust
56-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)RefCell<.+>$" --category Rust
58+
type synthetic add -l lldb_lookup.StdRefSyntheticProvider -x "^(core::([a-z_]+::)+)Ref(Cell|Mut)?<.+>$" --category Rust
59+
type summary add -F lldb_lookup.StdRefSummaryProvider -e -x -h "^(core::([a-z_]+::)+)Ref(Cell|Mut)?<.+>$" --category Rust
60+
5761
# NonZero
58-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)NonZero<.+>$" --category Rust
59-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^core::num::([a-z_]+::)*NonZero.+$" --category Rust
60-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)NonZero<.+>$" --category Rust
61-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^core::num::([a-z_]+::)*NonZero.+$" --category Rust
62+
type summary add -F lldb_lookup.StdNonZeroNumberSummaryProvider -e -x -h "^(core::([a-z_]+::)+)NonZero(<.+>|I\d{0,3}|U\d{0,3})$" --category Rust
63+
6264
# PathBuf
63-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::([a-z_]+::)+)PathBuf$" --category Rust
64-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::([a-z_]+::)+)PathBuf$" --category Rust
65+
type summary add -F lldb_lookup.StdPathBufSummaryProvider -e -x -h "^(std::([a-z_]+::)+)PathBuf$" --category Rust
66+
6567
# Path
66-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?(std::([a-z_]+::)+)Path$" --category Rust
67-
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?(std::([a-z_]+::)+)Path$" --category Rust
68+
type summary add -F lldb_lookup.StdPathSummaryProvider -e -x -h "^&(mut )?(std::([a-z_]+::)+)Path$" --category Rust
69+
6870
# Enum
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
79+
type synthetic add -l lldb_lookup.TupleSyntheticProvider -x "^\(.*\)$" --category Rust
80+
7781
## MSVC
7882
type synthetic add -l lldb_lookup.MSVCTupleSyntheticProvider -x "^tuple\$<.+>$" --category Rust
7983
type summary add -F lldb_lookup.TupleSummaryProvider -e -x -h "^tuple\$<.+>$" --category Rust
84+
8085
type category enable Rust

0 commit comments

Comments
 (0)