Reserve disk space before writing a file for efficiency

前端 未结 6 698
情书的邮戳
情书的邮戳 2020-12-14 19:42

I have noticed a huge performance hit in one of my projects when logging is enabled for the first time. But when the log file limit is reached and the program starts writing

相关标签:
6条回答
  • 2020-12-14 19:47

    If you're using C#, you can call SetLength on a FileStream to instantly set an initial size to the file.

    e.g.

    using (var fileStream = File.Open(@"file.txt", FileMode.Create, FileAccess.Write, FileShare.Read))
    {
        fileStream.SetLength(1024 * 1024 * 1024); // Reserve 1 GB
    }
    
    0 讨论(0)
  • 2020-12-14 19:52

    Here's a simple function that will work for files of any size:

    void SetFileSize(HANDLE hFile, LARGE_INTEGER size)
    {
        SetFilePointer(hFile, size, NULL, FILE_BEGIN);
        SetEndOfFile(hFile);
    }
    
    0 讨论(0)
  • 2020-12-14 19:56
    void ReserveSpace(LONG spaceLow, LONG spaceHigh, HANDLE hFile)
    {
        DWORD err = ::SetFilePointer(hFile, spaceLow, &spaceHigh, FILE_BEGIN);
    
        if (err == INVALID_SET_FILE_POINTER) {
            err = GetLastError();
            // handle error
        }
        if (!::SetEndOfFile(hFile)) {
            err = GetLastError();
            // handle error
        }
        err = ::SetFilePointer(hFile, 0, 0, FILE_BEGIN); // reset
    }
    
    0 讨论(0)
  • 2020-12-14 20:01

    If you are using C++ 17, you should do it with std::filesystem::resize_file

    Link

    Changes the size of the regular file named by p as if by POSIX truncate: if the file size was previously larger than new_size, the remainder of the file is discarded. If the file was previously smaller than new_size, the file size is increased and the new area appears as if zero-filled.

    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <filesystem>
    
    namespace fs = std::filesystem;
    
    int main()
    {
        fs::path p = fs::current_path() / "example.bin"; 
        std::ofstream(p).put('a');
        fs::resize_file(p, 1024*1024*1024); // resize to 1 G
    }
    
    0 讨论(0)
  • 2020-12-14 20:03

    wRAR is correct. Open a new file using your favourite library, then seek to the penultimate byte and write a 0 there. That should allocate all the required disk space.

    0 讨论(0)
  • 2020-12-14 20:04

    You can use the SetFileValidData function to extend the logical length of a file without having to write out all that data to disk. However, because it can allow to read disk data to which you may not otherwise have been privileged, it requires the SE_MANAGE_VOLUME_NAME privilege to use. Carefully read the Remarks section of the documentation.

    Also implementation of SetFileValidData depend on fs driver. NTFS support it and FAT only since Win7.

    0 讨论(0)
提交回复
热议问题