pingcap/tidb

use monotonic time directly instead of Go time in somewhere

Open

#32.121 geöffnet am 7. Feb. 2022

Auf GitHub ansehen
 (6 Kommentare) (3 Reaktionen) (1 zugewiesene Person)Go (6.186 Forks)batch import
help wantedtype/enhancementtype/performance

Repository-Metriken

Stars
 (40.090 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 14T 4h) (346 gemergte PRs in 30 T)

Beschreibung

Enhancement

In TiDB, we heavily use Go time.Now to calculate the duration of some operations, e.g. prometheus Histogram, query time, etc.

For time.Now, this function will call walltime and nanotime, but in the above scenario, we only need to call nanotime(monotonic clock) to calculate the duration.

import _ "unsafe"
//go:noescape
//go:linkname nanotime runtime.nanotime
func nanotime() int64

func Now() time.Duration {
	return time.Duration(nanotime())
}

Here is a simple implementation for Nanotime,


func BenchmarkTime1(b *testing.B) {
	for i := 0; i < b.N; i++ {
		time.Now()
	}
}

func BenchmarkTime2(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Now()
	}
}

The benchmark result is:

BenchmarkTime1-8          	17705235	        65.90 ns/op
BenchmarkTime2-8          	38601799	        29.68 ns/op

Contributor Guide