7

I've got pretty unusual request: I would like to load all files from specific folder (so far easy). I need something with very small memory footprint.

Now it gets complicated (at least for me). I DON'T need to store or use the content of the files - I just need to force block-level caching mechanism to cache all the blocks that are used by that specific folder.

I know there are many different methods (BinaryReader, StreamReader etc.), but my case is quite special, since I don't care about the content...

Any idea what would be the best way how to achieve this?

Should I use small buffer? But since it would filled quickly, wouldn't flushing of the buffer actually slow down the operation?

Thanks, Martin

Martin Zugec
  • 165
  • 1
  • 7
  • What's your *final* use case? – ken2k Jun 25 '13 at 12:12
  • Hello ken2k, I would like to create small utility that can force Windows to store files in standby cache. Specifically designed for product called Provisioning Services (streaming of operating systems): http://blogs.citrix.com/2012/10/25/pvs-internals-1-cache-manager/ – Martin Zugec Jun 25 '13 at 16:02

2 Answers2

2

I would perhaps memory map the files and then loop around accessing an element of each file at regular (block-spaced) intervals.

Assuming of course that you are able to use .Net 4.0.

In psuedo code you'd do something like:

using ( var mmf = MemoryMappedFile.CreateFromFile( path ) )
{    
    for ( long offset = 0 ; offset < file.Size ; offset += block_size )
    {
        using ( var acc = accessor = mmf.CreateViewAccessor(offset, 1) )
        {
            acc.ReadByte(offset);
        }
    }
}

But at the end of the day, each method will have different performance characteristics so you might have to use a bit of trial and error to find out which is the most performant.

Nick
  • 25,026
  • 7
  • 51
  • 83
1

I would simply read those files. When you do that, CacheManager in NTFS caches these files automatically, and you don't have to care about anything else - that's exactly the role of CacheManager, and by reading these files, you give it a hint that these files should be cached.

Robert Goldwein
  • 5,805
  • 6
  • 33
  • 38