From 56ab1e809117c614b2131493c710754da6e29cca Mon Sep 17 00:00:00 2001 From: maypok86 Date: Mon, 9 Sep 2024 09:13:46 +0300 Subject: [PATCH] fix: stop importing nanotime from runtime via linkname --- nanotime.go | 16 ++++++++++++++++ nanotime_time.go | 13 ------------- nanotime_unsafe.go | 12 ------------ 3 files changed, 16 insertions(+), 25 deletions(-) create mode 100644 nanotime.go delete mode 100644 nanotime_time.go delete mode 100644 nanotime_unsafe.go diff --git a/nanotime.go b/nanotime.go new file mode 100644 index 0000000..8a5351a --- /dev/null +++ b/nanotime.go @@ -0,0 +1,16 @@ +package puddle + +import "time" + +// nanotime returns the time in nanoseconds since process start. +// +// This approach, described at +// https://github.com/golang/go/issues/61765#issuecomment-1672090302, +// is fast, monotonic, and portable, and avoids the previous +// dependence on runtime.nanotime using the (unsafe) linkname hack. +// In particular, time.Since does less work than time.Now. +func nanotime() int64 { + return time.Since(globalStart).Nanoseconds() +} + +var globalStart = time.Now() diff --git a/nanotime_time.go b/nanotime_time.go deleted file mode 100644 index f8e7593..0000000 --- a/nanotime_time.go +++ /dev/null @@ -1,13 +0,0 @@ -//go:build purego || appengine || js - -// This file contains the safe implementation of nanotime using time.Now(). - -package puddle - -import ( - "time" -) - -func nanotime() int64 { - return time.Now().UnixNano() -} diff --git a/nanotime_unsafe.go b/nanotime_unsafe.go deleted file mode 100644 index fc3b8a2..0000000 --- a/nanotime_unsafe.go +++ /dev/null @@ -1,12 +0,0 @@ -//go:build !purego && !appengine && !js - -// This file contains the implementation of nanotime using runtime.nanotime. - -package puddle - -import "unsafe" - -var _ = unsafe.Sizeof(0) - -//go:linkname nanotime runtime.nanotime -func nanotime() int64