Skip to content

Commit 5dbda94

Browse files
committed
Pool ObjectID objects so only one instance for any given ID is created
Previously, every EMO had its own unique instance of ObjectID. Since ObjectID is effectively an immutable value class, one instance of a given ID can be safely shared in all places, so it now uses factory methods and a map to do so.
1 parent 6fe2709 commit 5dbda94

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

src/projections/Tools/Timeline/EntryMethodObject.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ protected EntryMethodObject(Data data, TimelineEvent tle,
140140
EventID = tle.EventID;
141141
msglen = tle.MsgLen;
142142
if (tle.id != null) {
143-
tid = new ObjectId(tle.id);
143+
tid = ObjectId.createObjectId(tle.id);
144144
} else {
145-
tid = new ObjectId();
145+
tid = ObjectId.createObjectId();
146146
}
147147

148148
if (tle.UserSpecifiedData != null || tle.memoryUsage > 0 ||

src/projections/analysis/ObjectId.java

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,60 @@
11
package projections.analysis;
22

3-
public class ObjectId implements Comparable
3+
import java.util.Arrays;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
6+
public final class ObjectId implements Comparable
47
{
58
private final static int ID_SIZE = 6;
6-
public int id[];
9+
public final int id[];
710

8-
public ObjectId() {
9-
id = new int[ID_SIZE];
10-
id[0] = id[1] = id[2] = id[3] = id[4] = id[5] = -1;
11-
}
12-
public ObjectId(ObjectId d) {
13-
if (d!=null) id = d.id.clone();
14-
else {
15-
id = new int[ID_SIZE];
16-
id[0] = id[1] = id[2] = id[3] = id[4] = id[5] = -1;
17-
}
18-
}
11+
private static ConcurrentHashMap<ObjectId, ObjectId> instances = new ConcurrentHashMap<>();
1912

20-
protected ObjectId(int[] data) {
13+
private ObjectId(final int[] data) {
2114
if (data.length > ID_SIZE)
2215
throw new IndexOutOfBoundsException("Attempted to assign " + data.length + "elements to ID of size " + ID_SIZE);
2316
id = new int[ID_SIZE];
24-
for (int i = 0; i < data.length; i++) {
17+
for (int i = 0; i < ID_SIZE; i++) {
2518
id[i] = data[i];
2619
}
2720
}
2821

22+
public static ObjectId createObjectId() {
23+
final int[] dummy = new int[ID_SIZE];
24+
dummy[0] = dummy[1] = dummy[2] = dummy[3] = dummy[4] = dummy[5] = -1;
25+
return createObjectId(dummy);
26+
}
27+
28+
public static ObjectId createObjectId(ObjectId d) {
29+
if (d == null)
30+
return createObjectId();
31+
else return d;
32+
}
33+
34+
protected static ObjectId createObjectId(final int[] data) {
35+
ObjectId candidate = new ObjectId(data);
36+
ObjectId canonical = instances.putIfAbsent(candidate, candidate);
37+
if (canonical == null)
38+
canonical = candidate;
39+
return canonical;
40+
}
41+
2942
public boolean equals(ObjectId comp) {
30-
System.out.println("EQUALS");
31-
return (id[0] == comp.id[0]) && (id[1] == comp.id[1]) && (id[2] == comp.id[2]) &&
32-
(id[3] == comp.id[3]) && (id[4] == comp.id[4]) && (id[5] == comp.id[5]);
43+
return Arrays.equals(id, comp.id);
3344
}
3445

3546
public boolean equals(Object o) {
3647
ObjectId comp = (ObjectId) o;
3748
return this.equals(comp);
3849
}
3950

51+
@Override
52+
public int hashCode() {
53+
return Arrays.hashCode(id);
54+
}
55+
4056
/** Needed for putting these as keys in a TreeSet */
57+
@Override
4158
@SuppressWarnings("ucd")
4259
public int compareTo(Object o) {
4360
ObjectId oid = (ObjectId)o;

src/projections/analysis/TimelineEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected TimelineEvent(long bt,long et, int ep,int pe, int mlen, long r,
6666
EntryPoint=ep; SrcPe=pe; MsgLen=mlen;
6767
RecvTime = r;
6868

69-
id = new ObjectId(d);
69+
id = ObjectId.createObjectId(d);
7070
EventID = eventid;
7171
this.numPapiCounts = numPapiCounts;
7272
this.papiCounts = papiCounts;
@@ -85,7 +85,7 @@ protected TimelineEvent(long bt,long et, int ep,int pe, int mlen, long r,
8585
EntryPoint=ep; SrcPe=pe; MsgLen=mlen;
8686
RecvTime = r;
8787

88-
id = new ObjectId(d);
88+
id = ObjectId.createObjectId(d);
8989
EventID = eventid;
9090
this.numPapiCounts = numPapiCounts;
9191
this.papiCounts = papiCounts;

0 commit comments

Comments
 (0)