On Tue, Sep 6, 2016 at 9:00 AM, <mworks...@gmail.com> wrote: > > Hey, It's commented "release lock while getting caller info - it's > expensive" in source code of /src/log/log.go line:150. > > I'm confused with what makes it expensive if we didn't unlock the l.mu ? > Does goroutines have context ?
As the comment suggests, `runtime.Caller` is not especially fast. > What will happen if I moved the _, file, line, ok = > runtime.Caller(calldepth) like below? Is better than the upper one ? > > func (l *Logger) Output(calldepth int, s string) error { > now := time.Now() // get this early. > var file string > var line int > > //moved here > var ok bool > _, file, line, ok = runtime.Caller(calldepth) > > l.mu.Lock() > defer l.mu.Unlock() > > l.buf = l.buf[:0] > if l.flag&(Lshortfile|Llongfile) != 0 { > if !ok { > file = "???" > line = 0 > } > l.formatHeader(&l.buf, now, file, line) > } > > l.buf = append(l.buf, s...) > if len(s) == 0 || s[len(s)-1] != '\n' { > l.buf = append(l.buf, '\n') > } > _, err := l.out.Write(l.buf) > return err > } This approach has to advantage of not holding the lock but the disadvantage of calling runtime.Caller in all cases, although it's only necessary when Lshortfile or Llongfile is set. In particular note that neither flag is set by default, so calling runtime.Caller in all cases penalizes all log calls for the benefit of a minority. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.