Skip to content

Commit ece63b8

Browse files
Fix perf issue on get locations of breakpoint (microsoft#88)
Signed-off-by: Jinbo Wang <jinbwan@microsoft.com>
1 parent ad02d8f commit ece63b8

File tree

1 file changed

+27
-12
lines changed
  • com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core

1 file changed

+27
-12
lines changed

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/Breakpoint.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ public CompletableFuture<IBreakpoint> install() {
133133
|| localClassPrepareRequest.equals(debugEvent.event.request())))
134134
.subscribe(debugEvent -> {
135135
ClassPrepareEvent event = (ClassPrepareEvent) debugEvent.event;
136-
List<BreakpointRequest> newRequests = createBreakpointRequests(event.referenceType(),
137-
lineNumber, hitCount);
136+
List<BreakpointRequest> newRequests = createBreakpointRequests(event.referenceType(), lineNumber,
137+
hitCount, false);
138138
requests.addAll(newRequests);
139139
if (!newRequests.isEmpty() && !future.isDone()) {
140140
this.putProperty("verified", true);
@@ -144,8 +144,9 @@ public CompletableFuture<IBreakpoint> install() {
144144
subscriptions.add(subscription);
145145

146146
List<ReferenceType> refTypes = vm.classesByName(className);
147-
List<BreakpointRequest> newRequests = createBreakpointRequests(refTypes, lineNumber, hitCount);
147+
List<BreakpointRequest> newRequests = createBreakpointRequests(refTypes, lineNumber, hitCount, true);
148148
requests.addAll(newRequests);
149+
149150
if (!newRequests.isEmpty() && !future.isDone()) {
150151
this.putProperty("verified", true);
151152
future.complete(this);
@@ -167,30 +168,44 @@ private static List<Location> collectLocations(ReferenceType refType, int lineNu
167168
return locations;
168169
}
169170

170-
private static List<Location> collectLocations(List<ReferenceType> refTypes, int lineNumber) {
171+
private static List<Location> collectLocations(List<ReferenceType> refTypes, int lineNumber, boolean includeNestedTypes) {
171172
List<Location> locations = new ArrayList<>();
172173
try {
173174
refTypes.forEach(refType -> {
174-
locations.addAll(collectLocations(refType, lineNumber));
175-
locations.addAll(collectLocations(refType.nestedTypes(), lineNumber));
175+
List<Location> newLocations = collectLocations(refType, lineNumber);
176+
if (!newLocations.isEmpty()) {
177+
locations.addAll(newLocations);
178+
} else if (includeNestedTypes) {
179+
// ReferenceType.nestedTypes() will invoke vm.allClasses() to list all loaded classes,
180+
// should avoid using nestedTypes for performance.
181+
for (ReferenceType nestedType : refType.nestedTypes()) {
182+
List<Location> nestedLocations = collectLocations(nestedType, lineNumber);
183+
if (!nestedLocations.isEmpty()) {
184+
locations.addAll(nestedLocations);
185+
break;
186+
}
187+
}
188+
}
176189
});
177190
} catch (VMDisconnectedException ex) {
178-
// collect locations operation may be executing while JVM is running, thus the VMDisconnectedException may be
191+
// collect locations operation may be executing while JVM is terminating, thus the VMDisconnectedException may be
179192
// possible, in case of VMDisconnectedException, this method will return an empty array which turns out a valid
180193
// response in vscode, causing no error log in trace.
181194
}
182195

183196
return locations;
184197
}
185198

186-
private List<BreakpointRequest> createBreakpointRequests(ReferenceType refType, int lineNumber, int hitCount) {
199+
private List<BreakpointRequest> createBreakpointRequests(ReferenceType refType, int lineNumber, int hitCount,
200+
boolean includeNestedTypes) {
187201
List<ReferenceType> refTypes = new ArrayList<>();
188202
refTypes.add(refType);
189-
return createBreakpointRequests(refTypes, lineNumber, hitCount);
203+
return createBreakpointRequests(refTypes, lineNumber, hitCount, includeNestedTypes);
190204
}
191205

192-
private List<BreakpointRequest> createBreakpointRequests(List<ReferenceType> refTypes, int lineNumber, int hitCount) {
193-
List<Location> locations = collectLocations(refTypes, lineNumber);
206+
private List<BreakpointRequest> createBreakpointRequests(List<ReferenceType> refTypes, int lineNumber,
207+
int hitCount, boolean includeNestedTypes) {
208+
List<Location> locations = collectLocations(refTypes, lineNumber, includeNestedTypes);
194209

195210
// find out the existing breakpoint locations
196211
List<Location> existingLocations = new ArrayList<>(requests.size());
@@ -217,7 +232,7 @@ private List<BreakpointRequest> createBreakpointRequests(List<ReferenceType> ref
217232
request.enable();
218233
newRequests.add(request);
219234
} catch (VMDisconnectedException ex) {
220-
// enable breakpoint operation may be executing while JVM is running, thus the VMDisconnectedException may be
235+
// enable breakpoint operation may be executing while JVM is terminating, thus the VMDisconnectedException may be
221236
// possible, in case of VMDisconnectedException, this method will return an empty array which turns out a valid
222237
// response in vscode, causing no error log in trace.
223238
}

0 commit comments

Comments
 (0)