samber / do

⚙️ A dependency injection toolkit based on Go 1.18+ Generics.
https://pkg.go.dev/github.com/samber/do
MIT License
1.71k stars 67 forks source link

Performance Question about service instnace : getInstance(i Injector) (T, error) #66

Open kcmvp opened 3 months ago

kcmvp commented 3 months ago
func (s *serviceEager[T]) getInstance(i Injector) (T, error) {
    frame, ok := stacktrace.NewFrameFromCaller()
    if ok {
        s.mu.Lock()
        s.invokationFrames = append(s.invokationFrames, frame) // @TODO: potential memory leak
        s.mu.Unlock()
    }

    return s.instance, nil
}

above code is from https://github.com/samber/do/blob/v2-%F0%9F%9A%80/service_eager.go,

right now I am using do by generate a global singleton scope and get the service on demand. but there is a call to get call stack everytime I try to get the instnace frame, ok := stacktrace.NewFrameFromCaller(), in fact this methos is not exported just for internal usagae. It will decrease the system performance. is there way to optimize the design?

samber commented 2 months ago

3 ideas come to my mind:

I'm adding this to v2.1 roadmap.