- Frugal Cafe
- Posts
- FC21: Replacement one for string.Split
FC21: Replacement one for string.Split
DIY version can be much faster
string.Split is an easy way to split a line of text into parts. It’s commonly overused to solve lots of problems. Its implementation is not optimal, so it’s commonly seen to cause performance issues. There are improvements in .Net Core, but fundamentally, its design is not optimal. Let’s replace it it with a frugal version:
We’re adding a class to store the output of splitting, so that the data storage can be easily reused. The data storage is stored in OpenList of value tuples for start position and length of each segment after the splitting. The StringSplitter class provides indexer to get each segment as a Substring struct. When the StringSplitter class is reused properly, we do not even need a single heap allocation.
Here is the Substring struct:
More API can be added later.
Performance test:
Results:
You could implement the same on List, but OpenList gives derived classes more direct access to its internal array.