Slimfast: drop last dependency, dep free

This commit is contained in:
kayos@tcp.direct 2023-12-02 00:46:40 -08:00
parent ac1a18a304
commit b673da7b39
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
5 changed files with 61 additions and 20 deletions

2
go.mod

@ -1,5 +1,3 @@
module git.tcp.direct/kayos/ifupdown
go 1.21.4
require git.tcp.direct/kayos/common v0.9.6

2
go.sum

@ -1,2 +0,0 @@
git.tcp.direct/kayos/common v0.9.6 h1:EITtktxZF/zkzqAhZZxvm6cZpFYoZ0P/gLB9RPatKUY=
git.tcp.direct/kayos/common v0.9.6/go.mod h1:8y9b+PN1+ZVaQ/VugD9dkKe+uqhE8jH7a64RyF7h2rM=

@ -2,39 +2,38 @@ package ifupdown
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"sync"
"git.tcp.direct/kayos/common/pool"
)
type Interfaces map[string]*NetworkInterface
func (i Interfaces) buf() *pool.Buffer {
buf := pools.Buffers.Get()
func (i Interfaces) buf() *bytes.Buffer {
buf := &bytes.Buffer{}
for _, iface := range i {
err := iface.write(func(s string) { buf.MustWrite([]byte(s)) })
err := iface.write(func(s string) { _, _ = buf.Write([]byte(s)) })
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}
buf.MustWrite([]byte("\n"))
_, _ = buf.Write([]byte("\n"))
}
return buf
}
func (i Interfaces) Read(p []byte) (int, error) {
buf := i.buf()
defer pools.Buffers.MustPut(buf)
defer pools.Buffers.Put(buf)
return buf.Read(p)
}
func (i Interfaces) String() string {
buf := i.buf()
defer pools.Buffers.MustPut(buf)
defer pools.Buffers.Put(buf)
return buf.String()
}
@ -84,13 +83,13 @@ func (p *MultiParser) Parse() (Interfaces, error) {
currentIfaceName := ""
buf := pools.Buffers.Get()
defer pools.Buffers.MustPut(buf)
defer pools.Buffers.Put(buf)
flush := func(name string) (*NetworkInterface, bool) {
if len(buf.Bytes()) == 0 {
return nil, false
}
defer buf.MustReset()
defer buf.Reset()
newIface := NewNetworkInterface(name)
if _, err := buf.WriteTo(newIface); err != nil {
p.Errs = append(p.Errs, err)

@ -375,10 +375,10 @@ func (iface *NetworkInterface) String() string {
return ""
}
str := pools.Strs.Get()
defer pools.Strs.MustPut(str)
defer pools.Strs.Put(str)
w := func(s string) {
if len(s) > 0 {
str.MustWriteString(s)
str.WriteString(s)
}
}
if err := iface.write(w); err != nil && !errors.Is(err, io.EOF) {

54
util.go

@ -1,10 +1,56 @@
package ifupdown
import "git.tcp.direct/kayos/common/pool"
import (
"bytes"
"strings"
"sync"
)
// lol a bit redundant
type poolGroup struct {
Buffers pool.BufferFactory
Strs pool.StringFactory
Buffers p[*bytes.Buffer]
Strs p[*strings.Builder]
}
var pools = poolGroup{Buffers: pool.NewBufferFactory(), Strs: pool.NewStringFactory()}
type p[T interface{ Reset() }] interface {
Get() T
Put(T)
}
type swim[T interface{ Reset() }] struct {
pool *sync.Pool
}
func newPool[T interface{ Reset() }](new func() T) swim[T] {
return swim[T]{
pool: &sync.Pool{New: func() interface{} { return new() }},
}
}
func (s *swim[T]) Get() T {
return s.pool.Get().(T)
}
func (s *swim[T]) Put(t T) {
t.Reset()
s.pool.Put(t)
}
type bufs struct {
buffers *swim[*bytes.Buffer]
strings *swim[*strings.Builder]
}
func newBufs() bufs {
b := newPool[*bytes.Buffer](func() *bytes.Buffer { return &bytes.Buffer{} })
s := newPool[*strings.Builder](func() *strings.Builder { return &strings.Builder{} })
return bufs{
buffers: &b,
strings: &s,
}
}
var bufPools = newBufs()
var pools = poolGroup{Buffers: bufPools.buffers, Strs: bufPools.strings}