Avoid Excess Ref Computation

This commit is contained in:
Rafael Weinstein
2015-09-26 11:33:04 -07:00
parent 729327b8b5
commit 6c5b2eb6f4
5 changed files with 18 additions and 62 deletions

View File

@@ -1,49 +0,0 @@
package chunks
import (
"flag"
"github.com/attic-labs/noms/d"
"github.com/attic-labs/noms/ref"
)
// NopStore is a no-write implementation of chunks.ChunkStore.
// Get() doesn't work, because the data isn't ever written, but other stuff works
type NopStore struct {
memoryRootTracker
}
// Get panics in NopStore! Since the data wasn't stored, you sure can't Get it.
func (ms *NopStore) Get(ref ref.Ref) Chunk {
d.Chk.Fail("Whoops, you shouldn't have called this!")
return EmptyChunk
}
func (ms *NopStore) Has(ref ref.Ref) bool {
return false
}
func (ms *NopStore) Put(c Chunk) {
}
func (ms *NopStore) Close() error {
return nil
}
type NopStoreFlags struct {
use *bool
}
func NopFlags(prefix string) NopStoreFlags {
return NopStoreFlags{
flag.Bool(prefix+"nop", false, "use a /dev/null-esque chunkstore"),
}
}
func (f NopStoreFlags) CreateStore() ChunkStore {
if *f.use {
return &NopStore{}
} else {
return nil
}
}

View File

@@ -32,7 +32,6 @@ func NewDataStore(cs chunks.ChunkStore) DataStore {
type Flags struct {
ldb chunks.LevelDBStoreFlags
memory chunks.MemoryStoreFlags
nop chunks.NopStoreFlags
hflags chunks.HttpStoreFlags
}
@@ -44,7 +43,6 @@ func NewFlagsWithPrefix(prefix string) Flags {
return Flags{
chunks.LevelDBFlags(prefix),
chunks.MemoryFlags(prefix),
chunks.NopFlags(prefix),
chunks.HttpFlags(prefix),
}
}
@@ -53,7 +51,6 @@ func (f Flags) CreateDataStore() (DataStore, bool) {
var cs chunks.ChunkStore
if cs = f.ldb.CreateStore(); cs != nil {
} else if cs = f.memory.CreateStore(); cs != nil {
} else if cs = f.nop.CreateStore(); cs != nil {
}
if cs != nil {

View File

@@ -13,12 +13,10 @@ import (
func TestJSONDecode(t *testing.T) {
assert := assert.New(t)
cs := &chunks.NopStore{}
put := func(s string) ref.Ref {
s += "\n"
c := chunks.NewChunk([]byte(s))
cs.Put(c)
return c.Ref()
}

View File

@@ -1,9 +1,6 @@
package types
import (
"github.com/attic-labs/noms/chunks"
"github.com/attic-labs/noms/ref"
)
import "github.com/attic-labs/noms/ref"
var getRefOverride func(v Value) ref.Ref
@@ -15,7 +12,7 @@ func getRef(v Value) ref.Ref {
}
func getRefNoOverride(v Value) ref.Ref {
return WriteValue(v, &chunks.NopStore{})
return writeValueInternal(v, nil)
}
func ensureRef(r *ref.Ref, v Value) ref.Ref {

View File

@@ -13,12 +13,25 @@ type primitive interface {
func WriteValue(v Value, cs chunks.ChunkSink) ref.Ref {
d.Chk.NotNil(cs)
return writeValueInternal(v, cs)
}
func writeChildValueInternal(v Value, cs chunks.ChunkSink) ref.Ref {
if cs == nil {
return v.Ref()
}
return writeValueInternal(v, cs)
}
func writeValueInternal(v Value, cs chunks.ChunkSink) ref.Ref {
e := toEncodeable(v, cs)
w := chunks.NewChunkWriter()
enc.Encode(w, e)
c := w.Chunk()
cs.Put(c)
if cs != nil {
cs.Put(c)
}
return c.Ref()
}
@@ -98,7 +111,7 @@ func makeTypeEncodeable(t TypeRef, cs chunks.ChunkSink) interface{} {
pkgRef := t.PackageRef().Ref()
p := LookupPackage(pkgRef)
if p != nil {
pkgRef = WriteValue(p.NomsValue(), cs)
pkgRef = writeChildValueInternal(p.NomsValue(), cs)
}
return enc.TypeRef{PkgRef: pkgRef, Name: t.Name(), Kind: uint8(t.Kind()), Desc: toEncodeable(t.Desc.ToValue(), cs)}
}
@@ -113,7 +126,7 @@ func processChild(f Future, cs chunks.ChunkSink) interface{} {
switch v := v.(type) {
// Blobs, lists, maps, and sets are always out-of-line
case Blob, List, Map, Set:
return WriteValue(v, cs)
return writeChildValueInternal(v, cs)
default:
// Other types are always inline.
return toEncodeable(v, cs)