Experiment with sync.Pool

This commit is contained in:
kortschak 2014-08-14 19:03:55 +09:30
parent 6d609f191a
commit 737037a894
2 changed files with 32 additions and 27 deletions

View file

@ -185,13 +185,13 @@ func (it *Iterator) Contains(v graph.Value) bool {
case quad.Subject: case quad.Subject:
offset = 0 offset = 0
case quad.Predicate: case quad.Predicate:
offset = (it.qs.hasherSize * 2) offset = (hashSize * 2)
case quad.Object: case quad.Object:
offset = (it.qs.hasherSize * 2) * 2 offset = (hashSize * 2) * 2
case quad.Label: case quad.Label:
offset = (it.qs.hasherSize * 2) * 3 offset = (hashSize * 2) * 3
} }
val := v.(string)[offset : it.qs.hasherSize*2+offset] val := v.(string)[offset : hashSize*2+offset]
if val == it.hash { if val == it.hash {
it.result = v it.result = v
return graph.ContainsLogOut(it, v, true) return graph.ContainsLogOut(it, v, true)

View file

@ -19,6 +19,7 @@ import (
"encoding/hex" "encoding/hex"
"hash" "hash"
"io" "io"
"sync"
"gopkg.in/mgo.v2" "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
@ -38,12 +39,17 @@ var _ graph.BulkLoader = (*TripleStore)(nil)
const DefaultDBName = "cayley" const DefaultDBName = "cayley"
var (
hashPool = sync.Pool{
New: func() interface{} { return sha1.New() },
}
hashSize = sha1.Size
)
type TripleStore struct { type TripleStore struct {
session *mgo.Session session *mgo.Session
db *mgo.Database db *mgo.Database
hasherSize int idCache *IDLru
makeHasher func() hash.Hash
idCache *IDLru
} }
func createNewMongoGraph(addr string, options graph.Options) error { func createNewMongoGraph(addr string, options graph.Options) error {
@ -87,26 +93,26 @@ func newTripleStore(addr string, options graph.Options) (graph.TripleStore, erro
} }
qs.db = conn.DB(dbName) qs.db = conn.DB(dbName)
qs.session = conn qs.session = conn
qs.hasherSize = sha1.Size
qs.makeHasher = sha1.New
qs.idCache = NewIDLru(1 << 16) qs.idCache = NewIDLru(1 << 16)
return &qs, nil return &qs, nil
} }
func (qs *TripleStore) getIdForTriple(t quad.Quad) string { func (qs *TripleStore) getIdForTriple(t quad.Quad) string {
hasher := qs.makeHasher() id := qs.convertStringToByteHash(t.Subject)
id := qs.convertStringToByteHash(t.Subject, hasher) id += qs.convertStringToByteHash(t.Predicate)
id += qs.convertStringToByteHash(t.Predicate, hasher) id += qs.convertStringToByteHash(t.Object)
id += qs.convertStringToByteHash(t.Object, hasher) id += qs.convertStringToByteHash(t.Label)
id += qs.convertStringToByteHash(t.Label, hasher)
return id return id
} }
func (qs *TripleStore) convertStringToByteHash(s string, hasher hash.Hash) string { func (qs *TripleStore) convertStringToByteHash(s string) string {
hasher.Reset() h := hashPool.Get().(hash.Hash)
key := make([]byte, 0, qs.hasherSize) h.Reset()
hasher.Write([]byte(s)) defer hashPool.Put(h)
key = hasher.Sum(key)
key := make([]byte, 0, hashSize)
h.Write([]byte(s))
key = h.Sum(key)
return hex.EncodeToString(key) return hex.EncodeToString(key)
} }
@ -246,8 +252,7 @@ func (qs *TripleStore) TriplesAllIterator() graph.Iterator {
} }
func (qs *TripleStore) ValueOf(s string) graph.Value { func (qs *TripleStore) ValueOf(s string) graph.Value {
h := qs.makeHasher() return qs.convertStringToByteHash(s)
return qs.convertStringToByteHash(s, h)
} }
func (qs *TripleStore) NameOf(v graph.Value) string { func (qs *TripleStore) NameOf(v graph.Value) string {
@ -292,13 +297,13 @@ func (qs *TripleStore) TripleDirection(in graph.Value, d quad.Direction) graph.V
case quad.Subject: case quad.Subject:
offset = 0 offset = 0
case quad.Predicate: case quad.Predicate:
offset = (qs.hasherSize * 2) offset = (hashSize * 2)
case quad.Object: case quad.Object:
offset = (qs.hasherSize * 2) * 2 offset = (hashSize * 2) * 2
case quad.Label: case quad.Label:
offset = (qs.hasherSize * 2) * 3 offset = (hashSize * 2) * 3
} }
val := in.(string)[offset : qs.hasherSize*2+offset] val := in.(string)[offset : hashSize*2+offset]
return val return val
} }