summary refs log tree commit diff stats
path: root/lib/std/private/osfiles.nim
diff options
context:
space:
mode:
authorJuan Carlos <juancarlospaco@gmail.com>2023-10-01 02:19:37 -0300
committerGitHub <noreply@github.com>2023-10-01 07:19:37 +0200
commitb60f15e0dcfd514521bfc2e33c6f2728f565bc9d (patch)
tree0de0780b0bb35c174f618a9fe99a3d6e85e13d92 /lib/std/private/osfiles.nim
parentc3b95cbd2c944512585b843df7f0920894cf16ae (diff)
downloadNim-b60f15e0dcfd514521bfc2e33c6f2728f565bc9d.tar.gz
copyFile with POSIX_FADV_SEQUENTIAL (#22776)
- Continuation of https://github.com/nim-lang/Nim/pull/22769
- See
https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_fadvise.html
- The code was already there in `std/posix` since years ago. 3 line
diff.

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Diffstat (limited to 'lib/std/private/osfiles.nim')
-rw-r--r--lib/std/private/osfiles.nim8
1 files changed, 8 insertions, 0 deletions
diff --git a/lib/std/private/osfiles.nim b/lib/std/private/osfiles.nim
index b7957fa4f..948df4211 100644
--- a/lib/std/private/osfiles.nim
+++ b/lib/std/private/osfiles.nim
@@ -203,6 +203,7 @@ proc copyFile*(source, dest: string, options = {cfSymlinkFollow}; bufferSize = 1
   ## `-d:nimLegacyCopyFile` is used.
   ##
   ## `copyFile` allows to specify `bufferSize` to improve I/O performance.
+  ##
   ## See also:
   ## * `CopyFlag enum`_
   ## * `copyDir proc`_
@@ -243,6 +244,13 @@ proc copyFile*(source, dest: string, options = {cfSymlinkFollow}; bufferSize = 1
         if not open(d, dest, fmWrite):
           close(s)
           raiseOSError(osLastError(), dest)
+
+        # Hints for kernel-level aggressive sequential low-fragmentation read-aheads:
+        # https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_fadvise.html
+        when defined(linux) or defined(osx):
+          discard posix_fadvise(getFileHandle(d), 0.cint, 0.cint, POSIX_FADV_SEQUENTIAL)
+          discard posix_fadvise(getFileHandle(s), 0.cint, 0.cint, POSIX_FADV_SEQUENTIAL)
+
         var buf = alloc(bufferSize)
         while true:
           var bytesread = readBuffer(s, buf, bufferSize)