EDIT: I now have a blog post on this topic which goes into a fair amount more detail.
Going by your numbers:
I could have 100k record of each record having up to 10 string having 30-60 characters.
Let's start off by adding in the object overhead - a string takes up about 20 bytes (IIRC - possibly more on a 64-bit CLR) plus the actual data, due to the inevitable object overhead and the length. Let's do the maths again:
Using string: 1 million objects at 20+120 bytes = 140MB
Using a new class: 1 million objects at 20+60 bytes = 80MB
Still a 60MB difference of course, but proportionally less than you were expecting. You're only saving 42% of the space instead of 50%.
Now, you talk about things being faster: given that the CLR is natively aware of string
, I suspect a third-party class won't be able to match the speed for some of its operations, and you'd have to put a lot of work in to get many of the others to be the same speed. Admittedly you will have better cache coherency, and if you can ignore culture issues, that should save a bit of time too by making all comparisons ordinal.
For the sake of 60MB, I wouldn't bother. That's a tiny difference these days - consider how many more customers you'll have to gain through making this small saving in order to make up for the significant extra cost of working with two different string types.
Having said all of this, I'm quite tempted to implement it myself anyway as a blogging project like Edulinq. Don't expect any results for weeks or months though :)
EDIT: I've just thought of another problem. The numbers we've got above aren't actually right... because the string class is special. It embeds its data directly in the object - unlike any other data type apart from arrays, the size of a string
instance isn't fixed; it varies based on the data within it.
Writing your own AsciiString
class, you wouldn't be able to do that - you'd have to embed an array reference within the class:
public class AsciiString
{
private readonly byte[] data;
}
That means you'd need an extra 4 or 8 bytes for the reference (32 or 64-bit CLR) and the additional overhead of an array object (16 bytes, IIRC) per string.
If you designed it like Java, taking a substring could reuse the existing byte array (two strings could share), but then you'd need an extra length and offset within AsciiString
. You'd also lose some of the cache coherency benefits.
You could use just raw byte arrays as the data structure and write a bunch of extension methods to act on them... but that would be horrible, as then you couldn't tell the difference between a normal byte array and one which was meant to represent an ASCII string.
Another possibility would be to create a struct like this:
struct AsciiString
{
private readonly byte[] data;
...
}
That would effectively give you strong typing again, but you'd need to think about things like:
AsciiString x = new AsciiString();
which would end up with a null data
reference. You could effectively treat this as if x
were a null value, but it would be pretty non-idiomatic.