Skip to content

RealClock Since&Until optimizations#102

Open
kriakable wants to merge 1 commit intojonboulle:masterfrom
kriakable:realclock-opt
Open

RealClock Since&Until optimizations#102
kriakable wants to merge 1 commit intojonboulle:masterfrom
kriakable:realclock-opt

Conversation

@kriakable
Copy link

@kriakable kriakable commented Aug 25, 2025

My implementation gives a way for time package optimizations:

// Since returns the time elapsed since t.
// It is shorthand for time.Now().Sub(t).
func Since(t Time) Duration {
	if t.wall&hasMonotonic != 0 {
		// Common case optimization: if t has monotonic time, then Sub will use only it.
		return subMono(runtimeNano()-startNano, t.ext)
	}
	return Now().Sub(t)
}

// Until returns the duration until t.
// It is shorthand for t.Sub(time.Now()).
func Until(t Time) Duration {
	if t.wall&hasMonotonic != 0 {
		// Common case optimization: if t has monotonic time, then Sub will use only it.
		return subMono(t.ext, runtimeNano()-startNano)
	}
	return t.Sub(Now())
}

Which leads to greatly decreased latencies of these methods

@kriakable
Copy link
Author

@jonboulle hello Nell, could you please have a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant