Skip to content

Commit fc00b9d

Browse files
authored
Merge pull request #249 from mikmart/compilation-time
b: Track and report compilation times
2 parents 439196e + 220a3bf commit fc00b9d

4 files changed

Lines changed: 73 additions & 3 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ RSS=\
2424
$(SRC)/lexer.rs \
2525
$(SRC)/nob.rs \
2626
$(SRC)/targets.rs \
27+
$(SRC)/time.rs \
2728
$(SRC)/jim.rs \
2829
$(SRC)/jimp.rs \
2930
$(SRC)/codegen/gas_aarch64.rs \
@@ -38,6 +39,7 @@ POSIX_OBJS=\
3839
$(BUILD)/glob.posix.o \
3940
$(BUILD)/libc.posix.o \
4041
$(BUILD)/arena.posix.o \
42+
$(BUILD)/time.posix.o \
4143
$(BUILD)/jim.posix.o \
4244
$(BUILD)/jimp.posix.o \
4345
$(BUILD)/shlex.posix.o \
@@ -48,6 +50,7 @@ MINGW32_OBJS=\
4850
$(BUILD)/glob.mingw32.o \
4951
$(BUILD)/libc.mingw32.o \
5052
$(BUILD)/arena.mingw32.o \
53+
$(BUILD)/time.mingw32.o \
5154
$(BUILD)/jim.mingw32.o \
5255
$(BUILD)/jimp.mingw32.o \
5356
$(BUILD)/shlex.mingw32.o \

src/b.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub mod codegen;
3535
pub mod lexer;
3636
pub mod targets;
3737
pub mod ir;
38+
pub mod time;
3839
pub mod shlex;
3940

4041
use core::ffi::*;
@@ -50,6 +51,7 @@ use arena::Arena;
5051
use targets::*;
5152
use lexer::{Lexer, Loc, Token};
5253
use ir::*;
54+
use time::Instant;
5355
use codegen::*;
5456
use shlex::*;
5557

@@ -1302,7 +1304,9 @@ pub unsafe fn main(mut argc: i32, mut argv: *mut*mut c_char) -> Option<()> {
13021304
sb_appendf(&mut sb, c!("%s"), input_path);
13031305
}
13041306
da_append(&mut sb, 0);
1305-
log(Log_Level::INFO, c!("compiling files %s"), sb.items);
1307+
log(Log_Level::INFO, c!("compiling %zu files: %s"), input_paths.count, sb.items);
1308+
1309+
let compilation_start = Instant::now();
13061310

13071311
let mut input: String_Builder = zeroed();
13081312

@@ -1324,15 +1328,17 @@ pub unsafe fn main(mut argc: i32, mut argv: *mut*mut c_char) -> Option<()> {
13241328

13251329
if find_var_deep(&mut c.vars, used_global.name).is_null() {
13261330
diagf!(used_global.loc, c!("ERROR: could not find name `%s`\n"), used_global.name);
1327-
bump_error_count(&mut c);
1331+
bump_error_count(&mut c)?;
13281332
}
13291333
}
13301334

13311335
scope_pop(&mut c.vars); // end global scope
13321336

13331337
if c.error_count > 0 {
1334-
return None
1338+
return None;
13351339
}
1340+
1341+
log(Log_Level::INFO, c!("compilation took %.3fs"), compilation_start.elapsed().as_secs_f64());
13361342
}
13371343

13381344
let mut output: String_Builder = zeroed();

src/time.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use core::time::Duration;
2+
3+
#[derive(Clone, Copy, PartialEq, PartialOrd)]
4+
pub struct Instant {
5+
nanos: u64,
6+
}
7+
8+
impl Instant {
9+
pub unsafe fn now() -> Instant {
10+
extern "C" {
11+
fn nanos_since_unspecified_epoch() -> u64;
12+
}
13+
Instant {
14+
nanos: nanos_since_unspecified_epoch(),
15+
}
16+
}
17+
18+
pub unsafe fn elapsed(self) -> Duration {
19+
Instant::now().duration_since(self)
20+
}
21+
22+
pub unsafe fn duration_since(self, earlier: Instant) -> Duration {
23+
if self > earlier {
24+
Duration::from_nanos(self.nanos - earlier.nanos)
25+
} else {
26+
Duration::ZERO
27+
}
28+
}
29+
}

thirdparty/time.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <time.h>
2+
#include <stdint.h>
3+
4+
#ifdef _WIN32
5+
# define WIN32_LEAN_AND_MEAN
6+
# include <windows.h>
7+
#endif // _WIN32
8+
9+
#define NANOS_PER_SEC 1000000000
10+
11+
// The maximum time span representable is 584 years.
12+
uint64_t nanos_since_unspecified_epoch() {
13+
#ifdef _WIN32
14+
LARGE_INTEGER Time;
15+
QueryPerformanceCounter(&Time);
16+
17+
static LARGE_INTEGER Frequency = {0};
18+
if (Frequency.QuadPart == 0) {
19+
QueryPerformanceFrequency(&Frequency);
20+
}
21+
22+
uint64_t Secs = Time.QuadPart / Frequency.QuadPart;
23+
uint64_t Nanos = Time.QuadPart % Frequency.QuadPart * NANOS_PER_SEC / Frequency.QuadPart;
24+
return NANOS_PER_SEC * Secs + Nanos;
25+
#else
26+
struct timespec ts;
27+
clock_gettime(CLOCK_MONOTONIC, &ts);
28+
29+
return NANOS_PER_SEC * ts.tv_sec + ts.tv_nsec;
30+
#endif // _WIN32
31+
}
32+
// TODO: Consider making this a part of https://github.com/tsoding/nob.h

0 commit comments

Comments
 (0)