aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2024-01-24 11:21:37 +0000
committerGitHub <noreply@github.com>2024-01-24 11:21:37 +0000
commitcfdf2bfb7760353fad0fc0e71a18d20065920539 (patch)
tree1a7e6592fd01652e6d5df499c77d47d24a5c3b81
parent2f8316840e97a365195de15756e6ddbc8839e201 (diff)
parent90d1f9ab276c6dc5ed3d208d3f3c7e6323f681d8 (diff)
Merge pull request #3129 from DragosPopse/assert-fix
`fmt.assertf` now correctly gets disabled on `-disable-assert`. `log.assert/f` procs. `@cold` trick on everything fixed.
-rw-r--r--core/fmt/fmt.odin24
-rw-r--r--core/log/log.odin36
2 files changed, 51 insertions, 9 deletions
diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin
index c9e284edc..f4fddd18d 100644
--- a/core/fmt/fmt.odin
+++ b/core/fmt/fmt.odin
@@ -253,18 +253,24 @@ bprintf :: proc(buf: []byte, fmt: string, args: ..any) -> string {
// - args: A variadic list of arguments to be formatted
// - loc: The location of the caller
//
-// Returns: True if the condition is met, otherwise triggers a runtime assertion with a formatted message
-//
-assertf :: proc(condition: bool, fmt: string, args: ..any, loc := #caller_location) -> bool {
+@(disabled=ODIN_DISABLE_ASSERT)
+assertf :: proc(condition: bool, fmt: string, args: ..any, loc := #caller_location) {
if !condition {
- p := context.assertion_failure_proc
- if p == nil {
- p = runtime.default_assertion_failure_proc
+ // NOTE(dragos): We are using the same trick as in builtin.assert
+ // to improve performance to make the CPU not
+ // execute speculatively, making it about an order of
+ // magnitude faster
+ @(cold)
+ internal :: proc(loc: runtime.Source_Code_Location, fmt: string, args: ..any) {
+ p := context.assertion_failure_proc
+ if p == nil {
+ p = runtime.default_assertion_failure_proc
+ }
+ message := tprintf(fmt, ..args)
+ p("Runtime assertion", message, loc)
}
- message := tprintf(fmt, ..args)
- p("Runtime assertion", message, loc)
+ internal(loc, fmt, ..args)
}
- return condition
}
// Runtime panic with a formatted message
//
diff --git a/core/log/log.odin b/core/log/log.odin
index 021a46000..b4039caa0 100644
--- a/core/log/log.odin
+++ b/core/log/log.odin
@@ -116,6 +116,42 @@ panicf :: proc(fmt_str: string, args: ..any, location := #caller_location) -> !
runtime.panic("log.panicf", location)
}
+@(disabled=ODIN_DISABLE_ASSERT)
+assert :: proc(condition: bool, message := "", loc := #caller_location) {
+ if !condition {
+ @(cold)
+ internal :: proc(message: string, loc: runtime.Source_Code_Location) {
+ p := context.assertion_failure_proc
+ if p == nil {
+ p = runtime.default_assertion_failure_proc
+ }
+ log(.Fatal, message, location=loc)
+ p("runtime assertion", message, loc)
+ }
+ internal(message, loc)
+ }
+}
+
+@(disabled=ODIN_DISABLE_ASSERT)
+assertf :: proc(condition: bool, fmt_str: string, args: ..any, loc := #caller_location) {
+ if !condition {
+ // NOTE(dragos): We are using the same trick as in builtin.assert
+ // to improve performance to make the CPU not
+ // execute speculatively, making it about an order of
+ // magnitude faster
+ @(cold)
+ internal :: proc(loc: runtime.Source_Code_Location, fmt_str: string, args: ..any) {
+ p := context.assertion_failure_proc
+ if p == nil {
+ p = runtime.default_assertion_failure_proc
+ }
+ message := fmt.tprintf(fmt_str, ..args)
+ log(.Fatal, message, location=loc)
+ p("Runtime assertion", message, loc)
+ }
+ internal(loc, fmt_str, ..args)
+ }
+}