Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/log: Avoid unnecessary mutex lock/unlock #19438

Closed
ShevaXu opened this issue Mar 7, 2017 · 5 comments
Closed

pkg/log: Avoid unnecessary mutex lock/unlock #19438

ShevaXu opened this issue Mar 7, 2017 · 5 comments

Comments

@ShevaXu
Copy link

ShevaXu commented Mar 7, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go1.8

What operating system and processor architecture are you using (go env)?

darwin/amd64 & linux/amd64

What did you do?

I found func (l *Logger) Output(calldepth int, s string) error in pkg/log had a strange lock-then-unlock logic for the expensive runtime.Caller(calldepth).

I tried to remove this lock-unlock logic and benchmark it, which results in ~20% improvement for concurrent logging with flags Lshortfile or Llongfile (tested on both darwin&linux).

Benchmark suit here: https://github.com/ShevaXu/bench-log

@mvdan
Copy link
Member

mvdan commented Mar 7, 2017

As someone not familiar with neither the log package nor runtime.Caller, are you sure the lock isn't there for a particular reason? Did you try git blame?

@ShevaXu
Copy link
Author

ShevaXu commented Mar 7, 2017

git blame shows commit 6075206 log: more locking did add this lock to guard prefix & flag. I think it has its point.

I was just curious how this would hurt log performance, and whether it is insignificant for most cases.

@ianlancetaylor
Copy link
Member

I'm going to close this because I don't think there is anything to do here. We can't simply remove the lock, or we would get race conditions if one goroutine logs while a different goroutine calls SetFlags.

@ShevaXu
Copy link
Author

ShevaXu commented Mar 7, 2017

I think the race conditions caused by removing the lock would not cause any bug or un-expected in real scenarios. Thanks for the discussion anyway.

@mvdan
Copy link
Member

mvdan commented Mar 7, 2017

@ShevaXu making the log package not thread-safe would break backwards compatibility, so it's not an option.

In any case, if what you need is performance in your log calls, you'd probably want to write custom code instead of using the standard logger.

@golang golang locked and limited conversation to collaborators Mar 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants