1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
Brian Stengaard 642ab409c1 Use Seek to speed up read/write in sysfs
This maintains `direction` and `value` `File`s for each DigitalPin
implementation. Instead of Open/Read/Close we now only do Seek/Read,
this speeds up Read/Write operations a bit.

A silly benchmark on the mock FS gives:

benchmark                  old ns/op     new ns/op     delta
BenchmarkDigitalRead-8     647           7.36          -98.86%

benchmark                  old allocs     new allocs     delta
BenchmarkDigitalRead-8     5              0              -100.00%

benchmark                  old bytes     new bytes     delta
BenchmarkDigitalRead-8     96            0             -100.00%
2016-02-18 21:18:45 +01:00

44 lines
1.2 KiB
Go

package sysfs
import (
"os"
)
// A File represents basic IO interactions with the underlying file system
type File interface {
Write(b []byte) (n int, err error)
WriteString(s string) (ret int, err error)
Sync() (err error)
Read(b []byte) (n int, err error)
ReadAt(b []byte, off int64) (n int, err error)
Seek(offset int64, whence int) (ret int64, err error)
Fd() uintptr
Close() error
}
// Filesystem opens files and returns either a native file system or user defined
type Filesystem interface {
OpenFile(name string, flag int, perm os.FileMode) (file File, err error)
}
// NativeFilesystem represents the native file system implementation
type NativeFilesystem struct{}
// Default to the host filesystem.
var fs Filesystem = &NativeFilesystem{}
// SetFilesystem sets the filesystem implementation.
func SetFilesystem(f Filesystem) {
fs = f
}
// OpenFile calls os.OpenFile().
func (fs *NativeFilesystem) OpenFile(name string, flag int, perm os.FileMode) (file File, err error) {
return os.OpenFile(name, flag, perm)
}
// OpenFile calls either the NativeFilesystem or user defined OpenFile
func OpenFile(name string, flag int, perm os.FileMode) (file File, err error) {
return fs.OpenFile(name, flag, perm)
}