forked from valyala/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimization: substitute time.Now() calls with coarse-grained time in…
… hot paths
- Loading branch information
Showing
5 changed files
with
77 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package fasthttp | ||
|
||
import ( | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
func coarseTimeNow() time.Time { | ||
tp := coarseTime.Load().(*time.Time) | ||
return *tp | ||
} | ||
|
||
func init() { | ||
t := time.Now() | ||
coarseTime.Store(&t) | ||
go func() { | ||
for { | ||
time.Sleep(time.Second) | ||
t := time.Now() | ||
coarseTime.Store(&t) | ||
} | ||
}() | ||
} | ||
|
||
var coarseTime atomic.Value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package fasthttp | ||
|
||
import ( | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func BenchmarkCoarseTimeNow(b *testing.B) { | ||
var zeroTimeCount uint64 | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
t := coarseTimeNow() | ||
if t.IsZero() { | ||
atomic.AddUint64(&zeroTimeCount, 1) | ||
} | ||
} | ||
}) | ||
if zeroTimeCount > 0 { | ||
b.Fatalf("zeroTimeCount must be zero") | ||
} | ||
} | ||
|
||
func BenchmarkTimeNow(b *testing.B) { | ||
var zeroTimeCount uint64 | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
t := time.Now() | ||
if t.IsZero() { | ||
atomic.AddUint64(&zeroTimeCount, 1) | ||
} | ||
} | ||
}) | ||
if zeroTimeCount > 0 { | ||
b.Fatalf("zeroTimeCount must be zero") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters