Skip to content

Commit 09ab391

Browse files
committed
Only use Entry ID<->index maps if necessary
In the common case, Entry IDs and indices are both contiguous and start from 0, so these maps are just the identity function. ID->index conversion is on the hot path for drawing Timeline and the boxing/unboxing and map lookup can be relatively costly, so avoid it when possible.
1 parent 8ca8a8c commit 09ab391

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

src/projections/analysis/StsReader.java

+34-14
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public String toString() {
8080
/** index by Integer ID in STS file, return String name */
8181
public Map<Integer, Chare> entryChares = new TreeMap<Integer, Chare>();
8282
/** keys are indexes into flat arrays, values are the IDs given in STS file */
83-
private Map<Integer, Integer> entryFlatToID = new TreeMap<Integer, Integer>();
83+
private Map<Integer, Integer> entryFlatToID = null;
8484
/** keys are the IDs given in STS file, values are indexes into flat arrays */
85-
private Map<Integer, Integer> entryIDToFlat = new TreeMap<Integer, Integer>();
85+
private Map<Integer, Integer> entryIDToFlat = null;
8686

8787

8888

@@ -224,8 +224,19 @@ public StsReader(String FileName)
224224
ChareID = Integer.parseInt(st.nextToken());
225225
st.nextToken(); // msgid
226226

227-
entryFlatToID.put(entryIndex, ID);
228-
entryIDToFlat.put(ID,entryIndex);
227+
// In general, Entry IDs will be contiguous starting from 0, so avoid
228+
// creating the ID<->flat maps if possible. This code creates the map
229+
// when it detects a non-contiguous case.
230+
if (ID != entryIndex && entryFlatToID == null) {
231+
entryFlatToID = new TreeMap<>();
232+
for (int i = 0; i < entryIndex; i++)
233+
entryFlatToID.put(i, i);
234+
entryIDToFlat = new TreeMap<>(entryFlatToID);
235+
}
236+
if (entryFlatToID != null) {
237+
entryFlatToID.put(entryIndex, ID);
238+
entryIDToFlat.put(ID, entryIndex);
239+
}
229240
entryIndex++;
230241
getEntryNames().put(ID,Name);
231242
getEntryChare().put(ID, Chares[ChareID]);
@@ -348,8 +359,13 @@ public String getEntryNameByID(int ID) {
348359
}
349360

350361
public String getEntryNameByIndex(int index) {
351-
if(entryFlatToID.containsKey(index)){
352-
return getEntryNames().get(entryFlatToID.get(index));
362+
// Check if the ID is valid. If entryFlatToID exists, then check if
363+
// there's a valid mapping for index there, otherwise the index is the
364+
// ID, so check in the entry names map directly.
365+
final boolean isValid = (entryFlatToID != null && entryFlatToID.containsKey(index)) ||
366+
(entryFlatToID == null && getEntryNames().containsKey(index));
367+
if (isValid) {
368+
return getEntryNames().get(getEntryID(index));
353369
} else {
354370
return "Unknown";
355371
}
@@ -372,15 +388,15 @@ private String getEntryChareNameByID(int ID) {
372388
}
373389

374390
public String getEntryChareNameByIndex(int index) {
375-
return getEntryChare().get(entryFlatToID.get(index)).name;
391+
return getEntryChare().get(getEntryID(index)).name;
376392
}
377393

378394
public int getEntryChareDimensionsByID(int ID) {
379395
return getEntryChare().get(ID).dimensions;
380396
}
381397

382398
public int getEntryChareDimensionsByIndex(int index) {
383-
return getEntryChare().get(entryFlatToID.get(index)).dimensions;
399+
return getEntryChare().get(getEntryID(index)).dimensions;
384400
}
385401

386402
public String getEntryFullNameByID(int ID) {
@@ -391,12 +407,16 @@ public String getEntryFullNameByIndex(int index) {
391407
return getEntryChareNameByIndex(index) + "::" + getEntryNameByIndex(index);
392408
}
393409

394-
public Integer getEntryIndex(int ID) {
395-
if(ID<0)
396-
return ID;
397-
return entryIDToFlat.get(ID);
398-
}
399-
410+
public int getEntryIndex(int ID) {
411+
if(ID<0)
412+
return ID;
413+
return (entryIDToFlat == null) ? ID : entryIDToFlat.get(ID);
414+
}
415+
416+
private int getEntryID(int index) {
417+
return (entryFlatToID == null) ? index : entryFlatToID.get(index);
418+
}
419+
400420
// *** user event accessors ***
401421
public int getNumUserDefinedEvents() {
402422
return userEvents.size();

0 commit comments

Comments
 (0)