Skip to content

Commit

Permalink
Reduce get call loc
Browse files Browse the repository at this point in the history
  • Loading branch information
tung.tq committed Oct 18, 2023
1 parent 4fc2dd4 commit f9233a6
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions svloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ type registeredService struct {
onShutdown func()
}

func (s *registeredService) newService(unv *Universe, callLoc string) any {
func (s *registeredService) newService(unv *Universe) any {
if s.onceDone.Load() {
return s.svc
}

callLoc := getCallerLocationWithSkip(2)

svc := s.newServiceSlow(unv, callLoc)

unv.data.appendShutdownFunc(s.onShutdown)
Expand Down Expand Up @@ -255,9 +257,7 @@ func (s *Locator[T]) Get(unv *Universe) T {
panic(err.Error())
}

loc := getCallerLocation()

svc := reg.newService(unv, loc)
svc := reg.newService(unv)
result, ok := svc.(T)
if !ok {
var empty T
Expand Down Expand Up @@ -451,6 +451,11 @@ func checkAllowRegistering() {
}
}

func getCallerLocationWithSkip(skip int) string {
_, file, line, _ := runtime.Caller(skip + 1)
return fmt.Sprintf("%s:%d", file, line)
}

func getCallerLocation() string {
_, file, line, _ := runtime.Caller(2)
return fmt.Sprintf("%s:%d", file, line)
Expand Down

0 comments on commit f9233a6

Please sign in to comment.