Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make monotimes have zero overhead if you don't use it #13338

Merged
merged 1 commit into from
Feb 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions lib/std/monotimes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ when defined(macosx):
proc mach_timebase_info(info: var MachTimebaseInfoData) {.importc,
header: "<mach/mach_time.h>".}

let machAbsoluteTimeFreq = block:
var freq: MachTimebaseInfoData
mach_timebase_info(freq)
freq

when defined(js):
proc getJsTicks: float =
## Returns ticks in the unit seconds
Expand Down Expand Up @@ -88,11 +83,6 @@ elif defined(windows):
proc QueryPerformanceFrequency(res: var uint64) {.
importc: "QueryPerformanceFrequency", stdcall, dynlib: "kernel32".}

let queryPerformanceCounterFreq = block:
var freq: uint64
QueryPerformanceFrequency(freq)
1_000_000_000'u64 div freq

proc getMonoTime*(): MonoTime {.tags: [TimeEffect].} =
## Get the current `MonoTime` timestamp.
##
Expand All @@ -105,6 +95,8 @@ proc getMonoTime*(): MonoTime {.tags: [TimeEffect].} =
result = MonoTime(ticks: (ticks * 1_000_000_000).int64)
elif defined(macosx):
let ticks = mach_absolute_time()
var machAbsoluteTimeFreq: MachTimebaseInfoData
mach_timebase_info(machAbsoluteTimeFreq)
result = MonoTime(ticks: ticks * machAbsoluteTimeFreq.numer div
machAbsoluteTimeFreq.denom)
elif defined(posix):
Expand All @@ -115,6 +107,10 @@ proc getMonoTime*(): MonoTime {.tags: [TimeEffect].} =
elif defined(windows):
var ticks: uint64
QueryPerformanceCounter(ticks)

var freq: uint64
QueryPerformanceFrequency(freq)
let queryPerformanceCounterFreq = 1_000_000_000'u64 div freq
result = MonoTime(ticks: (ticks * queryPerformanceCounterFreq).int64)

proc ticks*(t: MonoTime): int64 =
Expand Down