Skip to content

Commit ac4c185

Browse files
authored
Merge pull request #343 from bgrainger/reduce-allocations
Defer string creation in ColumnDefinitionPayload.
2 parents 095ace1 + 76b0612 commit ac4c185

File tree

1 file changed

+89
-19
lines changed

1 file changed

+89
-19
lines changed
Lines changed: 89 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
using System;
1+
using System;
22
using System.Text;
33

44
namespace MySql.Data.Serialization
55
{
66
internal class ColumnDefinitionPayload
77
{
8-
public string Name { get; private set; }
8+
public string Name
9+
{
10+
get
11+
{
12+
if (!m_readNames)
13+
ReadNames();
14+
return m_name;
15+
}
16+
}
917

1018
public CharacterSet CharacterSet { get; private set; }
1119

@@ -15,27 +23,67 @@ internal class ColumnDefinitionPayload
1523

1624
public ColumnFlags ColumnFlags { get; private set; }
1725

18-
public string SchemaName { get; private set; }
26+
public string SchemaName
27+
{
28+
get
29+
{
30+
if (!m_readNames)
31+
ReadNames();
32+
return m_schemaName;
33+
}
34+
}
1935

20-
public string CatelogName { get; private set; }
36+
public string CatalogName
37+
{
38+
get
39+
{
40+
if (!m_readNames)
41+
ReadNames();
42+
return m_catalogName;
43+
}
44+
}
2145

22-
public string Table { get; private set; }
46+
public string Table
47+
{
48+
get
49+
{
50+
if (!m_readNames)
51+
ReadNames();
52+
return m_table;
53+
}
54+
}
2355

24-
public string PhysicalTable { get; private set; }
56+
public string PhysicalTable
57+
{
58+
get
59+
{
60+
if (!m_readNames)
61+
ReadNames();
62+
return m_physicalTable;
63+
}
64+
}
2565

26-
public string PhysicalName { get; private set; }
66+
public string PhysicalName
67+
{
68+
get
69+
{
70+
if (!m_readNames)
71+
ReadNames();
72+
return m_physicalName;
73+
}
74+
}
2775

2876
public byte Decimals { get; private set; }
2977

3078
public static ColumnDefinitionPayload Create(PayloadData payload)
3179
{
3280
var reader = new ByteArrayReader(payload.ArraySegment);
33-
var catalog = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
34-
var schema = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
35-
var table = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
36-
var physicalTable = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
37-
var name = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
38-
var physicalName = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
81+
SkipLengthEncodedByteString(ref reader); // catalog
82+
SkipLengthEncodedByteString(ref reader); // schema
83+
SkipLengthEncodedByteString(ref reader); // table
84+
SkipLengthEncodedByteString(ref reader); // physical table
85+
SkipLengthEncodedByteString(ref reader); // name
86+
SkipLengthEncodedByteString(ref reader); // physical name
3987
reader.ReadByte(0x0C); // length of fixed-length fields, always 0x0C
4088
var characterSet = (CharacterSet) reader.ReadUInt16();
4189
var columnLength = reader.ReadUInt32();
@@ -57,19 +105,41 @@ public static ColumnDefinitionPayload Create(PayloadData payload)
57105

58106
return new ColumnDefinitionPayload
59107
{
60-
Name = name,
108+
OriginalPayload = payload,
61109
CharacterSet = characterSet,
62110
ColumnLength = columnLength,
63111
ColumnType = columnType,
64112
ColumnFlags = columnFlags,
65-
SchemaName = schema,
66-
CatelogName = catalog,
67-
Table = table,
68-
PhysicalTable = physicalTable,
69-
PhysicalName = physicalName,
70113
Decimals = decimals
71114
};
115+
}
72116

117+
private static void SkipLengthEncodedByteString(ref ByteArrayReader reader)
118+
{
119+
var length = checked((int) reader.ReadLengthEncodedInteger());
120+
reader.Offset += length;
73121
}
122+
123+
private void ReadNames()
124+
{
125+
var reader = new ByteArrayReader(OriginalPayload.ArraySegment);
126+
m_catalogName = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
127+
m_schemaName = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
128+
m_table = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
129+
m_physicalTable = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
130+
m_name = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
131+
m_physicalName = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
132+
m_readNames = true;
133+
}
134+
135+
PayloadData OriginalPayload { get; set; }
136+
137+
bool m_readNames;
138+
string m_name;
139+
string m_schemaName;
140+
string m_catalogName;
141+
string m_table;
142+
string m_physicalTable;
143+
string m_physicalName;
74144
}
75145
}

0 commit comments

Comments
 (0)