You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
for loop run 500k counts+, cost about 250ms on my pc(cpu i7 9700)
output: 250 ms cost, loop count 524288 11-30 11:37:46,info,archivehlpr.go.155:251 ms cost open apps.json
The text was updated successfully, but these errors were encountered:
funcextractFile(file*sevenzip.File, targetstring) error {
_f, err:=os.OpenFile(target, os.O_CREATE, file.Mode())
iferr!=nil {
returnerr
}
defer_f.Close()
fReader, err:=file.Open()
iferr!=nil {
returnerr
}
deferfReader.Close()
_, err=io.Copy(_f, fReader)
returnerr
}
funcextract7zArchive(archivestring, pathstring, passwordstring) error {
reader, err:=sevenz.OpenReaderWithPassword(archive, password)
iferr!=nil {
returnerr
}
deferreader.Close()
for_, f:=rangereader.File {
target:=PathAppend(path, f.Name)
// seems no flag to check if f is directory, but it will end with / if is directoryifIsStrEndWith(f.Name, "/", true) {
iferr:=os.MkdirAll(target, f.Mode()); err!=nil {
returnerr
}
} else {
pre:=time.Now().UnixMilli()
iferr=extractFile(f, target); err!=nil {
returnerr
}
LogInfo("%d ms cost open %s", time.Now().UnixMilli()-pre, f.Name)
}
}
returnnil
}
I've not compile-tested it but you should be able to get the idea.
The point is to close the io.ReadCloser as soon as you finished extracting a file before extracting the next one. The way your code is currently structured means that all of the io.ReadClosers are deferred until extract7zArchive() completes before closing all of them.
Internally, to read file n in an archive, you have to start at the beginning of the stream and read (and discard) the data for files 0 to n-1, so for increasing values of n it will get slower and slower as you have to read and discard more and more.
There's an optimisation in my library that will reuse the stream reader but that only works if you call Close() on the file reader before opening the next one.
You're also doing the same with your filehandles returned from os.OpenFile() so you can potentially run out of filehandles available to your process when dealing with very large archives.
I extract a file with password, my code likes:
it very slow on f.Open, I follow the code and found performance problem at internal/aes7z/key.go
for loop run 500k counts+, cost about 250ms on my pc(cpu i7 9700)
output:
250 ms cost, loop count 524288 11-30 11:37:46,info,archivehlpr.go.155:251 ms cost open apps.json
The text was updated successfully, but these errors were encountered: