Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use go:generate and stringer to replace maps with arrays for performance #27

Merged
merged 3 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ linters:
enable:
- megacheck
- govet
- exportloopref
- errcheck
enable-all: false
disable:
- maligned
Expand All @@ -92,6 +94,7 @@ linters:
- funlen
- gocognit
- gocyclo
- scopelint
presets:
- bugs
- unused
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


all:
go install golang.org/x/tools/...@latest
go generate
go test
golangci-lint run
6 changes: 0 additions & 6 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ func doBind(sc *Collection, originalInvokeF *provider, originalInitF *provider,
if err != nil {
return err
}
if invokeF.flows == nil {
return fmt.Errorf("internal error #4: no flows for invoke")
}
nonStaticTypes := make(map[typeCode]bool)
for _, tc := range invokeF.flows[outputParams] {
nonStaticTypes[tc] = true
Expand Down Expand Up @@ -59,9 +56,6 @@ func doBind(sc *Collection, originalInvokeF *provider, originalInitF *provider,
if err != nil {
return err
}
if initF.flows == nil {
return fmt.Errorf("internal error #5: no flows for initF")
}
funcs = append(funcs, initF)
}

Expand Down
4 changes: 2 additions & 2 deletions characterize.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type charContext struct {
inputsAreStatic bool
}

type flowMapType map[flowType][]typeCode
type flowMapType [bypassParams + 1][]typeCode

type characterization struct {
name string
Expand Down Expand Up @@ -525,7 +525,7 @@ Match:
}
a.fm.upRmap = make(map[typeCode]typeCode)
a.fm.downRmap = make(map[typeCode]typeCode)
a.fm.flows = make(flowMapType)
a.fm.flows = [lastFlowType][]typeCode{}
match.mutate(a)
return a.fm, nil
}
Expand Down
26 changes: 13 additions & 13 deletions characterize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type doesJ struct {
func (dj *doesJ) I() int { return dj.j * 3 }

func params() flowMapType {
return make(flowMapType)
return [bypassParams + 1][]typeCode{}
}

func (flows flowMapType) returns(f ...typeCode) flowMapType {
Expand Down Expand Up @@ -185,21 +185,21 @@ var characterizeTests = []struct {
func() {},
injectorFunc,
lastF, staticF, panicF,
nil,
params(),
},
{
"cacheable notCacheable",
Cacheable(NotCacheable(func() {})),
injectorFunc,
lastF, staticT, panicF,
nil,
params(),
},
{
"cacheable notCacheable",
Cacheable(NotCacheable(func() {})),
injectorFunc,
lastF, staticF, panicF,
nil,
params(),
},
{
"minimal fallible injector",
Expand Down Expand Up @@ -244,14 +244,14 @@ var characterizeTests = []struct {
func(int) func() { return func() {} },
finalFunc,
lastT, staticF, panicT,
nil,
params(),
},
{
"invalid: anonymous func that isn't a wrap #2",
func(func(), int) {},
finalFunc,
lastT, staticF, panicT,
nil,
params(),
},
{
"middleware func",
Expand Down Expand Up @@ -296,14 +296,14 @@ var characterizeTests = []struct {
&nadaFunc,
invokeFunc,
lastF, staticF, panicF,
nil,
params(),
},
{
"init: nada",
&nadaFunc,
initFunc,
lastF, staticT, panicF,
nil,
params(),
},
{
"literal: int",
Expand All @@ -317,7 +317,7 @@ var characterizeTests = []struct {
"foobar",
literalValue,
lastF, staticF, panicT,
nil,
params(),
},
}

Expand Down Expand Up @@ -389,13 +389,13 @@ func TestCharacterize(t *testing.T) {
}
}
for ft, ev := range test.flows {
t.Logf("flow %s: %s", ft, een(ev))
assert.EqualValues(t, een(ev), een(fm.flows[ft]), fmt.Sprintf("%s flow: %s", ft, test.name))
t.Logf("flow %s: %s", flowType(ft), een(ev))
assert.EqualValues(t, een(ev), een(fm.flows[ft]), fmt.Sprintf("%s flow: %s", flowType(ft), test.name))
}
for ft, gv := range fm.flows {
if test.flows[ft] == nil {
t.Logf("flow %s: %s", ft, een(gv))
assert.EqualValues(t, een(test.flows[ft]), een(gv), fmt.Sprintf("%s flow %s", ft, test.name))
t.Logf("flow %s: %s", flowType(ft), een(gv))
assert.EqualValues(t, een(test.flows[ft]), een(gv), fmt.Sprintf("%s flow %s", flowType(ft), test.name))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func dumpF(context string, fm *provider) {
out += fmt.Sprintf("\n\tclass: %s\n\tgroup: %s", fm.class, fm.group)
for name, flow := range fm.flows {
if len(flow) > 0 {
out += fmt.Sprintf("\n\t%s flow: %s", name, formatFlow(flow))
out += fmt.Sprintf("\n\t%s flow: %s", flowType(name), formatFlow(flow))
}
}
for upDown, rMap := range map[string]map[typeCode]typeCode{
Expand Down Expand Up @@ -168,7 +168,7 @@ func dumpF(context string, fm *provider) {
}
for param, users := range fm.d.usesDetail {
for tc, dep := range users {
out += fmt.Sprintf("\n\tUSES: %s (%s %s)", dep, param, tc)
out += fmt.Sprintf("\n\tUSES: %s (%s %s)", dep, flowType(param), tc)
}
}
for _, dep := range fm.d.usedBy {
Expand Down
7 changes: 3 additions & 4 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ func generateParameterMap(
vmap map[typeCode]int, // map of types in valueCollection to their positions
purpose string, // debug string
) (parameterMap, error) {
flow, found := fm.flows[param]
if !found {
return parameterMap{}, fm.errorf("internal error #12: flow %s missing", param)
}
flow := fm.flows[param]
m := make([]int, len(flow))
types := make([]reflect.Type, len(flow))
for i, p := range flow {
Expand Down Expand Up @@ -380,6 +377,8 @@ func generateWrappers(
case invokeFunc, initFunc, literalValue:
// handled elsewhere
return nil
case unsetClassType:
fallthrough
default:
return fmt.Errorf("internal error #11: unexpected class")
}
Expand Down
20 changes: 10 additions & 10 deletions include.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
)

type includeWorkingData struct {
usesDetail map[flowType]map[typeCode][]*provider
usesDetail [lastFlowType]map[typeCode][]*provider
uses []*provider
usesError map[flowType]map[typeCode]error
usesError [lastFlowType]map[typeCode]error
usedBy []*provider
usedByDetail map[flowType]map[typeCode][]*provider
mustConsumeFlow map[flowType]bool
usedByDetail [lastFlowType]map[typeCode][]*provider
mustConsumeFlow [lastFlowType]bool
excluded error
clusterMembers []*provider
wantedInCluster bool
Expand Down Expand Up @@ -78,7 +78,7 @@ func computeDependenciesAndInclusion(funcs []*provider, initF *provider) error {
fm.d.clusterMembers = []*provider{fm}
}
}
fm.d.mustConsumeFlow = make(map[flowType]bool)
fm.d.mustConsumeFlow = [lastFlowType]bool{}
if fm.mustConsume {
fm.d.mustConsumeFlow[outputParams] = true
}
Expand Down Expand Up @@ -276,7 +276,7 @@ func checkFlows(funcs []*provider, numFuncs int, canRemoveDesired bool) error {
debugf("\t\t\tcannot provide %s %s: %s: %s", param, tc, p, p.cannotInclude)
extra = fmt.Sprintf(" (not provided by %s because %s)", p, p.cannotInclude)
}
fm.cannotInclude = fmt.Errorf("no provider for %s in %s%s", tc, param, extra)
fm.cannotInclude = fmt.Errorf("no provider for %s in %s%s", tc, flowType(param), extra)
redo = append(redo, fm)
debugf("\t\tno source %s %s %s: %s", param, tc, fm, fm.cannotInclude)
continue Todo
Expand All @@ -299,7 +299,7 @@ func checkFlows(funcs []*provider, numFuncs int, canRemoveDesired bool) error {
debugf("\t\t\tcannot consume %s %s: %s: %s", param, tc, p, p.cannotInclude)
extra = fmt.Sprintf(" (not consumed by %s because %s)", p, p.cannotInclude)
}
fm.cannotInclude = fmt.Errorf("no consumer for %s in %s%s", tc, param, extra)
fm.cannotInclude = fmt.Errorf("no consumer for %s in %s%s", tc, flowType(param), extra)
redo = append(redo, fm)
debugf("\t\tnot consumed %s %s %s: %s", param, tc, fm, fm.cannotInclude)
continue Todo
Expand All @@ -317,10 +317,10 @@ func checkFlows(funcs []*provider, numFuncs int, canRemoveDesired bool) error {
func providesReturns(funcs []*provider, initF *provider) error {
debugln("calculating provides/returns")
for _, fm := range funcs {
fm.d.usedByDetail = make(map[flowType]map[typeCode][]*provider)
fm.d.usesDetail = make(map[flowType]map[typeCode][]*provider)
fm.d.usedByDetail = [lastFlowType]map[typeCode][]*provider{}
fm.d.usesDetail = [lastFlowType]map[typeCode][]*provider{}
fm.d.uses = nil
fm.d.usesError = make(map[flowType]map[typeCode]error)
fm.d.usesError = [lastFlowType]map[typeCode]error{}
fm.d.usedBy = nil
}
provide := make(interfaceMap)
Expand Down
4 changes: 2 additions & 2 deletions nject.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ func (fm *provider) String() string {
t = reflect.TypeOf(fm.fn).String()
}
class := ""
if fm.class != "" {
class = string(fm.class) + ": "
if fm.class != unsetClassType {
class = fm.class.String() + ": "
}
if fm.index >= 0 {
return fmt.Sprintf("%s%s(%d) [%s]", class, fm.origin, fm.index, t)
Expand Down
75 changes: 75 additions & 0 deletions stringer_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading