mirror of
https://github.com/octoleo/syncthing.git
synced 2025-02-02 11:58:28 +00:00
Simplify memory handling
This commit is contained in:
parent
b05fcbc9d7
commit
4dbce32738
@ -52,7 +52,7 @@ func (b *Beacon) Recv() ([]byte, net.Addr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Beacon) reader() {
|
func (b *Beacon) reader() {
|
||||||
var bs = make([]byte, 65536)
|
bs := make([]byte, 65536)
|
||||||
for {
|
for {
|
||||||
n, addr, err := b.conn.ReadFrom(bs)
|
n, addr, err := b.conn.ReadFrom(bs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -62,8 +62,11 @@ func (b *Beacon) reader() {
|
|||||||
if debug {
|
if debug {
|
||||||
l.Debugf("recv %d bytes from %s", n, addr)
|
l.Debugf("recv %d bytes from %s", n, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c := make([]byte, n)
|
||||||
|
copy(c, bs)
|
||||||
select {
|
select {
|
||||||
case b.outbox <- recv{bs[:n], addr}:
|
case b.outbox <- recv{c, addr}:
|
||||||
default:
|
default:
|
||||||
if debug {
|
if debug {
|
||||||
l.Debugln("dropping message")
|
l.Debugln("dropping message")
|
||||||
|
@ -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:
|
|
||||||
}
|
|
||||||
}
|
|
@ -14,7 +14,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/calmh/syncthing/beacon"
|
"github.com/calmh/syncthing/beacon"
|
||||||
"github.com/calmh/syncthing/buffers"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Discoverer struct {
|
type Discoverer struct {
|
||||||
@ -329,11 +328,8 @@ func (d *Discoverer) externalLookup(node string) []string {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
buffers.Put(buf)
|
|
||||||
|
|
||||||
buf = buffers.Get(2048)
|
|
||||||
defer buffers.Put(buf)
|
|
||||||
|
|
||||||
|
buf = make([]byte, 2048)
|
||||||
n, err := conn.Read(buf)
|
n, err := conn.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err, ok := err.(net.Error); ok && err.Timeout() {
|
if err, ok := err.(net.Error); ok && err.Timeout() {
|
||||||
|
@ -15,8 +15,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/calmh/syncthing/buffers"
|
|
||||||
"github.com/calmh/syncthing/cid"
|
"github.com/calmh/syncthing/cid"
|
||||||
"github.com/calmh/syncthing/config"
|
"github.com/calmh/syncthing/config"
|
||||||
"github.com/calmh/syncthing/files"
|
"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()
|
defer fd.Close()
|
||||||
|
|
||||||
buf := buffers.Get(int(size))
|
buf := make([]byte, size)
|
||||||
_, err = fd.ReadAt(buf, offset)
|
_, err = fd.ReadAt(buf, offset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -11,8 +11,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/calmh/syncthing/buffers"
|
|
||||||
"github.com/calmh/syncthing/cid"
|
"github.com/calmh/syncthing/cid"
|
||||||
"github.com/calmh/syncthing/config"
|
"github.com/calmh/syncthing/config"
|
||||||
"github.com/calmh/syncthing/osutil"
|
"github.com/calmh/syncthing/osutil"
|
||||||
@ -339,7 +337,6 @@ func (p *puller) handleRequestResult(res requestResult) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_, of.err = of.file.WriteAt(res.data, res.offset)
|
_, of.err = of.file.WriteAt(res.data, res.offset)
|
||||||
buffers.Put(res.data)
|
|
||||||
|
|
||||||
of.outstanding--
|
of.outstanding--
|
||||||
p.openFiles[f.Name] = of
|
p.openFiles[f.Name] = of
|
||||||
@ -490,12 +487,11 @@ func (p *puller) handleCopyBlock(b bqBlock) {
|
|||||||
defer exfd.Close()
|
defer exfd.Close()
|
||||||
|
|
||||||
for _, b := range b.copy {
|
for _, b := range b.copy {
|
||||||
bs := buffers.Get(int(b.Size))
|
bs := make([]byte, b.Size)
|
||||||
_, of.err = exfd.ReadAt(bs, b.Offset)
|
_, of.err = exfd.ReadAt(bs, b.Offset)
|
||||||
if of.err == nil {
|
if of.err == nil {
|
||||||
_, of.err = of.file.WriteAt(bs, b.Offset)
|
_, of.err = of.file.WriteAt(bs, b.Offset)
|
||||||
}
|
}
|
||||||
buffers.Put(bs)
|
|
||||||
if of.err != nil {
|
if of.err != nil {
|
||||||
if debug {
|
if debug {
|
||||||
l.Debugf("pull: error: %q / %q: %v", p.repoCfg.ID, f.Name, of.err)
|
l.Debugf("pull: error: %q / %q: %v", p.repoCfg.ID, f.Name, of.err)
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/calmh/syncthing/xdr"
|
"github.com/calmh/syncthing/xdr"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user