Reduce some of the reads and writes

It's really not necessary
This commit is contained in:
2025-03-28 23:38:51 +01:00
parent 2e3e958e15
commit 35b3d8b099

64
main.go
View File

@@ -5,7 +5,6 @@ import (
"fmt"
"os"
"sort"
"strings"
"sync"
"time"
@@ -132,18 +131,31 @@ func main() {
// Track per-file processing time
fileStartTime := time.Now()
err := RunIsolateCommands(association, file, &fileMutex)
fileData, err := os.ReadFile(file)
if err != nil {
logger.Error("Failed to read file %q: %v", file, err)
return
}
fileDataStr := string(fileData)
fileDataStr, err = RunIsolateCommands(association, file, fileDataStr, &fileMutex)
if err != nil {
logger.Error("Failed to run isolate commands for file %q: %v", file, err)
return
}
err = RunOtherCommands(file, association, &fileMutex)
fileDataStr, err = RunOtherCommands(file, fileDataStr, association, &fileMutex)
if err != nil {
logger.Error("Failed to run other commands for file %q: %v", file, err)
return
}
err = os.WriteFile(file, []byte(fileDataStr), 0644)
if err != nil {
logger.Error("Failed to write file %q: %v", file, err)
return
}
logger.Debug("File %q processed in %v", file, time.Since(fileStartTime))
}, file, commands)
}
@@ -218,21 +230,14 @@ func main() {
}
}
func RunOtherCommands(file string, association utils.FileCommandAssociation, fileMutex *sync.Mutex) error {
fileData, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("failed to read file %q: %w", file, err)
}
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
fileDataStr := string(fileData)
func RunOtherCommands(file string, fileDataStr string, association utils.FileCommandAssociation, fileMutex *sync.Mutex) (string, error) {
// Aggregate all the modifications and execute them
modifications := []utils.ReplaceCommand{}
for _, command := range association.Commands {
logger.Info("Processing file %q with command %q", file, command.Regex)
newModifications, err := processor.ProcessRegex(fileDataStr, command, file)
if err != nil {
return fmt.Errorf("failed to process file %q with command %q: %w", file, command.Regex, err)
return fileDataStr, fmt.Errorf("failed to process file %q with command %q: %w", file, command.Regex, err)
}
modifications = append(modifications, newModifications...)
// It is not guranteed that all the commands will be executed...
@@ -247,11 +252,12 @@ func RunOtherCommands(file string, association utils.FileCommandAssociation, fil
if len(modifications) == 0 {
logger.Info("No modifications found for file %q", file)
return nil
return fileDataStr, nil
}
// Sort commands in reverse order for safe replacements
fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr)
var count int
fileDataStr, count = utils.ExecuteModifications(modifications, fileDataStr)
fileMutex.Lock()
stats.ProcessedFiles++
@@ -259,35 +265,24 @@ func RunOtherCommands(file string, association utils.FileCommandAssociation, fil
fileMutex.Unlock()
logger.Info("Executed %d modifications for file %q", count, file)
err = os.WriteFile(file, []byte(fileDataStr), 0644)
if err != nil {
return fmt.Errorf("failed to write file %q: %w", file, err)
}
return nil
return fileDataStr, nil
}
func RunIsolateCommands(association utils.FileCommandAssociation, file string, fileMutex *sync.Mutex) error {
func RunIsolateCommands(association utils.FileCommandAssociation, file string, fileDataStr string, fileMutex *sync.Mutex) (string, error) {
for _, isolateCommand := range association.IsolateCommands {
fileData, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("failed to read file %q: %w", file, err)
}
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
fileDataStr := string(fileData)
logger.Info("Processing file %q with isolate command %q", file, isolateCommand.Regex)
modifications, err := processor.ProcessRegex(fileDataStr, isolateCommand, file)
if err != nil {
return fmt.Errorf("failed to process file %q with isolate command %q: %w", file, isolateCommand.Regex, err)
return fileDataStr, fmt.Errorf("failed to process file %q with isolate command %q: %w", file, isolateCommand.Regex, err)
}
if len(modifications) == 0 {
logger.Warning("No modifications found for file %q", file)
return nil
return fileDataStr, nil
}
fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr)
var count int
fileDataStr, count = utils.ExecuteModifications(modifications, fileDataStr)
fileMutex.Lock()
stats.ProcessedFiles++
@@ -295,11 +290,6 @@ func RunIsolateCommands(association utils.FileCommandAssociation, file string, f
fileMutex.Unlock()
logger.Info("Executed %d isolate modifications for file %q", count, file)
err = os.WriteFile(file, []byte(fileDataStr), 0644)
if err != nil {
return fmt.Errorf("failed to write file %q: %w", file, err)
}
}
return nil
return fileDataStr, nil
}