Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid zeroMem #207

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions stew/io2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
##
Expand All @@ -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] =
Expand All @@ -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()

Expand Down