Skip to content

ComparisonConstraints are allocating string on construction #2975

Closed
@lahma

Description

@lahma

While investigating our test suite's memory usage, NUnit also came up from some paths where a lot of parsed data was processed through loop that checked against LessThanOrEqualConstraint. If you look how all ComparisonConstraint inheritors are implemented, they all allocate Description when constructed even though I would presume it's not needed unless assertion fails?

public GreaterThanOrEqualConstraint(object expected) : base(expected)
{
Description = "greater than or equal to " + MsgUtils.FormatValue(expected);
}

I would suggest that these would be either lazy evaluated or evaluated every time the getter is called, like for example in EqualConstraint:

public override string Description
{
get
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(MsgUtils.FormatValue(_expected));
if (_tolerance != null && !_tolerance.IsUnsetOrDefault)
{
sb.Append(" +/- ");
sb.Append(MsgUtils.FormatValue(_tolerance.Amount));
if (_tolerance.Mode != ToleranceMode.Linear)
{
sb.Append(" ");
sb.Append(_tolerance.Mode.ToString());
}
}
if (_comparer.IgnoreCase)
sb.Append(", ignoring case");
return sb.ToString();
}
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions