From 8777321dd2e381fc58d4f55919789e462cd74b4d Mon Sep 17 00:00:00 2001 From: Aaron Boodman Date: Wed, 4 Jan 2017 14:15:26 -0800 Subject: [PATCH] Few cleanups to type simplification, plus enable it in various jobs (#3018) --- go/types/simplify.go | 4 ++-- go/types/simplify_test.go | 3 +++ go/types/type_cache.go | 5 +++-- samples/go/downloader/downloader.go | 1 + samples/go/face-merge/main.go | 1 + samples/go/photo-date-dedup/main.go | 1 + samples/go/photo-dedup/cmd/photo-dedup/main.go | 2 ++ samples/go/photo-dedup/cmd/photo-dhash/main.go | 2 ++ samples/go/photo-index/main.go | 1 + 9 files changed, 16 insertions(+), 4 deletions(-) diff --git a/go/types/simplify.go b/go/types/simplify.go index f688f1cad9..489d6d0eaa 100644 --- a/go/types/simplify.go +++ b/go/types/simplify.go @@ -35,8 +35,6 @@ import ( // Anytime any of the above cases generates a union as output, the same process // is applied to that union recursively. func makeSimplifiedUnion(in ...*Type) *Type { - d.Chk.True(len(in) > 0) - ts := make(typeset, len(in)) for _, t := range in { // De-cycle so that we handle cycles explicitly below. Otherwise, we would implicitly crawl @@ -135,6 +133,8 @@ func makeSimplifiedUnionImpl(in typeset) *Type { return out[0] } + staticTypeCache.Lock() + defer staticTypeCache.Unlock() return staticTypeCache.makeUnionType(out...) } diff --git a/go/types/simplify_test.go b/go/types/simplify_test.go index 37249785b4..4f3c89113e 100644 --- a/go/types/simplify_test.go +++ b/go/types/simplify_test.go @@ -148,6 +148,9 @@ func TestMakeSimplifiedUnion(t *testing.T) { in []*Type out *Type }{ + // {} -> + {[]*Type{}, + MakeUnionType()}, // {bool} -> bool {[]*Type{BoolType}, BoolType}, diff --git a/go/types/type_cache.go b/go/types/type_cache.go index cb97f6f783..5582df8a3c 100644 --- a/go/types/type_cache.go +++ b/go/types/type_cache.go @@ -539,11 +539,12 @@ func MakeStructType(name string, fieldNames []string, fieldTypes []*Type) *Type } func MakeUnionType(elemTypes ...*Type) *Type { - staticTypeCache.Lock() - defer staticTypeCache.Unlock() if enableTypeSimplification { return makeSimplifiedUnion(elemTypes...) } + + staticTypeCache.Lock() + defer staticTypeCache.Unlock() return staticTypeCache.makeUnionType(elemTypes...) } diff --git a/samples/go/downloader/downloader.go b/samples/go/downloader/downloader.go index 8cca05b1e8..13eed31bde 100644 --- a/samples/go/downloader/downloader.go +++ b/samples/go/downloader/downloader.go @@ -55,6 +55,7 @@ func download() (win bool) { var cacheDsArg = flag.String("cache-ds", "", "name of photo-cache dataset") var concurrencyArg = flag.Uint("concurrency", 4, "number of concurrent HTTP calls to retrieve remote resources") verbose.RegisterVerboseFlags(flag.CommandLine) + types.RegisterTypeSimplificationFlags(flag.CommandLine) flag.Usage = usage flag.Parse(false) diff --git a/samples/go/face-merge/main.go b/samples/go/face-merge/main.go index 11e9c17ee2..817211fb49 100644 --- a/samples/go/face-merge/main.go +++ b/samples/go/face-merge/main.go @@ -37,6 +37,7 @@ func mergeFaces() { var dbStr = flag.String("db", "", "input database spec") var outDSStr = flag.String("out-ds", "", "output dataset to write to") verbose.RegisterVerboseFlags(flag.CommandLine) + types.RegisterTypeSimplificationFlags(flag.CommandLine) flag.Usage = usage flag.Parse(false) diff --git a/samples/go/photo-date-dedup/main.go b/samples/go/photo-date-dedup/main.go index 7a1b9c767a..de4b2472f6 100644 --- a/samples/go/photo-date-dedup/main.go +++ b/samples/go/photo-date-dedup/main.go @@ -57,6 +57,7 @@ func index() (win bool) { var outDSStr = flag.String("out-ds", "", "output dataset to write to - if empty, defaults to input dataset") var thresh = flag.Int("threshold", 5000, "Number of milliseconds within which to consider photos duplicates") verbose.RegisterVerboseFlags(flag.CommandLine) + types.RegisterTypeSimplificationFlags(flag.CommandLine) flag.Usage = usage flag.Parse(true) diff --git a/samples/go/photo-dedup/cmd/photo-dedup/main.go b/samples/go/photo-dedup/cmd/photo-dedup/main.go index 3ed002db71..3983e909b8 100644 --- a/samples/go/photo-dedup/cmd/photo-dedup/main.go +++ b/samples/go/photo-dedup/cmd/photo-dedup/main.go @@ -12,6 +12,7 @@ import ( "github.com/attic-labs/noms/go/config" "github.com/attic-labs/noms/go/datas" "github.com/attic-labs/noms/go/spec" + "github.com/attic-labs/noms/go/types" "github.com/attic-labs/noms/go/util/exit" "github.com/attic-labs/noms/go/util/verbose" "github.com/attic-labs/noms/samples/go/photo-dedup/job" @@ -34,6 +35,7 @@ func main() { var outDSStr = flag.String("out-ds", "", "output dataset to write to") var threshold = flag.Int("threshold", 10, "photo's whose dhash distance is < threshold are grouped together") verbose.RegisterVerboseFlags(flag.CommandLine) + types.RegisterTypeSimplificationFlags(flag.CommandLine) flag.Usage = usage flag.Parse(false) diff --git a/samples/go/photo-dedup/cmd/photo-dhash/main.go b/samples/go/photo-dedup/cmd/photo-dhash/main.go index 0426842af7..513289348d 100644 --- a/samples/go/photo-dedup/cmd/photo-dhash/main.go +++ b/samples/go/photo-dedup/cmd/photo-dhash/main.go @@ -12,6 +12,7 @@ import ( "github.com/attic-labs/noms/go/config" "github.com/attic-labs/noms/go/datas" "github.com/attic-labs/noms/go/spec" + "github.com/attic-labs/noms/go/types" "github.com/attic-labs/noms/go/util/exit" "github.com/attic-labs/noms/go/util/verbose" "github.com/attic-labs/noms/samples/go/photo-dedup/job" @@ -34,6 +35,7 @@ func main() { var dbStr = flag.String("db", "", "input database spec") var outDSStr = flag.String("out-ds", "", "output dataset to write to") verbose.RegisterVerboseFlags(flag.CommandLine) + types.RegisterTypeSimplificationFlags(flag.CommandLine) flag.Usage = usage flag.Parse(false) diff --git a/samples/go/photo-index/main.go b/samples/go/photo-index/main.go index 846e7891b6..d7f4af0b1b 100644 --- a/samples/go/photo-index/main.go +++ b/samples/go/photo-index/main.go @@ -64,6 +64,7 @@ func index() (win bool) { var outDSStr = flag.String("out-ds", "", "output dataset to write to - if empty, defaults to input dataset") var indexCovers = flag.Bool("index-covers", false, "the resulting index will contain only the cover Photo, not the entire PhotoGroup") verbose.RegisterVerboseFlags(flag.CommandLine) + types.RegisterTypeSimplificationFlags(flag.CommandLine) flag.Usage = usage flag.Parse(false)