Skip to content

Commit

Permalink
optimize engine.AddEntry(...) to add multiple entries in one call
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Feb 15, 2023
1 parent 3bcea7c commit be6eee4
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 38 deletions.
6 changes: 3 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func (f optFunc) apply(opts *InitOpts) {
type Parser interface {
Name() string
Init(opts *ParserOpts) error
Parse(entries []interface{}) (Descriptors, error)
ParseContext(ctx MirCtx, entries []interface{})
Parse(entries []any) (Descriptors, error)
ParseContext(ctx MirCtx, entries []any)
Clone() Parser
}

Expand Down Expand Up @@ -274,7 +274,7 @@ func DefaultParser() Parser {
}

// Logus print log info
func Logus(format string, v ...interface{}) {
func Logus(format string, v ...any) {
if inDebug {
log.Printf("[mir] "+format, v...)
}
Expand Down
21 changes: 11 additions & 10 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,28 @@ import (

var (
mu = &sync.Mutex{}
mirEntries = make([]interface{}, 0, 8)
mirEntries = make([]any, 0, 8)
)

// AddEntry add mir's entry
func AddEntry(entry interface{}) {
// AddEntry add mir's entry list.
func AddEntry(entries ...any) {
mu.Lock()
defer mu.Unlock()

mirEntries = append(mirEntries, entry)
addEntries(entries...)
}

// AddEntries add mir's entry list
func AddEntries(entries ...interface{}) {
// AddEntries add mir's entry list.
// Deprecated use AddEntry(...) instead.
func AddEntries(entries ...any) {
mu.Lock()
defer mu.Unlock()

addEntries(entries...)
}

// Generate generate interface code from mir's iface entry
func Generate(opts core.Options, entries ...interface{}) (err error) {
func Generate(opts core.Options, entries ...any) (err error) {
mu.Lock()
defer mu.Unlock()

Expand Down Expand Up @@ -73,19 +74,19 @@ func Generate(opts core.Options, entries ...interface{}) (err error) {
return err
}

func addEntries(entries ...interface{}) {
func addEntries(entries ...any) {
mirEntries = append(mirEntries, entries...)
}

func doInSerial(p core.Parser, g core.Generator, entries []interface{}) error {
func doInSerial(p core.Parser, g core.Generator, entries []any) error {
descriptors, err := p.Parse(entries)
if err == nil {
return g.Generate(descriptors)
}
return err
}

func doInConcurrent(p core.Parser, g core.Generator, entries []interface{}) error {
func doInConcurrent(p core.Parser, g core.Generator, entries []any) error {
numCPU := runtime.NumCPU()
runtime.GOMAXPROCS(numCPU)
core.Logus("set GOMAXPROCS: %d", numCPU)
Expand Down
6 changes: 3 additions & 3 deletions internal/generator/templates/fiber_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
{{if notEmptyStr .Comment }}// {{.Comment}}{{end}}
type {{.TypeName}} interface {
{{if notEmptyStr .Chain }}// Chain provide handlers chain for fiber
{{.Chain}}() []interface{}
{{.Chain}}() []any
{{end}}
{{range .Fields}} {{.MethodName}}({{if notEmptyStr .InName }}*{{ .InName }}{{end}}) {{if notEmptyStr .OutName }}(*{{ .OutName}}, mir.Error){{else}}mir.Error{{end}}
{{end}}
Expand Down Expand Up @@ -91,8 +91,8 @@ type {{$unimplementedServant}} struct {
}

{{if notEmptyStr .Chain }}
func ({{$unimplementedServant}}){{.Chain}}() []interface{} {
return []interface{}{middleware.Logger()}
func ({{$unimplementedServant}}){{.Chain}}() []any {
return []any{middleware.Logger()}
}

{{end}}
Expand Down
6 changes: 3 additions & 3 deletions internal/generator/templates/fiber_iface_v2.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
{{if notEmptyStr .Comment }}// {{.Comment}}{{end}}
type {{.TypeName}} interface {
{{if notEmptyStr .Chain }}// Chain provide handlers chain for fiber
{{.Chain}}() []interface{}
{{.Chain}}() []any
{{end}}
{{range .Fields}} {{.MethodName}}({{if notEmptyStr .InName }}*{{ .InName }}{{end}}) {{if notEmptyStr .OutName }}(*{{ .OutName}}, mir.Error){{else}}mir.Error{{end}}
{{end}}
Expand Down Expand Up @@ -94,8 +94,8 @@ type {{$unimplementedServant}} struct {
}

{{if notEmptyStr .Chain }}
func ({{$unimplementedServant}}){{.Chain}}() []interface{} {
return []interface{}{logger.New()}
func ({{$unimplementedServant}}){{.Chain}}() []any {
return []any{logger.New()}
}

{{end}}
Expand Down
6 changes: 3 additions & 3 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (p *mirParser) Init(opts *core.ParserOpts) error {
}

// Parse serial parse interface defined object entries
func (p *mirParser) Parse(entries []interface{}) (core.Descriptors, error) {
func (p *mirParser) Parse(entries []any) (core.Descriptors, error) {
if len(entries) == 0 {
return nil, errors.New("entries is empty")
}
Expand All @@ -66,14 +66,14 @@ func (p *mirParser) Parse(entries []interface{}) (core.Descriptors, error) {
}

// ParseContext concurrent parse interface defined object entries
func (p *mirParser) ParseContext(ctx core.MirCtx, entries []interface{}) {
func (p *mirParser) ParseContext(ctx core.MirCtx, entries []any) {
_, ifaceSink := ctx.Pipe()
muxSet := utils.NewMuxSet()

wg := &sync.WaitGroup{}
for _, entry := range entries {
wg.Add(1)
go func(ctx core.MirCtx, wg *sync.WaitGroup, ifaceSink chan<- *core.IfaceDescriptor, entry interface{}) {
go func(ctx core.MirCtx, wg *sync.WaitGroup, ifaceSink chan<- *core.IfaceDescriptor, entry any) {
defer wg.Done()

r := reflex.NewReflex(p.engineInfo, p.tagName, p.watchCtxDone, p.noneQuery)
Expand Down
2 changes: 1 addition & 1 deletion internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type siteV2 struct {
func TestMirParser_Parse(t *testing.T) {
p := &mirParser{tagName: defaultTag}

entries := []interface{}{
entries := []any{
new(site),
new(siteV1),
new(siteV2),
Expand Down
2 changes: 1 addition & 1 deletion internal/reflex/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (r *reflex) inflateQuery(qs string) []string {
}

// valueByName return field value by field name
func (r *reflex) valueByName(value reflect.Value, name string) interface{} {
func (r *reflex) valueByName(value reflect.Value, name string) any {
if fieldValue := value.FieldByName(name); !fieldValue.IsNil() {
return fieldValue.Elem().Interface()
}
Expand Down
4 changes: 2 additions & 2 deletions internal/reflex/reflex.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type reflex struct {

// Parse get Descriptors from parse entries
// Notice: Descriptors may be an empty if no actual item and is not routine safe
func (r *reflex) Parse(entries []interface{}) (core.Descriptors, error) {
func (r *reflex) Parse(entries []any) (core.Descriptors, error) {
ds := make(core.Descriptors)
for _, entry := range entries {
iface, err := r.IfaceFrom(entry)
Expand All @@ -41,7 +41,7 @@ func (r *reflex) Parse(entries []interface{}) (core.Descriptors, error) {
return ds, nil
}

func (r *reflex) IfaceFrom(entry interface{}) (*core.IfaceDescriptor, error) {
func (r *reflex) IfaceFrom(entry any) (*core.IfaceDescriptor, error) {
// used to find tagInfo
entryType := reflect.TypeOf(entry)
if entryType == nil {
Expand Down
6 changes: 3 additions & 3 deletions mirc/templates/fiber-v2/mirc/auto/api/site.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type Tweet struct {

type Site interface {
// Chain provide handlers chain for fiber
Chain() []interface{}
Chain() []any

Logout(*LogoutReq) mir.Error
Login(*LoginReq) (*LoginResp, mir.Error)
Expand Down Expand Up @@ -160,8 +160,8 @@ func RegisterSiteServant(app *fiber.App, s Site, b SiteBinding, r SiteRender) {
type UnimplementedSiteServant struct {
}

func (UnimplementedSiteServant) Chain() []interface{} {
return []interface{}{logger.New()}
func (UnimplementedSiteServant) Chain() []any {
return []any{logger.New()}
}

func (UnimplementedSiteServant) Logout(req *LogoutReq) mir.Error {
Expand Down
6 changes: 3 additions & 3 deletions mirc/templates/fiber-v2/mirc/auto/api/v1/site.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type Site interface {
// Chain provide handlers chain for fiber
Chain() []interface{}
Chain() []any

Articles() mir.Error
Index() mir.Error
Expand Down Expand Up @@ -56,8 +56,8 @@ func RegisterSiteServant(app *fiber.App, s Site, r SiteRender) {
type UnimplementedSiteServant struct {
}

func (UnimplementedSiteServant) Chain() []interface{} {
return []interface{}{logger.New()}
func (UnimplementedSiteServant) Chain() []any {
return []any{logger.New()}
}

func (UnimplementedSiteServant) Articles() mir.Error {
Expand Down
6 changes: 3 additions & 3 deletions mirc/templates/fiber/mirc/auto/api/site.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type Tweet struct {

type Site interface {
// Chain provide handlers chain for fiber
Chain() []interface{}
Chain() []any

Logout(*LogoutReq) mir.Error
Login(*LoginReq) (*LoginResp, mir.Error)
Expand Down Expand Up @@ -154,8 +154,8 @@ func RegisterSiteServant(app *fiber.App, s Site, b SiteBinding, r SiteRender) {
type UnimplementedSiteServant struct {
}

func (UnimplementedSiteServant) Chain() []interface{} {
return []interface{}{middleware.Logger()}
func (UnimplementedSiteServant) Chain() []any {
return []any{middleware.Logger()}
}

func (UnimplementedSiteServant) Logout(req *LogoutReq) mir.Error {
Expand Down
6 changes: 3 additions & 3 deletions mirc/templates/fiber/mirc/auto/api/v1/site.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type Site interface {
// Chain provide handlers chain for fiber
Chain() []interface{}
Chain() []any

Articles() mir.Error
Index() mir.Error
Expand Down Expand Up @@ -54,8 +54,8 @@ func RegisterSiteServant(app *fiber.App, s Site, r SiteRender) {
type UnimplementedSiteServant struct {
}

func (UnimplementedSiteServant) Chain() []interface{} {
return []interface{}{middleware.Logger()}
func (UnimplementedSiteServant) Chain() []any {
return []any{middleware.Logger()}
}

func (UnimplementedSiteServant) Articles() mir.Error {
Expand Down

0 comments on commit be6eee4

Please sign in to comment.