Aligning structures on disk

For some reason, I feel likes it necessary when allocating disk storage space inside CouchDb that it has to be aligned to a power-of-two boundary. It's seem likes that's necessary somehow, but I think it's just a carry-over from writing Compute and having to deal with memory alignment problems. I can't think of any reason disk storage should have alignment issues, I think even 1 Byte alignment is fine. Yet somehow it feels wrong.

Posted April 19, 2005 1:39 AM

Comments

Disk storage isn't immune from alignment issues. This technote is somewhat old and Mac-specific, but you should take home the gist of the issue:

http://developer.apple.com/technotes/fl/fl_16.html

Generally, you should care about keeping things 512-byte aligned. If you really care at all. Profile first.

rentzsch, April 19, 2005 2:28 AM

On Windows, you may want to map your buffers to the disk's sector size. Also, it used to be the case that DOS would work with a fixed-size "DTA" (disk transfer area), a memory buffer for moving bytes to and from disk. You got better performance by doing I/O operations that were aligned on the buffer size (e.g., 128 bytes). Don't know if that's still true in win32

Bob Balaban, April 19, 2005 5:39 AM

On windows NTFS, the standard sector size is 4k, so no disk read is possible without reading a full 4k. So if I were to align my structures to that, I'd waste tons of space. Plus, I'm pretty sure all major OS's cache disk reads by default anyway, which should already reduce actually disk IO considerably.

My big concern at this point is the writes and if corruptions are more likely when writng across sectors. My gut is there is no straight answer for these questions, it all depends on the exact OS and hardware configuration.

I'm going to check out the SQLite code (I hear it's easy to read), see what it does and if it has any rationalizations for it's workings.

Damien, April 19, 2005 11:37 AM

Post a comment




Remember Me?

(you may use HTML tags for style)