Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing unnecessary copy on serialization #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rwkarg
Copy link

@rwkarg rwkarg commented Jul 18, 2017

SyslogMessage.StructuredDataElement is accepted as an array but was being stored and surfaced through the get property as an IEnumerable<>. This required a .ToList() on every serialization request as well as a .Any().

Changing the backing field and get property to an array removes the copy from the .ToList() and allows a .Length != 0 instead of the Linq .Any().

It is mostly a non-breaking change as any usage of the SyslogMessage.StructuredDataElement assuming an IEnumerable<> will work against the new array type. Inspection of the actual type returned will continue to be an array as before. The only potential for a breaking change would be code that depends on the defined type of the property (not the type of what's returned) being an IEnumerable<> but that seems like an odd requirement for code to depend on and is easily updated by changing the expected type.

Copy link
Collaborator

@grubman grubman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I Think that the upside of using "Length" over "Any()" is insignificant compare to the downside of changing the property type from IEnumerable to an Array.

(If we were using .net 4.5 I would've recommend using IReadOnlyCollection instead of IEnumerable)

Please change the PR so you only delete the unnecessary ToList.

@@ -33,8 +32,8 @@ public void Serialize(SyslogMessage message, Stream stream)

writeStream(stream, Encoding.ASCII, messageBuilder.ToString());

var structuredData = message.StructuredDataElements?.ToList();
if (structuredData != null && structuredData.Any())
var structuredData = message.StructuredDataElements;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ToList is indeed not necessary in this case, it should be removed to avoid a potential costly materialization.

@@ -128,7 +127,7 @@ public string Message
get { return message; }
}

public IEnumerable<StructuredDataElement> StructuredDataElements
public StructuredDataElement[] StructuredDataElements
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary change, you should leave it as IEnumerable

@@ -12,7 +11,7 @@ public class SyslogMessage
private readonly string procId;
private readonly string msgId;
private readonly string message;
private readonly IEnumerable<StructuredDataElement> structuredDataElements;
private readonly StructuredDataElement[] structuredDataElements;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary change, you should leave it as IEnumerable

var structuredData = message.StructuredDataElements?.ToList();
if (structuredData != null && structuredData.Any())
var structuredData = message.StructuredDataElements;
if (structuredData != null && structuredData.Length != 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary change, you should leave it as Any()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants