Skip to content

Commit

Permalink
Defer string creation.
Browse files Browse the repository at this point in the history
There's no need to deserialize the column & table names unless the schema is read.
  • Loading branch information
bgrainger committed Oct 1, 2017
1 parent 978129e commit 76b0612
Showing 1 changed file with 89 additions and 19 deletions.
108 changes: 89 additions & 19 deletions src/MySqlConnector/Serialization/ColumnDefinitionPayload.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
using System;
using System;
using System.Text;

namespace MySql.Data.Serialization
{
internal class ColumnDefinitionPayload
{
public string Name { get; private set; }
public string Name
{
get
{
if (!m_readNames)
ReadNames();
return m_name;
}
}

public CharacterSet CharacterSet { get; private set; }

Expand All @@ -15,27 +23,67 @@ internal class ColumnDefinitionPayload

public ColumnFlags ColumnFlags { get; private set; }

public string SchemaName { get; private set; }
public string SchemaName
{
get
{
if (!m_readNames)
ReadNames();
return m_schemaName;
}
}

public string CatelogName { get; private set; }
public string CatalogName
{
get
{
if (!m_readNames)
ReadNames();
return m_catalogName;
}
}

public string Table { get; private set; }
public string Table
{
get
{
if (!m_readNames)
ReadNames();
return m_table;
}
}

public string PhysicalTable { get; private set; }
public string PhysicalTable
{
get
{
if (!m_readNames)
ReadNames();
return m_physicalTable;
}
}

public string PhysicalName { get; private set; }
public string PhysicalName
{
get
{
if (!m_readNames)
ReadNames();
return m_physicalName;
}
}

public byte Decimals { get; private set; }

public static ColumnDefinitionPayload Create(PayloadData payload)
{
var reader = new ByteArrayReader(payload.ArraySegment);
var catalog = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
var schema = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
var table = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
var physicalTable = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
var name = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
var physicalName = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
SkipLengthEncodedByteString(ref reader); // catalog
SkipLengthEncodedByteString(ref reader); // schema
SkipLengthEncodedByteString(ref reader); // table
SkipLengthEncodedByteString(ref reader); // physical table
SkipLengthEncodedByteString(ref reader); // name
SkipLengthEncodedByteString(ref reader); // physical name
reader.ReadByte(0x0C); // length of fixed-length fields, always 0x0C
var characterSet = (CharacterSet) reader.ReadUInt16();
var columnLength = reader.ReadUInt32();
Expand All @@ -57,19 +105,41 @@ public static ColumnDefinitionPayload Create(PayloadData payload)

return new ColumnDefinitionPayload
{
Name = name,
OriginalPayload = payload,
CharacterSet = characterSet,
ColumnLength = columnLength,
ColumnType = columnType,
ColumnFlags = columnFlags,
SchemaName = schema,
CatelogName = catalog,
Table = table,
PhysicalTable = physicalTable,
PhysicalName = physicalName,
Decimals = decimals
};
}

private static void SkipLengthEncodedByteString(ref ByteArrayReader reader)
{
var length = checked((int) reader.ReadLengthEncodedInteger());
reader.Offset += length;
}

private void ReadNames()
{
var reader = new ByteArrayReader(OriginalPayload.ArraySegment);
m_catalogName = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
m_schemaName = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
m_table = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
m_physicalTable = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
m_name = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
m_physicalName = Encoding.UTF8.GetString(reader.ReadLengthEncodedByteString());
m_readNames = true;
}

PayloadData OriginalPayload { get; set; }

bool m_readNames;
string m_name;
string m_schemaName;
string m_catalogName;
string m_table;
string m_physicalTable;
string m_physicalName;
}
}

0 comments on commit 76b0612

Please sign in to comment.