Skip to content

Added tests and improved allocations for HtmlEncoder#313

Merged
rexm merged 4 commits intoHandlebars-Net:masterfrom
304NotModified:html-encode
Aug 6, 2019
Merged

Added tests and improved allocations for HtmlEncoder#313
rexm merged 4 commits intoHandlebars-Net:masterfrom
304NotModified:html-encode

Conversation

@304NotModified
Copy link
Copy Markdown
Contributor

As the HtmlEncoder is an important thing when rendering, I added some tests for the coverage and improved the allocations, no need to allocate a stringbuilder and new string if no encoding needs to be done :)

@304NotModified
Copy link
Copy Markdown
Contributor Author

[xUnit.net 00:00:01.7340644] Handlebars.Test.HtmlEncoderTests.EncodeTest(input: "�", expected: "ü") [FAIL]
985[xUnit.net 00:00:01.7409369] Assert.Equal() Failure
986[xUnit.net 00:00:01.7421150] ↓ (pos 2)
987[xUnit.net 00:00:01.7431782] Expected: ü
988[xUnit.net 00:00:01.7441620] Actual: �

That's strange, it works local :|

@rexm
Copy link
Copy Markdown
Member

rexm commented Aug 3, 2019

Thanks for the additional test. It's not clear to me how the allocation change is a net improvement.

@304NotModified
Copy link
Copy Markdown
Contributor Author

Allocations are taking memory and makes the GC busy.

FYI: We've done a lot of those improvements in NLog.

@304NotModified
Copy link
Copy Markdown
Contributor Author

That's strange, it works local :|

This is fixed with the UTF-8 BOM on the file

@rexm
Copy link
Copy Markdown
Member

rexm commented Aug 3, 2019

Yeah I get that generally speaking allocations take memory and make GC busy, but in this specific case it is not clear to me what the net improvement is.

@304NotModified
Copy link
Copy Markdown
Contributor Author

less memory usage and lower GC pressure for all renderings?

@rexm
Copy link
Copy Markdown
Member

rexm commented Aug 3, 2019

Great! How much?

@304NotModified
Copy link
Copy Markdown
Contributor Author

304NotModified commented Aug 3, 2019

That depends on the machine. Could try to measure (but that's more work than the fix+unit tests ;))

@rexm
Copy link
Copy Markdown
Member

rexm commented Aug 5, 2019

Can always leave the unit tests and remove the change. Generally we don't put in changes that claim to be performance improvements without instrumenting them.

@304NotModified
Copy link
Copy Markdown
Contributor Author

Generally we don't put in changes that claim to be performance improvements without instrumenting them.

That's clear ;)

Can always leave the unit tests and remove the change.

If I don't find time this week, then that's OK. I think it could do the performance instrumention with https://www.jetbrains.com/dotmemory/unit/

@304NotModified
Copy link
Copy Markdown
Contributor Author

304NotModified commented Aug 5, 2019

I tested this case:

        [Fact]
        public void LargeEncodeTest()
        {
            var input = new string('0', 1_000_000_000);
            // Arrange
            var htmlEncoder = new HtmlEncoder();


            // Act
            var result = htmlEncoder.Encode(input);

        }

before:

image

after:

image

all run 3 times (results where significant the same), release mode.

@304NotModified
Copy link
Copy Markdown
Contributor Author

304NotModified commented Aug 5, 2019

from the profiler:

before:

image

after:

image

@rexm rexm merged commit f49068e into Handlebars-Net:master Aug 6, 2019
@rexm
Copy link
Copy Markdown
Member

rexm commented Aug 6, 2019

Thanks! New release probably this weekend.

@304NotModified 304NotModified deleted the html-encode branch August 6, 2019 16:10
@304NotModified
Copy link
Copy Markdown
Contributor Author

Great!

Copy link
Copy Markdown

@roydukkey roydukkey left a comment

Choose a reason for hiding this comment

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

Please verify and document the addition of five (5) to the text length when creating the StringBuilder.


private static string ReallyEncode(string text, int i)
{
var sb = new StringBuilder(text.Length + 5);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@304NotModified Why are you adding 5 to the length? How does this help during allocation?

{
return ReallyEncode(text, i);
}
else
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@304NotModified Could this else be omitted? Or, at the very least be use braces {} to match the style of the rest of the project?

sb.Append(((int)text[i]).ToString(CultureInfo.InvariantCulture));
sb.Append(";");
}
else
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@304NotModified I guess I see the braces existed like this before your edits. So maybe this would be an appropriate time to fix?

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