4242import java .util .Map ;
4343import java .util .Optional ;
4444import java .util .concurrent .atomic .AtomicLong ;
45+ import java .util .stream .Collectors ;
4546import java .util .stream .Stream ;
4647import java .util .zip .GZIPInputStream ;
4748import 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