Skip to content

Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.

C/C++: valid UTF-8 demo data, no FMA in the C demo, errors before warnings

EL4CTEO95da0c8ed69b
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f788238..98b6b14 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -161,12 +161,12 @@ jobs:
working-directory: examples/mandelbrot-c
run: |
fa=../../target/release/fast-anything
- $fa scan -- clang -O2 main.c mandelbrot.c
+ $fa scan -- clang -O2 -ffp-contract=off main.c mandelbrot.c
$fa port
cp reference/port.rs .fast/ports/$(ls .fast/ports)/src/port.rs
$fa verify
$fa enable
- clang -O2 main.c mandelbrot.c -o app && ./app
+ clang -O2 -ffp-contract=off main.c mandelbrot.c -o app && ./app
$fa bench --runs 2
$fa disable
git diff --exit-code mandelbrot.c
@@ -174,12 +174,14 @@ jobs:
working-directory: examples/fuzzy-dedupe-cpp
run: |
fa=../../target/release/fast-anything
- $fa scan -- clang++ -O2 -std=c++17 main.cpp fuzzy.cpp
+ # the runner's standalone clang can be older than the MSVC STL it pairs with
+ extra=""; if [ "$RUNNER_OS" = Windows ]; then extra=-D_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH; fi
+ $fa scan -- clang++ -O2 -std=c++17 $extra main.cpp fuzzy.cpp
$fa port
cp reference/port.rs .fast/ports/$(ls .fast/ports)/src/port.rs
$fa verify
$fa enable
- clang++ -O2 -std=c++17 main.cpp fuzzy.cpp -o app && ./app
+ clang++ -O2 -std=c++17 $extra main.cpp fuzzy.cpp -o app && ./app
$fa bench --runs 2
$fa disable
git diff --exit-code fuzzy.cpp
diff --git a/examples/fuzzy-dedupe-cpp/main.cpp b/examples/fuzzy-dedupe-cpp/main.cpp
index bf174c9..2749ee9 100644
--- a/examples/fuzzy-dedupe-cpp/main.cpp
+++ b/examples/fuzzy-dedupe-cpp/main.cpp
@@ -21,7 +21,10 @@ int main() {
std::vector<std::string> names;
for (int k = 0; k < 6000; k++) {
std::string name = std::string(FIRST[next(20)]) + " " + LAST[next(20)];
- if (next(100) < 35) name[next((unsigned) name.size())] = "aeiourstln"[next(10)];
+ if (next(100) < 35) {
+ size_t at = next((unsigned) name.size());
+ if ((unsigned char) name[at] < 0x80) name[at] = "aeiourstln"[next(10)]; // keep it valid UTF-8
+ }
names.push_back(name);
}
auto pairs = find_duplicates(names, 2);
diff --git a/examples/mandelbrot-c/main.c b/examples/mandelbrot-c/main.c
index 76ba9df..818439f 100644
--- a/examples/mandelbrot-c/main.c
+++ b/examples/mandelbrot-c/main.c
@@ -1,5 +1,6 @@
// Count the points inside the Mandelbrot set at a few zoom levels, the slow way.
-// clang -O2 main.c mandelbrot.c -o mandelbrot
+// clang -O2 -ffp-contract=off main.c mandelbrot.c -o mandelbrot
+// (-ffp-contract=off: no fused multiply-add, so every compiler gives the same doubles)
#include <stdio.h>
long long mandelbrot(int width, int height, int max_iter);
diff --git a/src/cfamily.rs b/src/cfamily.rs
index 113dace..138298f 100644
--- a/src/cfamily.rs
+++ b/src/cfamily.rs
@@ -138,10 +138,22 @@ fn compile(ctx: &Ctx, cc: &str, b: &Build, extra: &[PathBuf], include: Option<&P
cmd.arg("-include").arg(h);
}
let out = output(&mut cmd, cc)?;
- ensure!(out.status.success(), "building the app failed:\n{}", tail(&combined(&out), 30));
+ ensure!(out.status.success(), "building the app failed:\n{}", errors_first(&combined(&out)));
Ok(bin)
}
+/// Compiler output with the error lines (and a few after each) first: warnings can bury them.
+fn errors_first(log: &str) -> String {
+ let lines: Vec<&str> = log.lines().collect();
+ let mut keep = Vec::new();
+ for (i, l) in lines.iter().enumerate() {
+ if l.contains("error") && !l.contains("warning") {
+ keep.extend(lines[i..(i + 4).min(lines.len())].iter().copied());
+ }
+ }
+ if keep.is_empty() { tail(log, 30) } else { keep.into_iter().take(40).collect::<Vec<_>>().join("\n") }
+}
+
fn run(ctx: &Ctx, bin: &Path, b: &Build, env: &[(&str, &Path)], derive: bool) -> Result<std::process::Output> {
let mut cmd = Command::new(bin);
cmd.args(&b.args).current_dir(&ctx.root).env(DISABLE_ENV, "1").stdin(Stdio::null());
@@ -1073,6 +1085,7 @@ pub const FLAVOR_C: Flavor = Flavor {
"C `int` is `i32`, `long` is `i64` here; unsigned types wrap. Use `wrapping_*` where the original can overflow.",
"A `const T *` followed by a length arrives as a Vec, and the length parameter still arrives on its own.",
"`const char *` arrives as a UTF-8 String: `strlen` and `s[i]` are bytes, use `as_bytes()` for the same behavior.",
+ "Floats: compilers may fuse `a * b + c` into one FMA instruction (clang does on ARM), which rounds differently. If recorded results differ in the last digits, the original was built that way: use `f64::mul_add` there, or build the app with `-ffp-contract=off`.",
"Returns are numbers or bools. Undefined behavior in the original (overflow of signed ints, out-of-bounds reads) isn't a spec: match the defined cases.",
],
};
diff --git a/src/cfamily/prof.c b/src/cfamily/prof.c
index 5224c81..d88dda8 100644
--- a/src/cfamily/prof.c
+++ b/src/cfamily/prof.c
@@ -1,4 +1,7 @@
/* fast-anything profiler runtime: compiled into the app for one run. */
+#ifndef _CRT_SECURE_NO_WARNINGS
+#define _CRT_SECURE_NO_WARNINGS
+#endif
#include <stdio.h>
#include <stdlib.h>
#include "fa_clock.h"
diff --git a/src/cfamily/rec.c b/src/cfamily/rec.c
index 49759e5..1fc2b2b 100644
--- a/src/cfamily/rec.c
+++ b/src/cfamily/rec.c
@@ -1,4 +1,7 @@
/* fast-anything recorder runtime for C: compiled into the app for one run. */
+#ifndef _CRT_SECURE_NO_WARNINGS
+#define _CRT_SECURE_NO_WARNINGS
+#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>