Skip to content

Commit 9868671

Browse files
committed
Use builder pattern to avoid re-sorting and re-copying of bazel thread data
Signed-off-by: Felipe <afrueda97@outlook.com>
1 parent ae84789 commit 9868671

3 files changed

Lines changed: 324 additions & 296 deletions

File tree

analyzer/java/com/engflow/bazel/invocation/analyzer/bazelprofile/BazelProfile.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.Map;
4343
import java.util.Optional;
4444
import java.util.concurrent.atomic.AtomicLong;
45+
import java.util.stream.Collectors;
4546
import java.util.stream.Stream;
4647
import java.util.zip.GZIPInputStream;
4748
import java.util.zip.ZipException;
@@ -89,10 +90,11 @@ public static BazelProfile of(Reader reader) {
8990

9091
private final BazelVersion bazelVersion;
9192
private final Map<String, String> otherData = new HashMap<>();
92-
private final Map<ThreadId, ProfileThread> threads = new HashMap<>();
93+
private final Map<ThreadId, ProfileThread> threads;
9394

9495
private BazelProfile(JsonReader profileReader) {
9596
try {
97+
Map<ThreadId, ProfileThread.Builder> threadBuilders = new HashMap<>();
9698
boolean hasOtherData = false;
9799
boolean hasTraceEvents = false;
98100
profileReader.beginObject();
@@ -121,17 +123,17 @@ private BazelProfile(JsonReader profileReader) {
121123
continue;
122124
}
123125
ThreadId threadId = new ThreadId(pid, tid);
124-
ProfileThread profileThread =
125-
threads.compute(
126+
ProfileThread.Builder profileThreadBuilder =
127+
threadBuilders.compute(
126128
threadId,
127129
(key, t) -> {
128130
if (t == null) {
129-
t = new ProfileThread(threadId);
131+
t = new ProfileThread.Builder().setThreadId(key);
130132
}
131133
return t;
132134
});
133135
// TODO: Use success response to take action on errant events.
134-
profileThread.addEvent(traceEvent);
136+
profileThreadBuilder.addEvent(traceEvent);
135137
}
136138
profileReader.endArray();
137139
break;
@@ -148,6 +150,9 @@ private BazelProfile(JsonReader profileReader) {
148150
TraceEventFormatConstants.SECTION_OTHER_DATA,
149151
TraceEventFormatConstants.SECTION_TRACE_EVENTS));
150152
}
153+
threads =
154+
threadBuilders.entrySet().stream()
155+
.collect(Collectors.toConcurrentMap(Map.Entry::getKey, e -> e.getValue().build()));
151156
} catch (IllegalStateException | IOException e) {
152157
throw new IllegalArgumentException("Could not parse Bazel profile.", e);
153158
}

0 commit comments

Comments
 (0)