1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 17:47:03 +00:00

file: pool index readers to reduce allocs for #122

This commit is contained in:
James Hillyerd
2018-10-20 20:33:27 -07:00
parent 98745b3bb9
commit f68f07d896
2 changed files with 31 additions and 6 deletions

View File

@@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"time" "time"
"github.com/jhillyerd/inbucket/pkg/config" "github.com/jhillyerd/inbucket/pkg/config"
@@ -40,10 +41,11 @@ func countGenerator(c chan int) {
// Store implements DataStore aand is the root of the mail storage // Store implements DataStore aand is the root of the mail storage
// hiearchy. It provides access to Mailbox objects // hiearchy. It provides access to Mailbox objects
type Store struct { type Store struct {
hashLock storage.HashLock hashLock storage.HashLock
path string path string
mailPath string mailPath string
messageCap int messageCap int
bufReaderPool sync.Pool
} }
// New creates a new DataStore object using the specified path // New creates a new DataStore object using the specified path
@@ -60,7 +62,16 @@ func New(cfg config.Storage) (storage.Store, error) {
Msg("Error creating dir") Msg("Error creating dir")
} }
} }
return &Store{path: path, mailPath: mailPath, messageCap: cfg.MailboxMsgCap}, nil return &Store{
path: path,
mailPath: mailPath,
messageCap: cfg.MailboxMsgCap,
bufReaderPool: sync.Pool{
New: func() interface{} {
return bufio.NewReader(nil)
},
},
}, nil
} }
// AddMessage adds a message to the specified mailbox. // AddMessage adds a message to the specified mailbox.
@@ -253,6 +264,18 @@ func (fs *Store) mboxFromHash(hash string) *mbox {
} }
} }
// getPooledReader pulls a buffered reader from the fs.bufReaderPool
func (fs *Store) getPooledReader(r io.Reader) *bufio.Reader {
br := fs.bufReaderPool.Get().(*bufio.Reader)
br.Reset(r)
return br
}
// putPooledReader returns a buffered reader to the fs.bufReaderPool
func (fs *Store) putPooledReader(br *bufio.Reader) {
fs.bufReaderPool.Put(br)
}
// generatePrefix converts a Time object into the ISO style format we use // generatePrefix converts a Time object into the ISO style format we use
// as a prefix for message files. Note: It is used directly by unit // as a prefix for message files. Note: It is used directly by unit
// tests. // tests.

View File

@@ -120,7 +120,9 @@ func (mb *mbox) readIndex() error {
} }
}() }()
// Decode gob data // Decode gob data
dec := gob.NewDecoder(bufio.NewReader(file)) br := mb.store.getPooledReader(file)
defer mb.store.putPooledReader(br)
dec := gob.NewDecoder(br)
name := "" name := ""
if err = dec.Decode(&name); err != nil { if err = dec.Decode(&name); err != nil {
return fmt.Errorf("Corrupt mailbox %q: %v", mb.indexPath, err) return fmt.Errorf("Corrupt mailbox %q: %v", mb.indexPath, err)