Skip to content

Commit

Permalink
Go back to using os.ReadFile (which calls os.Stat)
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Jul 29, 2023
1 parent 476b288 commit 3c168b3
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 38 deletions.
Binary file removed img/readfile_opt.png
Binary file not shown.
2 changes: 1 addition & 1 deletion v2/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (kh *KeyHolder) ReadAPIKey() bool {
if kh.Filename == "" {
return kh.Key != ""
}
data, err := ReadFileNoStat(kh.Filename)
data, err := os.ReadFile(kh.Filename)
if err != nil {
return kh.Key != ""
}
Expand Down
2 changes: 1 addition & 1 deletion v2/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (e *Editor) GenerateBuildCommand(filename string) (*exec.Cmd, func() (bool,
}
// Just build the current file
sourceCode := ""
sourceData, err := ReadFileNoStat(sourceFilename)
sourceData, err := os.ReadFile(sourceFilename)
if err == nil { // success
sourceCode = string(sourceData)
}
Expand Down
4 changes: 3 additions & 1 deletion v2/copyfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"os"

"github.com/xyproto/clip"
)

Expand All @@ -9,7 +11,7 @@ import (
// The returned string is the last 7 characters written to the file.
func SetClipboardFromFile(filename string) (int, string, error) {
// Read the file
data, err := ReadFileNoStat(filename)
data, err := os.ReadFile(filename)
if err != nil {
return 0, "", err
}
Expand Down
2 changes: 1 addition & 1 deletion v2/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2133,7 +2133,7 @@ func (e *Editor) VerticalScrollIfNeeded(c *vt100.Canvas) {

// InsertFile inserts the contents of a file at the current location
func (e *Editor) InsertFile(c *vt100.Canvas, filename string) error {
data, err := ReadFileNoStat(filename)
data, err := os.ReadFile(filename)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion v2/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ func saveHighScore(highScore uint) error {
// loadHighScore will load the current high score from the highScoreFile,
// if possible.
func loadHighScore() (uint, error) {
data, err := ReadFileNoStat(highScoreFile)
data, err := os.ReadFile(highScoreFile)
if err != nil {
return 0, err
}
Expand Down
10 changes: 5 additions & 5 deletions v2/lochist.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (locationHistory LocationHistory) Len() int {
func LoadLocationHistory(configFile string) (LocationHistory, error) {
locationHistory := make(LocationHistory)

contents, err := ReadFileNoStat(configFile)
contents, err := os.ReadFile(configFile)
if err != nil {
// Could not read file, return an empty map and an error
return locationHistory, err
Expand Down Expand Up @@ -164,7 +164,7 @@ func LoadLocationHistory(configFile string) (LocationHistory, error) {
func LoadVimLocationHistory(vimInfoFilename string) LocationHistory {
locationHistory := make(LocationHistory)
// Attempt to read the ViM location history (that may or may not exist)
data, err := ReadFileNoStat(vimInfoFilename)
data, err := os.ReadFile(vimInfoFilename)
if err != nil {
return locationHistory
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func LoadVimLocationHistory(vimInfoFilename string) LocationHistory {
// FindInVimLocationHistory will try to find the given filename in the ViM .viminfo file
func FindInVimLocationHistory(vimInfoFilename, searchFilename string) (LineNumber, error) {
// Attempt to read the ViM location history (that may or may not exist)
data, err := ReadFileNoStat(vimInfoFilename)
data, err := os.ReadFile(vimInfoFilename)
if err != nil {
return LineNumber(-1), err
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func FindInVimLocationHistory(vimInfoFilename, searchFilename string) (LineNumbe
func FindInNvimLocationHistory(nvimLocationFilename, searchFilename string) (LineNumber, error) {
nol := LineNumber(-1) // no line number

data, err := ReadFileNoStat(nvimLocationFilename) // typically main.shada, a MsgPack file
data, err := os.ReadFile(nvimLocationFilename) // typically main.shada, a MsgPack file
if err != nil {
return nol, err
}
Expand Down Expand Up @@ -459,7 +459,7 @@ func FindInNvimLocationHistory(nvimLocationFilename, searchFilename string) (Lin
func LoadEmacsLocationHistory(emacsPlacesFilename string) map[string]CharacterPosition {
locationCharHistory := make(map[string]CharacterPosition)
// Attempt to read the Emacs location history (that may or may not exist)
data, err := ReadFileNoStat(emacsPlacesFilename)
data, err := os.ReadFile(emacsPlacesFilename)
if err != nil {
return locationCharHistory
}
Expand Down
4 changes: 2 additions & 2 deletions v2/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func HasPortal() bool {

// LoadPortal will load a filename + line number from the portal.txt file
func LoadPortal() (*Portal, error) {
data, err := ReadFileNoStat(portalFilename)
data, err := os.ReadFile(portalFilename)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func (p *Portal) PopLine(e *Editor, removeLine bool) (string, error) {
// The line moving is done by the editor InsertAbove and InsertBelow functions
return e.Line(p.LineIndex()), nil
}
data, err := ReadFileNoStat(p.absFilename)
data, err := os.ReadFile(p.absFilename)
if err != nil {
return "", err
}
Expand Down
22 changes: 1 addition & 21 deletions v2/readfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,9 @@ import (
"github.com/xyproto/binary"
)

// ReadFileNoStat is similar to os.ReadFile, but does not call os.Stat to get the file size first.
// It also reads data into a 1024 bytes large buffer, which has a good performance profile.
// See the graph in ../img/readfile_opt.png for why reading 1024 bytes at the time was chosen.
func ReadFileNoStat(name string) ([]byte, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
defer f.Close()

// Prepare the buffer
buf := bytes.NewBuffer(make([]byte, 0, 1024))

_, err = buf.ReadFrom(f)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

// ReadFileAndProcessLines reads the named file concurrently, processes its lines, and updates the Editor.
func (e *Editor) ReadFileAndProcessLines(filename string) error {
data, err := ReadFileNoStat(filename)
data, err := os.ReadFile(filename)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion v2/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ AGAIN:

// LoadSearchHistory will load a list of strings from the given filename
func LoadSearchHistory(filename string) ([]string, error) {
data, err := ReadFileNoStat(filename)
data, err := os.ReadFile(filename)
if err != nil {
return []string{}, err
}
Expand Down
3 changes: 2 additions & 1 deletion v2/suggest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"os"
"path/filepath"
"regexp"

Expand All @@ -22,7 +23,7 @@ func corpus(searchword, glob string) []string {
var data []byte
var highestCount int
for _, filename := range filenames {
data, err = ReadFileNoStat(filename)
data, err = os.ReadFile(filename)
if err != nil {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions v2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func getFullName() (fullName string) {
// Then look for ~/.gitconfig
gitConfigFilename := env.ExpandUser("~/.gitconfig")
if exists(gitConfigFilename) {
data, err := ReadFileNoStat(gitConfigFilename)
data, err := os.ReadFile(gitConfigFilename)
if err != nil {
return fullName
}
Expand Down Expand Up @@ -432,7 +432,7 @@ func shortPath(path string) string {

// fileHas checks if the given file exists and contains the given string
func fileHas(path, what string) bool {
data, err := ReadFileNoStat(path)
data, err := os.ReadFile(path)
if err != nil {
return false
}
Expand All @@ -441,7 +441,7 @@ func fileHas(path, what string) bool {

// parentCommand returns either the command of the parent process or an empty string
func parentCommand() string {
commandString, err := ReadFileNoStat(fmt.Sprintf("/proc/%d/cmdline", os.Getppid()))
commandString, err := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", os.Getppid()))
if err != nil {
return ""
}
Expand Down

0 comments on commit 3c168b3

Please sign in to comment.