Skip to content

Commit b7b92c6

Browse files
author
Feng Yuan
committed
TraceCodeAddresses: avoid linear growth resizing, cache strings for real
1 parent da4f100 commit b7b92c6

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,4 @@ src/TraceEvent/EventCounterHandler.cs
204204
OSExtensions.cs
205205
password.txt
206206
documentation/internal/
207+
*.userosscache

src/TraceEvent/TraceLog.cs

+34-9
Original file line numberDiff line numberDiff line change
@@ -7571,10 +7571,16 @@ public enum CodeAddressIndex
75717571
/// </summary>
75727572
public sealed class TraceCodeAddresses : IFastSerializable, IEnumerable<TraceCodeAddress>
75737573
{
7574+
/// <summary>
7575+
/// Chunk size for <see cref="codeAddressObjects"/>
7576+
/// </summary>
7577+
private const int ChunkSize = 4096;
7578+
75747579
/// <summary>
75757580
/// Returns the count of code address indexes (all code address indexes are strictly less than this).
75767581
/// </summary>
75777582
public int Count { get { return codeAddresses.Count; } }
7583+
75787584
/// <summary>
75797585
/// Given a code address index, return the name associated with it (the method name). It will
75807586
/// have the form MODULE!METHODNAME. If the module name is unknown a ? is used, and if the
@@ -7587,7 +7593,8 @@ public string Name(CodeAddressIndex codeAddressIndex)
75877593
names = new string[Count];
75887594
}
75897595

7590-
string name = names[(int)codeAddressIndex];
7596+
string name = this.names[(int)codeAddressIndex];
7597+
75917598
if (name == null)
75927599
{
75937600
string moduleName = "?";
@@ -7608,8 +7615,9 @@ public string Name(CodeAddressIndex codeAddressIndex)
76087615
methodName = "0x" + ((ulong)Address(codeAddressIndex)).ToString("x");
76097616
}
76107617

7611-
name = moduleName + "!" + methodName;
7618+
this.names[(int)codeAddressIndex] = name = moduleName + "!" + methodName;
76127619
}
7620+
76137621
return name;
76147622
}
76157623
/// <summary>
@@ -7654,29 +7662,46 @@ public int ILOffset(CodeAddressIndex codeAddressIndex)
76547662

76557663
return ilMap.GetILOffsetForNativeAddress(Address(codeAddressIndex));
76567664
}
7665+
76577666
/// <summary>
76587667
/// Given a code address index, returns a TraceCodeAddress for it.
76597668
/// </summary>
76607669
public TraceCodeAddress this[CodeAddressIndex codeAddressIndex]
76617670
{
76627671
get
76637672
{
7664-
if (codeAddressObjects == null || (int)codeAddressIndex >= codeAddressObjects.Length)
7673+
if (codeAddressIndex == CodeAddressIndex.Invalid)
76657674
{
7666-
codeAddressObjects = new TraceCodeAddress[(int)codeAddressIndex + 16];
7675+
return null;
76677676
}
76687677

7669-
if (codeAddressIndex == CodeAddressIndex.Invalid)
7678+
int chunk = (int)codeAddressIndex / ChunkSize;
7679+
int offset = (int)codeAddressIndex % ChunkSize;
7680+
7681+
if (this.codeAddressObjects == null)
76707682
{
7671-
return null;
7683+
this.codeAddressObjects = new TraceCodeAddress[chunk + 1][];
7684+
}
7685+
else if (chunk >= this.codeAddressObjects.Length)
7686+
{
7687+
Array.Resize(ref this.codeAddressObjects, Math.Max(this.codeAddressObjects.Length * 2, chunk + 1));
7688+
}
7689+
7690+
TraceCodeAddress[] data = this.codeAddressObjects[chunk];
7691+
7692+
if (data == null)
7693+
{
7694+
data = this.codeAddressObjects[chunk] = new TraceCodeAddress[ChunkSize];
76727695
}
76737696

7674-
TraceCodeAddress ret = codeAddressObjects[(int)codeAddressIndex];
7697+
TraceCodeAddress ret = data[offset];
7698+
76757699
if (ret == null)
76767700
{
76777701
ret = new TraceCodeAddress(this, codeAddressIndex);
7678-
codeAddressObjects[(int)codeAddressIndex] = ret;
7702+
data[offset] = ret;
76797703
}
7704+
76807705
return ret;
76817706
}
76827707
}
@@ -8999,7 +9024,7 @@ void IFastSerializable.FromStream(Deserializer deserializer)
89999024
private GrowableArray<ILToNativeMap> ILToNativeMaps; // only Jitted code has these, indexed by ILMapIndex
90009025
private Dictionary<long, ILMapIndex> methodIDToILToNativeMap;
90019026

9002-
private TraceCodeAddress[] codeAddressObjects; // If we were asked for TraceCodeAddresses (instead of indexes) we cache them
9027+
private TraceCodeAddress[][] codeAddressObjects; // If we were asked for TraceCodeAddresses (instead of indexes) we cache them, in sparse array
90039028
private string[] names; // A cache (one per code address) of the string name of the address
90049029
private int managedMethodRecordCount; // Remembers how many code addresses are managed methods (currently not serialized)
90059030
internal int totalCodeAddresses; // Count of the number of times a code address appears in the log.

0 commit comments

Comments
 (0)