From: Mauricio Fernandez Date: 2007-01-08T10:51:34+09:00 Subject: Re: SparseFile On Mon, Jan 08, 2007 at 09:44:33AM +0900, Erik Veenstra wrote: > I updated the implementation [1] of SparseFile [2] and added > some unit tests [3]. > > Any thoughts? Ideas? Comments? I welcome any feedback. Instead of SPARSE_BLOCKS[length] ||= "\000" * length unless data == SPARSE_BLOCKS[length] @file.pos = @pos @file.write(data) end What about unless data.count("\0") == data.size @file.pos = @pos @file.write(data) end ? Saves some mem if you have lots of "null blocks" of different sizes (or use huge blocks), and the loop shouldn't be much slower than a strcmpish one: while (s < send) { if (table[*s++ & 0xff]) { i++; } } But a C implementation that detects leading \0s and skips them would be best. In Ruby it'd be idx = data.index(/[^\0]/) if idx @file.pos += idx @file.write(data[idx..-1]) else @file.write(data) end but the regexp matching and the aref would make it way too slow. I'll post it tomorrow if I don't forget. -- Mauricio Fernandez - http://eigenclass.org - singular Ruby