Removed writeToWriteAt

Didn't seem to have any performance advantage
This commit is contained in:
Caleb Gardner
2025-03-04 04:08:13 -06:00
parent d890932d5c
commit d6c8efcfe6
2 changed files with 84 additions and 86 deletions
+77 -75
View File
@@ -9,7 +9,6 @@ import (
"sync" "sync"
"github.com/CalebQ42/squashfs/internal/decompress" "github.com/CalebQ42/squashfs/internal/decompress"
"github.com/CalebQ42/squashfs/internal/routinemanager"
"github.com/CalebQ42/squashfs/internal/toreader" "github.com/CalebQ42/squashfs/internal/toreader"
) )
@@ -43,6 +42,9 @@ func (r *FullReader) AddFrag(frag FragReaderConstructor) {
} }
func (r *FullReader) SetGoroutineLimit(limit uint16) { func (r *FullReader) SetGoroutineLimit(limit uint16) {
if limit <= 0 {
r.goroutineLimit = 1
}
r.goroutineLimit = limit r.goroutineLimit = limit
} }
@@ -75,9 +77,9 @@ func (r FullReader) process(index uint64, fileOffset uint64, pool *sync.Pool, re
} }
func (r FullReader) WriteTo(w io.Writer) (int64, error) { func (r FullReader) WriteTo(w io.Writer) (int64, error) {
if wa, is := w.(io.WriterAt); is { // if wa, is := w.(io.WriterAt); is {
return r.writeToWriteAt(wa) // return r.writeToWriteAt(wa)
} // }
var curIndex uint64 var curIndex uint64
var curOffset uint64 var curOffset uint64
var toProcess uint16 var toProcess uint16
@@ -173,74 +175,74 @@ func (r FullReader) WriteTo(w io.Writer) (int64, error) {
return wrote, nil return wrote, nil
} }
func (r FullReader) writeToWriteAt(w io.WriterAt) (out int64, outErr error) { // func (r FullReader) writeToWriteAt(w io.WriterAt) (out int64, outErr error) {
wait := sync.WaitGroup{} // wait := &sync.WaitGroup{}
wait.Add(len(r.sizes)) // wait.Add(len(r.sizes))
mgr := routinemanager.NewManager(r.goroutineLimit) // mgr := routinemanager.NewManager(r.goroutineLimit)
curOffset := r.initialOffset // curOffset := r.initialOffset
for i := uint64(0); i < uint64(len(r.sizes)); i++ { // for i := uint64(0); i < uint64(len(r.sizes)); i++ {
go func(index uint64, fileOffset int64) { // go func(index uint64, fileOffset int64) {
lckNum := mgr.Lock() // lckNum := mgr.Lock()
defer mgr.Unlock(lckNum) // defer mgr.Unlock(lckNum)
defer wait.Done() // defer wait.Done()
realSize := r.sizes[index] &^ (1 << 24) // realSize := r.sizes[index] &^ (1 << 24)
if realSize == 0 { // if realSize == 0 {
if index == uint64(len(r.sizes))-1 && r.frag == nil { // if index == uint64(len(r.sizes))-1 && r.frag == nil {
_, err := w.WriteAt([]byte{0}, int64((uint64(r.blockSize)*index)+r.finalBlockSize)-1) // _, err := w.WriteAt([]byte{0}, int64((uint64(r.blockSize)*index)+r.finalBlockSize)-1)
if err != nil { // if err != nil {
outErr = errors.Join(outErr, err) // outErr = errors.Join(outErr, err)
return // return
} // }
out = max(out, int64((uint64(r.blockSize)*index)+r.finalBlockSize)) // out = max(out, int64((uint64(r.blockSize)*index)+r.finalBlockSize))
} // }
return // return
} // }
data := make([]byte, realSize) // data := make([]byte, realSize)
err := binary.Read(toreader.NewReader(r.r, int64(r.initialOffset)+int64(fileOffset)), binary.LittleEndian, &data) // err := binary.Read(toreader.NewReader(r.r, int64(fileOffset)), binary.LittleEndian, &data)
if err != nil { // if err != nil {
outErr = errors.Join(outErr, err) // outErr = errors.Join(outErr, err)
return // return
} // }
if r.sizes[index] == realSize { // if r.sizes[index] == realSize {
data, err = r.d.Decompress(data) // data, err = r.d.Decompress(data)
} // }
if err != nil { // if err != nil {
outErr = errors.Join(outErr, err) // outErr = errors.Join(outErr, err)
return // return
} // }
_, err = w.WriteAt(data, int64(uint64(r.blockSize)*index)) // _, err = w.WriteAt(data, int64(uint64(r.blockSize)*index))
if err != nil { // if err != nil {
outErr = errors.Join(outErr, err) // outErr = errors.Join(outErr, err)
return // return
} // }
out = max(out, int64(uint64(r.blockSize)*(index+1))) // out = max(out, int64(uint64(r.blockSize)*(index+1)))
}(i, curOffset) // }(i, curOffset)
curOffset += int64(r.sizes[i]) &^ (1 << 24) // curOffset += int64(r.sizes[i]) &^ (1 << 24)
} // }
if r.frag != nil { // if r.frag != nil {
wait.Add(1) // wait.Add(1)
go func() { // go func() {
lckNum := mgr.Lock() // lckNum := mgr.Lock()
defer mgr.Unlock(lckNum) // defer mgr.Unlock(lckNum)
defer wait.Done() // defer wait.Done()
rdr, err := r.frag() // rdr, err := r.frag()
if err != nil { // if err != nil {
outErr = errors.Join(outErr, err) // outErr = errors.Join(outErr, err)
return // return
} // }
dat, err := io.ReadAll(rdr) // dat, err := io.ReadAll(rdr)
if err != nil { // if err != nil {
outErr = errors.Join(outErr, err) // outErr = errors.Join(outErr, err)
return // return
} // }
_, err = w.WriteAt(dat, int64(int(r.blockSize)*len(r.sizes))) // _, err = w.WriteAt(dat, int64(int(r.blockSize)*len(r.sizes)))
if err != nil { // if err != nil {
outErr = errors.Join(outErr, err) // outErr = errors.Join(outErr, err)
return // return
} // }
out = int64(int(r.blockSize)*len(r.sizes)) + int64(r.finalBlockSize) // out = int64(int(r.blockSize)*len(r.sizes)) + int64(r.finalBlockSize)
}() // }()
} // }
wait.Wait() // wait.Wait()
return // return
} // }
+7 -11
View File
@@ -100,8 +100,8 @@ func BenchmarkRace(b *testing.B) {
b.Log("Unsquashfs error:", err) b.Log("Unsquashfs error:", err)
} }
unsquashTime = time.Since(start) unsquashTime = time.Since(start)
// b.Log("Library took:", libTime.Round(time.Millisecond)) b.Log("Library took:", libTime.Round(time.Millisecond))
// b.Log("unsquashfs took:", unsquashTime.Round(time.Millisecond)) b.Log("unsquashfs took:", unsquashTime.Round(time.Millisecond))
b.Log("unsquashfs is", strconv.FormatFloat(float64(libTime.Milliseconds())/float64(unsquashTime.Milliseconds()), 'f', 2, 64), "times faster") b.Log("unsquashfs is", strconv.FormatFloat(float64(libTime.Milliseconds())/float64(unsquashTime.Milliseconds()), 'f', 2, 64), "times faster")
} }
@@ -124,7 +124,7 @@ func TestExtractQuick(t *testing.T) {
} }
os.RemoveAll(filepath.Join(tmpDir, "testLog.txt")) os.RemoveAll(filepath.Join(tmpDir, "testLog.txt"))
logFil, _ := os.Create(filepath.Join(tmpDir, "testLog.txt")) logFil, _ := os.Create(filepath.Join(tmpDir, "testLog.txt"))
op := DefaultOptions() op := FastOptions()
op.Verbose = true op.Verbose = true
op.IgnorePerm = true op.IgnorePerm = true
op.LogOutput = logFil op.LogOutput = logFil
@@ -167,7 +167,7 @@ func TestExtractQuick(t *testing.T) {
} }
} }
var filePath = "bin" var filePath = "Start.exe"
func TestSingleFile(t *testing.T) { func TestSingleFile(t *testing.T) {
tmpDir := "testing" tmpDir := "testing"
@@ -184,15 +184,11 @@ func TestSingleFile(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = f.(*File).ExtractWithOptions("testing", &ExtractionOptions{Verbose: true}) op := DefaultOptions()
op.Verbose = true
err = f.(*File).ExtractWithOptions("testing", op)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Fatal("HI") t.Fatal("HI")
} }
func TestStuff(t *testing.T) {
fil, _ := os.Create("testing/stuff.txt")
_, err := fil.WriteAt([]byte("Yo"), 1024)
t.Fatal(err)
}