diff options
| author | 2025-11-05 17:53:47 +0100 | |
|---|---|---|
| committer | 2025-11-17 14:12:29 +0100 | |
| commit | ddf887d95c1cef95746f9881dc4180d983e7a351 (patch) | |
| tree | 085dd1942c87ca1ff7da32dcdc5b3a9c0b6e5f28 /vendor/codeberg.org/gruf/go-mmap/open.go | |
| parent | [bugfix] more RSS validation issues (#4517) (diff) | |
| download | gotosocial-ddf887d95c1cef95746f9881dc4180d983e7a351.tar.xz | |
[performance] when transforming media, perform read operations of large files using mmap (#4541)
the performance gains aren't as substantial as i was hoping, but benchmarks did show it averaged out faster using this method. unfortunately i managed to lose the benchmarks i wrote with a poorly timed `git checkout -- .` ðŸ˜
Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4541
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-mmap/open.go')
| -rw-r--r-- | vendor/codeberg.org/gruf/go-mmap/open.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-mmap/open.go b/vendor/codeberg.org/gruf/go-mmap/open.go new file mode 100644 index 000000000..c12a091d3 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-mmap/open.go @@ -0,0 +1,62 @@ +package mmap + +import ( + "io" + "io/fs" + "os" + "runtime" + "syscall" +) + +// MmapThreshold defines the threshold file size (in bytes) for which +// a call to OpenRead() will deem as big enough for a file to be worth +// opening using an `mmap` syscall. This is a runtime initialized number +// based on the number of available CPUs as in concurrent conditions Go +// can make optimizations for blocking `read` syscalls which scales with +// the number of available goroutines it can have running at once. +var MmapThreshold = int64(runtime.NumCPU() * syscall.Getpagesize()) + +// FileReader defines the base interface +// of a readable file, whether accessed +// via `read` or `mmap` syscalls. +type FileReader interface { + fs.File + io.ReaderAt + io.WriterTo + io.Seeker + Name() string +} + +// Threshold is a receiving type for OpenRead() +// that allows defining a custom MmapThreshold. +type Threshold struct{ At int64 } + +// OpenRead: see mmap.OpenRead(). +func (t Threshold) OpenRead(path string) (FileReader, error) { + stat, err := stat(path) + if err != nil { + return nil, err + } + if stat.Size() >= t.At { + return openMmap(path, stat) + } else { + return os.OpenFile(path, syscall.O_RDONLY, 0) + } +} + +// OpenRead will open the file as read only (erroring if it does +// not already exist). If the file at path is beyond 'MmapThreshold' +// it will be opened for reads using an `mmap` syscall, by calling +// MmappedRead(path). Else, it will be opened using os.OpenFile(). +// +// Please note that the reader returned by this function is not +// guaranteed to be concurrency-safe. Calls returned by os.OpenFile() +// follow the usual standard library concurrency guarantees, but the +// reader returned by MmappedRead() provides no concurrent protection. +// +// Also note that this may not always be faster! If the file you need +// to open will be immediately drained to another file, TCP or Unix +// connection, then the standard library will used optimized syscalls. +func OpenRead(path string) (FileReader, error) { + return Threshold{MmapThreshold}.OpenRead(path) +} |
