avoid zeroMem (#207)

on cached data, it doubles read time (!)
This commit is contained in:
Jacek Sieka 2023-07-21 08:01:07 +02:00 committed by GitHub
parent 7afe7e3c07
commit 02f0929fc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 6 deletions

View File

@ -1329,7 +1329,7 @@ proc setFilePos*(handle: IoHandle, offset: int64,
else:
ok()
proc checkFileSize*(value: int64): IoResult[void] =
proc checkFileSize*(value: int64): IoResult[int] =
## Checks if ``value`` fits into supported by Nim string/sequence indexing
## mechanism.
##
@ -1339,9 +1339,9 @@ proc checkFileSize*(value: int64): IoResult[void] =
if value > 0x7FFF_FFFF'i64:
err(UnsupportedFileSize)
else:
ok()
ok(int(value))
elif sizeof(int) == 8:
ok()
ok(int(value))
proc readFile*[T: byte|char](pathName: string,
data: var openArray[T]): IoResult[uint] =
@ -1365,9 +1365,16 @@ proc readFile*[T: byte|char](pathName: string,
proc readFile*[T: seq[byte]|string](pathName: string,
data: var T): IoResult[void] =
## Read all data from file ``pathName`` and store it to ``data``.
let fileSize = ? getFileSize(pathName)
? checkFileSize(fileSize)
data.setLen(fileSize)
let
fileSize = ? getFileSize(pathName)
memSize = ? checkFileSize(fileSize)
if data.len() != memSize:
# `zeroMem` creates a measurable performance degradation here
when data is seq[byte]:
data = newSeqUninitialized[byte](memSize)
else:
data = newString(memSize)
let res {.used.} = ? readFile(pathName, data.toOpenArray(0, len(data) - 1))
ok()