Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion benchmarks/cross-language/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ would use, fixed by the shape of the work rather than picked per pattern.
### Statistical approach

- Each benchmark runs **5 times** (`BENCH_RUNS=N`) after **1 warmup run**
(`BENCH_WARMUP=N`), the same for every language
(`BENCH_WARMUP=N`), the same for every language. Skynet runs **25 times**
(`BENCH_SKYNET_RUNS=N`): one run takes about 0.3 ms, short enough that a
median of five is mostly scheduling jitter (#3868)
- The **median** of the timed runs is reported, with the minimum, the maximum
and the coefficient of variation (sample standard deviation over the mean)
- Every run's value is stored in the results JSON
Expand Down
1 change: 1 addition & 0 deletions benchmarks/cross-language/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The runner reads its settings from the environment:
| `BENCHMARK_MESSAGES` | 1000000 | Messages per run (skynet: leaves) |
| `BENCH_RUNS` | 5 | Timed runs per language and pattern; the median is reported |
| `BENCH_WARMUP` | 1 | Discarded runs before them, the same for every language |
| `BENCH_SKYNET_RUNS` | 25 | Timed runs for skynet, whose run is about 0.3 ms (#3868); never fewer than `BENCH_RUNS` |
| `BENCH_LANGS` | all | Languages to run, comma-separated (`aether,c,rust`) |
| `BENCH_PATTERNS` | all | Patterns to run, comma-separated (`ping_pong,skynet`) |
| `BENCH_STRICT` | 0 | 1: exit 1 if any selected language does not build or is wrong (the CI job) |
Expand Down
21 changes: 16 additions & 5 deletions benchmarks/cross-language/run_benchmarks.ae
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,21 @@ main() {
if env_r != 0 { runs = atoi(env_r) }
env_w = getenv("BENCH_WARMUP")
if env_w != 0 { warmup = atoi(env_w) }
// Skynet gets more timed runs (#3868): one run creates 1,111 units in
// about 0.3 ms, short enough that scheduling jitter outweighs speed
// (cv 14-27% in five runs), so its median is taken over 25. The same
// count for every language, as every other setting (FAIRNESS.md); never
// fewer than BENCH_RUNS.
skynet_runs = 25
env_sr = getenv("BENCH_SKYNET_RUNS")
if env_sr != 0 { skynet_runs = atoi(env_sr) }
env_m = getenv("BENCHMARK_MESSAGES")
if env_m != 0 { msgs = atoi(env_m) }
if runs < 1 {
println("ERROR: BENCH_RUNS must be at least 1")
exit(1)
}
if skynet_runs < runs { skynet_runs = runs }
only_langs = ""
env_l = getenv("BENCH_LANGS")
if env_l != 0 { only_langs = string.to_lower(env_l) }
Expand Down Expand Up @@ -461,16 +470,14 @@ main() {
}
arch = sh_text("uname -m")

cfg = Settings { runs: runs, warmup: warmup, msgs: msgs, time_cmd: time_cmd, is_mac: is_mac }

_mk, _mks = sh_run("mkdir -p build")
io_write_file("build/problems.txt", "")

println("============================================")
println(" Aether Cross-Language Benchmark Suite")
println("============================================")
println(" Messages: ${msgs}")
println(" Runs: ${runs} (median), warmup: ${warmup} for every language")
println(" Runs: ${runs} (median; skynet ${skynet_runs}), warmup: ${warmup} for every language")
println(" ${measure_note}")
println("")

Expand All @@ -486,11 +493,15 @@ main() {
println(" Running: ${pattern}")
println("============================================")

pruns = runs
if pattern == "skynet" { pruns = skynet_runs }
pcfg = Settings { runs: pruns, warmup: warmup, msgs: msgs, time_cmd: time_cmd, is_mac: is_mac }

ts = sh_text("date -u +\"%Y-%m-%dT%H:%M:%SZ\"")
json = "{\n \"timestamp\": \"${ts}\",\n"
json = "${json} \"pattern\": \"${pattern}\",\n"
json = "${json} \"messages\": ${msgs},\n"
json = "${json} \"runs_per_benchmark\": ${runs},\n"
json = "${json} \"runs_per_benchmark\": ${pruns},\n"
json = "${json} \"warmup_runs\": ${warmup},\n"
json = "${json} \"description\": \"${pattern_description(pattern)}\",\n"
json = "${json} \"measurement\": \"${json_escape(measure_note)}\",\n"
Expand All @@ -508,7 +519,7 @@ main() {
li = li + 1
continue
}
res = run_lang(li, pattern, cfg)
res = run_lang(li, pattern, pcfg)
// A missing third-party toolchain is a fair skip; a missing or
// wrong Aether is not. A results file that omits Aether while
// listing ten other languages reads as a complete comparison.
Expand Down
6 changes: 6 additions & 0 deletions new_changelogs/20261124T192416Z-changed-3868-skynet-runs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- **The cross-language skynet takes its median over 25 runs (#3868).**
One skynet run creates 1,111 units in about 0.3 ms, short enough that a
median of five runs was mostly scheduling jitter (cv 14-27%, Pony's lead
inside it). The runner now times skynet 25 times for every language
(`BENCH_SKYNET_RUNS`, never fewer than `BENCH_RUNS`) and records the
count in the results; the other patterns keep five.
14 changes: 14 additions & 0 deletions new_changelogs/20261124T192416Z-performance-3852-math-inline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- **std.math's scalar functions are inline (#3852).** `math.sqrt`,
`abs`, `min`, `max`, `clamp`, `floor`, `ceil`, `round` and their f32
forms were calls into libaether.a the C compiler could not see through,
each calling libm in turn. They now come from `aether_math_inline.h`
through `@c_include`, as std.string's reads do (#3836), using the
compiler's builtins on GCC and clang (no `<math.h>` in the program) and
libm's names elsewhere; the exported `math_*` functions are the same
bodies, and the values are unchanged (NaN handling of min/max,
`abs_int(INT_MIN)`, half-away rounding: `test_math_inline_scalars`). A
loop of sqrt, floor, min and abs went from 2.38 to 1.98 ns an iteration.
Transcendental functions stay out of line. On x86-64 MinGW `math.sqrt`
is now `sqrtsd`, correctly rounded, where the CRT's x87 sqrt rounded
twice (#3027): a result there may differ from before by one ulp, toward
the correct one.
10 changes: 10 additions & 0 deletions new_changelogs/20261124T192416Z-performance-3853-slice-release.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- **A slice's release is inline again for a view (#3853).** The release a
slice gets where it dies (a local's scope exit, a store, an unwind) is
`static inline`, but it carried the string release, the buffer's free and
its element drops, so the compiler left the whole release out of line: a
call per place for what is, for a view (`ptr as T[]`, a slice of a
slice), one test of its marks. The counted path that frees or touches a
published count now sits in a cold helper; the uncounted test and the
common counted case (a reference that is not the last) stay inline.
bench-core on an Apple M5 Pro: `slice_view_returned` 0.86 to 0.22 ns,
`slice_view_bound` 3.34 to 3.15 ns; the ownership rows unchanged.
41 changes: 37 additions & 4 deletions runtime/aether_slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,28 @@ static inline void aether_slice_buf_free(AetherSliceBuf* h) {
free(h);
}

/* The reference a place held to the buffer `s` views, given back; the last
* frees the buffer. An uncounted slice gives back nothing. */
static inline void aether_slice_unref(AetherSlice s) {
/* aether_slice_unref's counted path, out of its callers (#3853): a release
* is inlined at every place a slice dies (a local's scope exit, a store, an
* unwind), and most of them hold a view (`ptr as T[]`, a parameter, a
* slice of a slice), which gives back nothing. With the counted path
* inline, the string release and the buffer's free with its element drops
* came too, and the compiler left the whole release out of line, a call per
* place for two flag tests. Marked as AETHER_SLICE_FAIL's helpers are:
* cold on GCC and clang, which keeps it out of its callers, and noinline
* on cl.exe, which has no cold. */
#if defined(__GNUC__) || defined(__clang__)
#define AETHER_SLICE_COLD_PATH static inline __attribute__((cold))
#elif defined(_MSC_VER)
#define AETHER_SLICE_COLD_PATH static __inline __declspec(noinline)
#else
#define AETHER_SLICE_COLD_PATH static inline
#endif

AETHER_SLICE_COLD_PATH void aether_slice_unref_counted(AetherSlice s) {
if (s.flags & AETHER_SLICE_STRING) {
string_release(aether_slice_string_of(s));
return;
}
if (!(s.flags & AETHER_SLICE_MADE)) return;
AetherSliceBuf* h = aether_slice_buf(s);
int rc = aether_count_load(&h->refs);
if (rc & AETHER_SLICE_SHARED_BIT) {
Expand All @@ -222,6 +236,25 @@ static inline void aether_slice_unref(AetherSlice s) {
if (rc <= 1) aether_slice_buf_free(h);
}

/* The reference a place held to the buffer `s` views, given back; the last
* frees the buffer. Inline: an uncounted slice, which gives back nothing,
* and the common counted case, a reference of an unpublished buffer that is
* not the last (a view of a counted buffer, stored again in a loop), a load
* and a store. Out of line: a string's release, a published count, and the
* last reference, which frees. */
static inline void aether_slice_unref(AetherSlice s) {
if (!(s.flags & (AETHER_SLICE_STRING | AETHER_SLICE_MADE))) return;
if (!(s.flags & AETHER_SLICE_STRING)) {
AetherSliceBuf* h = aether_slice_buf(s);
int rc = aether_count_load(&h->refs);
if (!(rc & AETHER_SLICE_SHARED_BIT) && rc > 1) {
h->refs = rc - 1;
return;
}
}
aether_slice_unref_counted(s);
}

/* `s` is reachable from other threads from here on (a module `var`, a
* closure, a message, an actor's state): its count is atomic, and so is the
* count of everything its elements hold (a row of a slice of slices, a
Expand Down
45 changes: 20 additions & 25 deletions std/math/aether_math.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions std/math/aether_math_inline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* aether_math_inline.h: std.math's scalar functions as `static inline`
* (#3852).
*
* std.math asks for this header with `@c_include` (the #1986 mechanism, as
* std.string does since #3836), so in a program that imports it
* `math.sqrt(x)`, `math.min_float(a, b)` and the rest below are the
* operations they name rather than calls into libaether.a the C compiler
* cannot see through: `math.sqrt` was a call to a function that called
* libm's sqrt, where the compiler emits one instruction (a sqrt and a
* check of its operand, keeping errno as libm sets it).
*
* ONE BODY. aether_math.c includes this header, and the exported math_*
* functions there are these, so C that calls the exported names and an
* Aether program that calls the inline ones compute the same values: the
* same libm semantics (fmin/fmax return the non-NaN operand, abs_int of
* INT_MIN is INT_MIN), the same rounding.
*
* Injected into the translation unit of every program that imports
* std.math, so it declares nothing that is not prefixed (the lesson of
* #2162) and does not include <math.h> on GCC and clang: generated C does
* not include it there, and a program may define a function of its own
* with one of libm's names. The compiler's builtins are those functions
* without a declaration. Other compilers (cl.exe) get <math.h>, which
* generated C already includes for them. Transcendental functions (sin,
* pow, exp...) stay out of line: their cost is the libm call itself. */
#ifndef AETHER_MATH_INLINE_H
#define AETHER_MATH_INLINE_H

#if defined(__GNUC__) || defined(__clang__)
#define AETHER_MATH_SQRT(x) __builtin_sqrt(x)
#define AETHER_MATH_SQRTF(x) __builtin_sqrtf(x)
#define AETHER_MATH_FABS(x) __builtin_fabs(x)
#define AETHER_MATH_FABSF(x) __builtin_fabsf(x)
#define AETHER_MATH_FMIN(a, b) __builtin_fmin((a), (b))
#define AETHER_MATH_FMAX(a, b) __builtin_fmax((a), (b))
#define AETHER_MATH_FMINF(a, b) __builtin_fminf((a), (b))
#define AETHER_MATH_FMAXF(a, b) __builtin_fmaxf((a), (b))
#define AETHER_MATH_FLOOR(x) __builtin_floor(x)
#define AETHER_MATH_FLOORF(x) __builtin_floorf(x)
#define AETHER_MATH_CEIL(x) __builtin_ceil(x)
#define AETHER_MATH_CEILF(x) __builtin_ceilf(x)
#define AETHER_MATH_ROUND(x) __builtin_round(x)
#else
#include <math.h>
#define AETHER_MATH_SQRT(x) sqrt(x)
#define AETHER_MATH_SQRTF(x) sqrtf(x)
#define AETHER_MATH_FABS(x) fabs(x)
#define AETHER_MATH_FABSF(x) fabsf(x)
#define AETHER_MATH_FMIN(a, b) fmin((a), (b))
#define AETHER_MATH_FMAX(a, b) fmax((a), (b))
#define AETHER_MATH_FMINF(a, b) fminf((a), (b))
#define AETHER_MATH_FMAXF(a, b) fmaxf((a), (b))
#define AETHER_MATH_FLOOR(x) floor(x)
#define AETHER_MATH_FLOORF(x) floorf(x)
#define AETHER_MATH_CEIL(x) ceil(x)
#define AETHER_MATH_CEILF(x) ceilf(x)
#define AETHER_MATH_ROUND(x) round(x)
#endif

/* |x|. Aether's int wraps, and INT_MIN has no positive counterpart in an
* int, so abs_int(INT_MIN) is INT_MIN (as in Java and Go). Negated in
* unsigned arithmetic, where that is defined: `-x` overflowed, which C
* leaves undefined. */
static inline int aether_math_abs_int_inline(int x) {
return x < 0 ? (int)(0u - (unsigned)x) : x;
}
static inline double aether_math_abs_float_inline(double x) { return AETHER_MATH_FABS(x); }
static inline int aether_math_min_int_inline(int a, int b) { return a < b ? a : b; }
static inline int aether_math_max_int_inline(int a, int b) { return a > b ? a : b; }

/* fmin/fmax: when one side is NaN the other is returned, whichever side it
* is on. A `<` pick returned b for min(NaN, 1) but NaN for min(1, NaN). */
static inline double aether_math_min_float_inline(double a, double b) { return AETHER_MATH_FMIN(a, b); }
static inline double aether_math_max_float_inline(double a, double b) { return AETHER_MATH_FMAX(a, b); }

static inline int aether_math_clamp_int_inline(int x, int min, int max) {
if (x < min) return min;
if (x > max) return max;
return x;
}
static inline double aether_math_clamp_float_inline(double x, double min, double max) {
if (x < min) return min;
if (x > max) return max;
return x;
}

static inline double aether_math_sqrt_inline(double x) { return AETHER_MATH_SQRT(x); }
static inline double aether_math_floor_inline(double x) { return AETHER_MATH_FLOOR(x); }
static inline double aether_math_ceil_inline(double x) { return AETHER_MATH_CEIL(x); }
static inline double aether_math_round_inline(double x) { return AETHER_MATH_ROUND(x); }

/* Single precision (#2151). fminf/fmaxf rather than a `<` pick: they
* return the non-NaN operand when one side is NaN, as the double versions
* do through fmin/fmax. */
static inline float aether_math_sqrt_f32_inline(float x) { return AETHER_MATH_SQRTF(x); }
static inline float aether_math_abs_f32_inline(float x) { return AETHER_MATH_FABSF(x); }
static inline float aether_math_min_f32_inline(float a, float b) { return AETHER_MATH_FMINF(a, b); }
static inline float aether_math_max_f32_inline(float a, float b) { return AETHER_MATH_FMAXF(a, b); }
static inline float aether_math_clamp_f32_inline(float x, float min, float max) {
if (x < min) return min;
if (x > max) return max;
return x;
}
static inline float aether_math_floor_f32_inline(float x) { return AETHER_MATH_FLOORF(x); }
static inline float aether_math_ceil_f32_inline(float x) { return AETHER_MATH_CEILF(x); }

#endif /* AETHER_MATH_INLINE_H */
Loading
Loading