diff options
author | Jakob Borg <jakob@nym.se> | 2014-06-18 23:57:22 +0200 |
---|---|---|
committer | Jakob Borg <jakob@nym.se> | 2014-06-19 01:02:32 +0200 |
commit | 4dbce32738ac610f8cabc8db2133580f31dbadee (patch) | |
tree | 2a21d7c2cf36bfb1d8940f9d035e1866a4253e96 | |
parent | b05fcbc9d72dda356989bad150ada80b690bea64 (diff) |
Simplify memory handling
-rw-r--r-- | beacon/beacon.go | 7 | ||||
-rw-r--r-- | buffers/buffers.go | 50 | ||||
-rw-r--r-- | discover/discover.go | 6 | ||||
-rw-r--r-- | model/model.go | 4 | ||||
-rw-r--r-- | model/puller.go | 6 | ||||
-rw-r--r-- | protocol/protocol.go | 1 |
6 files changed, 8 insertions, 66 deletions
diff --git a/beacon/beacon.go b/beacon/beacon.go index 8ad7176117..f2bfd46a12 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 51bedcdab0..0000000000 --- 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 decebe839b..4eb82d388e 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 08284baf3d..9f581e23a3 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 e13778f0f5..77eba070a5 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 56f1338a5f..75c304d084 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -12,7 +12,6 @@ import ( "io" "sync" "time" - "github.com/calmh/syncthing/xdr" ) |