mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
don't create a new controller instance if it doesn't have struct dependencies and the fields length is 0 - 0.4MB/s difference from the raw handlers now.
Former-commit-id: f808291fe84bc2cdd83f60f62f8b3140204110a5
This commit is contained in:
parent
34664aa311
commit
a25c0557de
|
@ -5,7 +5,7 @@ package main
|
||||||
// with bindings or without).
|
// with bindings or without).
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris/_benchmarks/iris-mvc2/controllers"
|
"github.com/kataras/iris/_benchmarks/iris-mvc/controllers"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/mvc"
|
"github.com/kataras/iris/mvc"
|
||||||
|
@ -16,3 +16,5 @@ func main() {
|
||||||
mvc.New(app.Party("/api/values/{id}")).Register(new(controllers.ValuesController))
|
mvc.New(app.Party("/api/values/{id}")).Register(new(controllers.ValuesController))
|
||||||
app.Run(iris.Addr(":5000"), iris.WithoutVersionChecker)
|
app.Run(iris.Addr(":5000"), iris.WithoutVersionChecker)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// +2MB/s faster than the previous implementation, 0.4MB/s difference from the raw handlers.
|
||||||
|
|
|
@ -58,6 +58,8 @@ func getNameOf(typ reflect.Type) string {
|
||||||
return fullname
|
return fullname
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// TODO: activate controllers with go routines so the startup time of iris
|
||||||
|
// can be improved on huge applications.
|
||||||
func newControllerActivator(router router.Party, controller interface{}, d *di.D) *ControllerActivator {
|
func newControllerActivator(router router.Party, controller interface{}, d *di.D) *ControllerActivator {
|
||||||
var (
|
var (
|
||||||
val = reflect.ValueOf(controller)
|
val = reflect.ValueOf(controller)
|
||||||
|
@ -215,20 +217,125 @@ func (c *ControllerActivator) Handle(method, path, funcName string, middleware .
|
||||||
if c.injector == nil {
|
if c.injector == nil {
|
||||||
c.injector = c.Dependencies.Struct(c.Value)
|
c.injector = c.Dependencies.Struct(c.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handler := buildHandler(m, c.Type, c.Value, c.injector, funcInjector, funcIn)
|
||||||
|
|
||||||
|
// register the handler now.
|
||||||
|
route := c.Router.Handle(method, path, append(middleware, handler)...)
|
||||||
|
if route != nil {
|
||||||
|
// change the main handler's name in order to respect the controller's and give
|
||||||
|
// a proper debug message.
|
||||||
|
route.MainHandlerName = fmt.Sprintf("%s.%s", c.FullName, funcName)
|
||||||
|
}
|
||||||
|
|
||||||
|
return route
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildHandler has many many dublications but we do that to achieve the best
|
||||||
|
// performance possible, to use the information we know
|
||||||
|
// and calculate what is needed and what not in serve-time.
|
||||||
|
func buildHandler(m reflect.Method, typ reflect.Type, initRef reflect.Value, structInjector *di.StructInjector, funcInjector *di.FuncInjector, funcIn []reflect.Type) context.Handler {
|
||||||
var (
|
var (
|
||||||
hasStructInjector = c.injector != nil && c.injector.Valid
|
hasStructInjector = structInjector != nil && structInjector.Valid
|
||||||
hasFuncInjector = funcInjector != nil && funcInjector.Valid
|
hasFuncInjector = funcInjector != nil && funcInjector.Valid
|
||||||
|
|
||||||
implementsBase = isBaseController(c.Type)
|
implementsBase = isBaseController(typ)
|
||||||
// we will make use of 'n' to make a slice of reflect.Value
|
// we will make use of 'n' to make a slice of reflect.Value
|
||||||
// to pass into if the function has input arguments that
|
// to pass into if the function has input arguments that
|
||||||
// are will being filled by the funcDependencies.
|
// are will being filled by the funcDependencies.
|
||||||
n = len(funcIn)
|
n = len(funcIn)
|
||||||
|
|
||||||
elemTyp = di.IndirectType(c.Type)
|
elemTyp = di.IndirectType(typ)
|
||||||
)
|
)
|
||||||
|
|
||||||
handler := func(ctx context.Context) {
|
// if it doesn't implements the base controller,
|
||||||
|
// it may have struct injector and/or func injector.
|
||||||
|
if !implementsBase {
|
||||||
|
|
||||||
|
if !hasStructInjector {
|
||||||
|
// if the controller doesn't have a struct injector
|
||||||
|
// and the controller's fields are empty
|
||||||
|
// then we don't need a new controller instance, we use the passed controller instance.
|
||||||
|
if elemTyp.NumField() == 0 {
|
||||||
|
if !hasFuncInjector {
|
||||||
|
return func(ctx context.Context) {
|
||||||
|
DispatchFuncResult(ctx, initRef.Method(m.Index).Call(emptyIn))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(ctx context.Context) {
|
||||||
|
in := make([]reflect.Value, n, n)
|
||||||
|
in[0] = initRef
|
||||||
|
funcInjector.Inject(&in, reflect.ValueOf(ctx))
|
||||||
|
if ctx.IsStopped() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
DispatchFuncResult(ctx, m.Func.Call(in))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// it has fields, so it's request-scoped, even without struct injector
|
||||||
|
// it's safe to create a new controller on each request because the end-dev
|
||||||
|
// may use the controller's fields for request-scoping, so they should be
|
||||||
|
// zero on the next request.
|
||||||
|
if !hasFuncInjector {
|
||||||
|
return func(ctx context.Context) {
|
||||||
|
DispatchFuncResult(ctx, reflect.New(elemTyp).Method(m.Index).Call(emptyIn))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return func(ctx context.Context) {
|
||||||
|
in := make([]reflect.Value, n, n)
|
||||||
|
in[0] = reflect.New(elemTyp)
|
||||||
|
funcInjector.Inject(&in, reflect.ValueOf(ctx))
|
||||||
|
if ctx.IsStopped() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
DispatchFuncResult(ctx, m.Func.Call(in))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// it has struct injector for sure and maybe a func injector.
|
||||||
|
if !hasFuncInjector {
|
||||||
|
return func(ctx context.Context) {
|
||||||
|
ctrl := reflect.New(elemTyp)
|
||||||
|
ctxValue := reflect.ValueOf(ctx)
|
||||||
|
elem := ctrl.Elem()
|
||||||
|
structInjector.InjectElem(elem, ctxValue)
|
||||||
|
if ctx.IsStopped() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
DispatchFuncResult(ctx, ctrl.Method(m.Index).Call(emptyIn))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// has struct injector and func injector.
|
||||||
|
return func(ctx context.Context) {
|
||||||
|
ctrl := reflect.New(elemTyp)
|
||||||
|
ctxValue := reflect.ValueOf(ctx)
|
||||||
|
|
||||||
|
elem := ctrl.Elem()
|
||||||
|
structInjector.InjectElem(elem, ctxValue)
|
||||||
|
if ctx.IsStopped() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
in := make([]reflect.Value, n, n)
|
||||||
|
in[0] = ctrl
|
||||||
|
funcInjector.Inject(&in, ctxValue)
|
||||||
|
if ctx.IsStopped() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
DispatchFuncResult(ctx, m.Func.Call(in))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// if implements the base controller,
|
||||||
|
// it may have struct injector and func injector as well.
|
||||||
|
return func(ctx context.Context) {
|
||||||
ctrl := reflect.New(elemTyp)
|
ctrl := reflect.New(elemTyp)
|
||||||
|
|
||||||
if implementsBase {
|
if implementsBase {
|
||||||
|
@ -251,7 +358,7 @@ func (c *ControllerActivator) Handle(method, path, funcName string, middleware .
|
||||||
ctxValue := reflect.ValueOf(ctx)
|
ctxValue := reflect.ValueOf(ctx)
|
||||||
if hasStructInjector {
|
if hasStructInjector {
|
||||||
elem := ctrl.Elem()
|
elem := ctrl.Elem()
|
||||||
c.injector.InjectElem(elem, ctxValue)
|
structInjector.InjectElem(elem, ctxValue)
|
||||||
if ctx.IsStopped() {
|
if ctx.IsStopped() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -277,14 +384,4 @@ func (c *ControllerActivator) Handle(method, path, funcName string, middleware .
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// register the handler now.
|
|
||||||
route := c.Router.Handle(method, path, append(middleware, handler)...)
|
|
||||||
if route != nil {
|
|
||||||
// change the main handler's name in order to respect the controller's and give
|
|
||||||
// a proper debug message.
|
|
||||||
route.MainHandlerName = fmt.Sprintf("%s.%s", c.FullName, funcName)
|
|
||||||
}
|
|
||||||
|
|
||||||
return route
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -231,7 +231,7 @@ func (p *methodParser) parsePathParam(path string, w string, funcArgPos int) (st
|
||||||
// so retry with the "funcArgPos" incremented.
|
// so retry with the "funcArgPos" incremented.
|
||||||
//
|
//
|
||||||
// the "funcArgPos" will be updated to the caller as well
|
// the "funcArgPos" will be updated to the caller as well
|
||||||
// because we return it as well.
|
// because we return it among the path and the error.
|
||||||
return p.parsePathParam(path, w, funcArgPos+1)
|
return p.parsePathParam(path, w, funcArgPos+1)
|
||||||
}
|
}
|
||||||
return "", 0, errors.New("invalid syntax for " + p.fn.Name)
|
return "", 0, errors.New("invalid syntax for " + p.fn.Name)
|
||||||
|
|
16
mvc/param.go
16
mvc/param.go
|
@ -27,25 +27,11 @@ func getPathParamsForInput(params []macro.TemplateParam, funcIn ...reflect.Type)
|
||||||
// fmt.Printf("%s input arg type vs %s param type\n", in.Kind().String(), p.Type.Kind().String())
|
// fmt.Printf("%s input arg type vs %s param type\n", in.Kind().String(), p.Type.Kind().String())
|
||||||
if paramType.Assignable(in.Kind()) {
|
if paramType.Assignable(in.Kind()) {
|
||||||
consumedParams[j] = true
|
consumedParams[j] = true
|
||||||
// fmt.Printf("path_param_binder.go: bind path param func for paramName = '%s' and paramType = '%s'\n", paramName, paramType.String())
|
// fmt.Printf("param.go: bind path param func for paramName = '%s' and paramType = '%s'\n", paramName, paramType.String())
|
||||||
values = append(values, makeFuncParamGetter(paramType, paramName))
|
values = append(values, makeFuncParamGetter(paramType, paramName))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// funcInIdx := 0
|
|
||||||
// // it's a valid param type.
|
|
||||||
// for _, p := range params {
|
|
||||||
// in := funcIn[funcInIdx]
|
|
||||||
// paramType := p.Type
|
|
||||||
// paramName := p.Name
|
|
||||||
// // fmt.Printf("%s input arg type vs %s param type\n", in.Kind().String(), p.Type.Kind().String())
|
|
||||||
// if paramType.Assignable(in.Kind()) {
|
|
||||||
// // fmt.Printf("path_param_binder.go: bind path param func for paramName = '%s' and paramType = '%s'\n", paramName, paramType.String())
|
|
||||||
// values = append(values, makeFuncParamGetter(paramType, paramName))
|
|
||||||
// }
|
|
||||||
|
|
||||||
// funcInIdx++
|
|
||||||
// }
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user