Make context.Background return a global background context.

It currently allocates a new context on the heap each time it is called. Some
of these are in relatively hot paths like signal delivery and releasing gofer
inodes.  It is also called very commonly in afterLoad.  All of these should
benefit from fewer heap allocations.

PiperOrigin-RevId: 232938873
Change-Id: I53cec0ca299f56dcd4866b0b4fd2ec4938526849
This commit is contained in:
Nicolas Lacasse 2019-02-07 13:54:13 -08:00 committed by Shentubot
parent e0afa87899
commit fcae058a14
1 changed files with 4 additions and 1 deletions

View File

@ -108,6 +108,9 @@ func (NoopSleeper) UninterruptibleSleepStart(bool) {}
// UninterruptibleSleepFinish does nothing. // UninterruptibleSleepFinish does nothing.
func (NoopSleeper) UninterruptibleSleepFinish(bool) {} func (NoopSleeper) UninterruptibleSleepFinish(bool) {}
// bgContext is the context returned by context.Background.
var bgContext = &logContext{Logger: log.Log()}
// Background returns an empty context using the default logger. // Background returns an empty context using the default logger.
// //
// Users should be wary of using a Background context. Please tag any use with // Users should be wary of using a Background context. Please tag any use with
@ -119,5 +122,5 @@ func (NoopSleeper) UninterruptibleSleepFinish(bool) {}
// Using a Background context for tests is fine, as long as no values are // Using a Background context for tests is fine, as long as no values are
// needed from the context in the tested code paths. // needed from the context in the tested code paths.
func Background() Context { func Background() Context {
return logContext{Logger: log.Log()} return bgContext
} }