mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 02:47:04 +00:00
more checks about creating new instance of controller on each request - this time if all bindings are static then set them to the initial-devpassed controller and if the total number of lengths are equal with these static dependencies then we ignore the injector and use the initial controller on each request - maximize the performance when simple controller is used - need more cleanup before new release but I hope until Christmas iris developers will be amazed
Former-commit-id: 32ed69368d1df2c25cdb712bb7f0cf47b2e36c05
This commit is contained in:
16
mvc/di/di.go
16
mvc/di/di.go
@@ -44,19 +44,11 @@ func (d *D) GoodFunc(fn TypeChecker) *D {
|
||||
// Clone returns a new Dependency Injection container, it adopts the
|
||||
// parent's (current "D") hijacker, good func type checker and all dependencies values.
|
||||
func (d *D) Clone() *D {
|
||||
clone := New()
|
||||
clone.hijacker = d.hijacker
|
||||
clone.goodFunc = d.goodFunc
|
||||
|
||||
// copy the current dynamic bindings (func binders)
|
||||
// and static struct bindings (services) to this new child.
|
||||
if n := len(d.Values); n > 0 {
|
||||
values := make(Values, n, n)
|
||||
copy(values, d.Values)
|
||||
clone.Values = values
|
||||
return &D{
|
||||
Values: d.Values.Clone(),
|
||||
hijacker: d.hijacker,
|
||||
goodFunc: d.goodFunc,
|
||||
}
|
||||
|
||||
return clone
|
||||
}
|
||||
|
||||
// Struct is being used to return a new injector based on
|
||||
|
||||
@@ -125,6 +125,12 @@ type field struct {
|
||||
AnyValue reflect.Value
|
||||
}
|
||||
|
||||
// NumFields returns the total number of fields, and the embedded, even if the embedded struct is not exported,
|
||||
// it will check for its exported fields.
|
||||
func NumFields(elemTyp reflect.Type) int {
|
||||
return len(lookupFields(elemTyp, nil))
|
||||
}
|
||||
|
||||
func lookupFields(elemTyp reflect.Type, parentIndex []int) (fields []field) {
|
||||
if elemTyp.Kind() != reflect.Struct {
|
||||
return
|
||||
|
||||
@@ -97,6 +97,17 @@ func (s *StructInjector) InjectElem(destElem reflect.Value, ctx ...reflect.Value
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StructInjector) InjectElemStaticOnly(destElem reflect.Value) (n int) {
|
||||
for _, f := range s.fields {
|
||||
if f.Object.BindType != Static {
|
||||
continue
|
||||
}
|
||||
destElem.FieldByIndex(f.FieldIndex).Set(f.Object.Value)
|
||||
n++
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *StructInjector) New(ctx ...reflect.Value) reflect.Value {
|
||||
dest := reflect.New(s.elemType)
|
||||
s.InjectElem(dest, ctx...)
|
||||
|
||||
@@ -2,12 +2,35 @@ package di
|
||||
|
||||
import "reflect"
|
||||
|
||||
type ValuesReadOnly interface {
|
||||
// Has returns true if a binder responsible to
|
||||
// bind and return a type of "typ" is already registered to this controller.
|
||||
Has(value interface{}) bool
|
||||
// Len returns the length of the values.
|
||||
Len() int
|
||||
}
|
||||
|
||||
type Values []reflect.Value
|
||||
|
||||
func NewValues() Values {
|
||||
return Values{}
|
||||
}
|
||||
|
||||
// Clone returns a copy of the current values.
|
||||
func (bv Values) Clone() Values {
|
||||
if n := len(bv); n > 0 {
|
||||
values := make(Values, n, n)
|
||||
copy(values, bv)
|
||||
return values
|
||||
}
|
||||
|
||||
return NewValues()
|
||||
}
|
||||
|
||||
func (bv Values) Len() int {
|
||||
return len(bv)
|
||||
}
|
||||
|
||||
// Add binds values to this controller, if you want to share
|
||||
// binding values between controllers use the Engine's `Bind` function instead.
|
||||
func (bv *Values) Add(values ...interface{}) {
|
||||
@@ -57,13 +80,12 @@ func (bv *Values) remove(typ reflect.Type, n int) (ok bool) {
|
||||
|
||||
// Has returns true if a binder responsible to
|
||||
// bind and return a type of "typ" is already registered to this controller.
|
||||
func (bv *Values) Has(value interface{}) bool {
|
||||
func (bv Values) Has(value interface{}) bool {
|
||||
return bv.valueTypeExists(reflect.TypeOf(value))
|
||||
}
|
||||
|
||||
func (bv *Values) valueTypeExists(typ reflect.Type) bool {
|
||||
input := *bv
|
||||
for _, in := range input {
|
||||
func (bv Values) valueTypeExists(typ reflect.Type) bool {
|
||||
for _, in := range bv {
|
||||
if equalTypes(in.Type(), typ) {
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user