Data size and disk access
Is there a benefit to aligning data to a certain size on storage? For example, if I have the option to use one byte to store information or 4 bytes, which is preferred (assuming that storage size doesn't matter, only optimization)?
I ask this question mostly because I know that it "matters" if you're taking about in-memory values (and hence the reason why a .NET boolean is 4 bytes, for example, as per another question on this site).
I don't think it wou开发者_Go百科ld matter, but I am using the .NET framework (C# specifically).
If you need to be able to get to any particular record in a file, you'll either need some sort of index or a fixed record size - but that's for the whole record rather than each individual part of the record. I wouldn't usually go to any great lengths to align data on 4 or 8 byte boundaries within storage. Of course, if you read a record at a time, to an aligned location in memory, then you end up with aligned data to perform any conversions on... so it can all end up being intertwined to some extent - but the conversion is likely to be one-off, rather than frequent access after conversion.
Storage size matters for optimization of course - because reading less data from the disk will be cheaper than reading more (generally...).
Unless you have specific requirements like fixed record sizes, I would just try to design the storage so that it's as easy to use as possible. If you have specific areas of concern for performance, you should profile those. For example, it may be more efficient to use UTF-16 to encode strings than UTF-8, as the encoding and decoding should require less work... even though it'll take more space. You should test these rather than making any assumptions though. Note that where you're loading the storage format from will make a big difference - over the network, from a mechanical disk, from a solid state drive... these will have different performance characteristics, which probably make it hard to design something which is fastest for all cases.
精彩评论