✨ Implement simple base class for custom encoding implementations #181
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Add a simple abstract class to implement custom encoding implementations.
It only requires to provide the encode and decoding methods base on the performance-based
Span
and it provides help methods to report invalid chars / bytes.The byte/char count methods will re-use the encode and decode methods, as typically the implementation is very similar and the performance is not affected.
This class was already use in Metatron to implement the Persona encoding.
Also refactor the performance test app so we can run all the tests individually.
Performance comparison
The performance between a custom implementation on
Encoding
and on this class is practically the same. Thanks to the advance usage of Span, simplified in this class, the memory is 3x times better and consumes the same as the standard implementations of .NET.In this case we compare a custom Shift-JIS implementation with the .NET implementation. The performance is worse than the .NET API provided encoding as they do optimizations at low-level with binary dictionaries. Our implementation is a typically use-case with conditions following the specs.
The values are good for a maximum use case of 5 MB of text (0.5 sec), the memory is great and the implementation is very simple.
Example