Skip to content

Commit

Permalink
Perf: Avoid unnecessary disk I/O in vfs.dofile
Browse files Browse the repository at this point in the history
The difference only matters when including large files in the archive, but it's an easy win.
  • Loading branch information
rdw-software committed Aug 21, 2024
1 parent 9a32d21 commit c214c0b
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions Runtime/Libraries/vfs.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
local ffi = require("ffi")
local miniz = require("miniz")
local uv = require("uv")
local validation = require("validation")

local ffi_cast = ffi.cast
local ffi_sizeof = ffi.sizeof
local ffi_string = ffi.string

local assert = assert
local loadstring = loadstring
local tonumber = tonumber

Expand Down Expand Up @@ -60,21 +58,16 @@ function vfs.decode(fileContents)
local archiveEndOffset = archiveStartOffset + zipApp.signature.archiveSize - 1
zipApp.archive = fileContents:sub(archiveStartOffset, archiveEndOffset)

zipApp.reader = miniz.new_reader_memory(zipApp.archive)

return zipApp
end

function vfs.dofile(zipApp, filePath)
validation.validateTable(zipApp, "zipApp")
validation.validateString(filePath, "filePath")

local tempFile, tempFilePath = uv.fs_mkstemp("LUAZIP-XXXXXX")
C_FileSystem.WriteFile(tempFilePath, zipApp.archive)

local reader = miniz.new_reader(tempFilePath)

assert(uv.fs_close(tempFile))
assert(uv.fs_unlink(tempFilePath))

local reader = zipApp.reader
for index = 1, reader:get_num_files() do
if reader:get_filename(index) == filePath then
local fileContents = reader:extract(index)
Expand Down

0 comments on commit c214c0b

Please sign in to comment.