From 4dbce32738ac610f8cabc8db2133580f31dbadee Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Wed, 18 Jun 2014 23:57:22 +0200 Subject: [PATCH] Simplify memory handling --- beacon/beacon.go | 7 +++++-- buffers/buffers.go | 50 -------------------------------------------- discover/discover.go | 6 +----- model/model.go | 4 +--- model/puller.go | 6 +----- protocol/protocol.go | 1 - 6 files changed, 8 insertions(+), 66 deletions(-) delete mode 100644 buffers/buffers.go diff --git a/beacon/beacon.go b/beacon/beacon.go index 8ad717611..f2bfd46a1 100644 --- a/beacon/beacon.go +++ b/beacon/beacon.go @@ -52,7 +52,7 @@ func (b *Beacon) Recv() ([]byte, net.Addr) { } func (b *Beacon) reader() { - var bs = make([]byte, 65536) + bs := make([]byte, 65536) for { n, addr, err := b.conn.ReadFrom(bs) if err != nil { @@ -62,8 +62,11 @@ func (b *Beacon) reader() { if debug { l.Debugf("recv %d bytes from %s", n, addr) } + + c := make([]byte, n) + copy(c, bs) select { - case b.outbox <- recv{bs[:n], addr}: + case b.outbox <- recv{c, addr}: default: if debug { l.Debugln("dropping message") diff --git a/buffers/buffers.go b/buffers/buffers.go deleted file mode 100644 index 51bedcdab..000000000 --- a/buffers/buffers.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved. -// Use of this source code is governed by an MIT-style license that can be -// found in the LICENSE file. - -// Package buffers manages a set of reusable byte buffers. -package buffers - -const ( - largeMin = 1024 -) - -var ( - smallBuffers = make(chan []byte, 32) - largeBuffers = make(chan []byte, 32) -) - -func Get(size int) []byte { - var ch = largeBuffers - if size < largeMin { - ch = smallBuffers - } - - var buf []byte - select { - case buf = <-ch: - default: - } - - if len(buf) < size { - return make([]byte, size) - } - return buf[:size] -} - -func Put(buf []byte) { - buf = buf[:cap(buf)] - if len(buf) == 0 { - return - } - - var ch = largeBuffers - if len(buf) < largeMin { - ch = smallBuffers - } - - select { - case ch <- buf: - default: - } -} diff --git a/discover/discover.go b/discover/discover.go index decebe839..4eb82d388 100644 --- a/discover/discover.go +++ b/discover/discover.go @@ -14,7 +14,6 @@ import ( "time" "github.com/calmh/syncthing/beacon" - "github.com/calmh/syncthing/buffers" ) type Discoverer struct { @@ -329,11 +328,8 @@ func (d *Discoverer) externalLookup(node string) []string { } return nil } - buffers.Put(buf) - - buf = buffers.Get(2048) - defer buffers.Put(buf) + buf = make([]byte, 2048) n, err := conn.Read(buf) if err != nil { if err, ok := err.(net.Error); ok && err.Timeout() { diff --git a/model/model.go b/model/model.go index 08284baf3..9f581e23a 100644 --- a/model/model.go +++ b/model/model.go @@ -15,8 +15,6 @@ import ( "path/filepath" "sync" "time" - - "github.com/calmh/syncthing/buffers" "github.com/calmh/syncthing/cid" "github.com/calmh/syncthing/config" "github.com/calmh/syncthing/files" @@ -433,7 +431,7 @@ func (m *Model) Request(nodeID, repo, name string, offset int64, size int) ([]by } defer fd.Close() - buf := buffers.Get(int(size)) + buf := make([]byte, size) _, err = fd.ReadAt(buf, offset) if err != nil { return nil, err diff --git a/model/puller.go b/model/puller.go index e13778f0f..77eba070a 100644 --- a/model/puller.go +++ b/model/puller.go @@ -11,8 +11,6 @@ import ( "path/filepath" "runtime" "time" - - "github.com/calmh/syncthing/buffers" "github.com/calmh/syncthing/cid" "github.com/calmh/syncthing/config" "github.com/calmh/syncthing/osutil" @@ -339,7 +337,6 @@ func (p *puller) handleRequestResult(res requestResult) { } _, of.err = of.file.WriteAt(res.data, res.offset) - buffers.Put(res.data) of.outstanding-- p.openFiles[f.Name] = of @@ -490,12 +487,11 @@ func (p *puller) handleCopyBlock(b bqBlock) { defer exfd.Close() for _, b := range b.copy { - bs := buffers.Get(int(b.Size)) + bs := make([]byte, b.Size) _, of.err = exfd.ReadAt(bs, b.Offset) if of.err == nil { _, of.err = of.file.WriteAt(bs, b.Offset) } - buffers.Put(bs) if of.err != nil { if debug { l.Debugf("pull: error: %q / %q: %v", p.repoCfg.ID, f.Name, of.err) diff --git a/protocol/protocol.go b/protocol/protocol.go index 56f1338a5..75c304d08 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -12,7 +12,6 @@ import ( "io" "sync" "time" - "github.com/calmh/syncthing/xdr" )