Skip to content

Commit 2b407b5

Browse files
changed write file to accept 1MiB blocks to improve file write speed on windows
1 parent 79390f4 commit 2b407b5

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/os/core/win32/os_core_win32.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -366,27 +366,29 @@ os_file_write(OS_Handle file, Rng1U64 rng, void *data)
366366
HANDLE win_handle = (HANDLE)file.u64[0];
367367
U64 src_off = 0;
368368
U64 dst_off = rng.min;
369-
U64 bytes_to_write_total = rng.max-rng.min;
370-
U64 total_bytes_written = 0;
371-
for(;src_off < bytes_to_write_total;)
369+
U64 total_write_size = dim_1u64(rng);
370+
for(;;)
372371
{
373-
void *bytes_src = (void *)((U8 *)data + src_off);
374-
U64 bytes_to_write_64 = (bytes_to_write_total-src_off);
375-
U32 bytes_to_write_32 = u32_from_u64_saturate(bytes_to_write_64);
376-
U32 bytes_written = 0;
372+
void *bytes_src = (U8 *)data + src_off;
373+
U64 bytes_left = total_write_size - src_off;
374+
DWORD write_size = Min(MB(1), bytes_left);
375+
DWORD bytes_written = 0;
377376
OVERLAPPED overlapped = {0};
378-
overlapped.Offset = (dst_off&0x00000000ffffffffull);
377+
overlapped.Offset = (dst_off&0x00000000ffffffffull);
379378
overlapped.OffsetHigh = (dst_off&0xffffffff00000000ull) >> 32;
380-
BOOL success = WriteFile(win_handle, bytes_src, bytes_to_write_32, (DWORD *)&bytes_written, &overlapped);
379+
BOOL success = WriteFile(win_handle, bytes_src, write_size, &bytes_written, &overlapped);
381380
if(success == 0)
382381
{
383382
break;
384383
}
385384
src_off += bytes_written;
386385
dst_off += bytes_written;
387-
total_bytes_written += bytes_written;
386+
if(bytes_left == 0)
387+
{
388+
break;
389+
}
388390
}
389-
return total_bytes_written;
391+
return src_off;
390392
}
391393

392394
internal B32

0 commit comments

Comments
 (0)