Files
benilla-octo/crates/benilla-ui/src/script/tests/dispatch_bench.rs
T
Sam SungandClaude Opus 5.5 fd386e75ae Comments in benilla-ui: one line of fact each, no history, no record numbers (#143)
Part of #41: the comments of `benilla-ui` (the Lua UI engine, its FrameXML loader and its widgets) are rewritten to the rule in `docs/METHOD.md`, which is what the code does and the reference fact behind it, in a line. 43,140 comment lines become 15,161.

- **The mechanical strip.** The citations that were only a private record number go first, and each edit is shown to be a pure removal.
- **Wrong facts corrected.** The rewrite corrects the comments that stated wrong facts. Among them:
  - `0x64a480` is the case-sensitive compare (a byte `repz cmpsb`), and `0x64a4c0` the case-insensitive one.
  - A frame's id sits at `+0xb0`, not `+0xb4`. `EnableKeyboard`/`IsKeyboardEnabled` are `0x776ec0`/`0x776f90`.
  - A region's unpinned edge has no fallback to its owner's rect: the owner supplies only the scale.
  - `$parent` expands to the nearest named ancestor, or "Top" when none is named (`0x76c5b0`), not simply to the parent.
  - A StatusBar fills from its internal normalised fraction, not from `GetValue()`.
  - The addresses of `StartDuel` and `StartDuelUnit` were swapped.
  - Many "untraced" notes now give the traced reference fact: the drag threshold, the per-grip sizing edges, the empty-`autoFocus` skip, and `SetTexCoordModifiesRect`'s default.
  - Stock FrameXML line citations are checked against the stock 1.12 files and corrected. `ref-` file names become the stock file names.
- **Deviation labels.** "Deviation:" now marks only a choice with a reason. A surface that exists only for the retired Era addon target says so plainly: it is not 1.12.
- **Verification.** Every file strips to identical code, and `cargo fmt --check` is clean. Nine second readers compared every file old against new, restored the facts the rewrite had dropped, and checked each new citation against the code, the stock files, vmangos and the reference.

Behaviour differences the rewrite surfaced are filed as their own issues (#131–#142), and more are being validated. The comments here state what the code does. String literals are untouched; they are #92.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 01:58:49 +07:00

163 lines
5.3 KiB
Rust

//! What one widget method lookup through the wrapper's `__index` costs: every call pays a hit,
//! and a miss (the duck-type probe `if f.SetValue then`) walks every registry in the kind chain.
//!
//! Run: `cargo test --release -p benilla-ui --lib -- --ignored --nocapture dispatch_bench`.
use std::time::Instant;
use super::common::script;
/// Lookups per timed loop, enough that the loop's own overhead is noise at nanosecond scale.
const N: usize = 400_000;
/// Timed repeats per row; each row reports the fastest.
const ROUNDS: usize = 5;
fn ns_per(t: Instant, n: usize) -> f64 {
t.elapsed().as_secs_f64() * 1e9 / n as f64
}
#[test]
#[ignore = "bench — run explicitly with --release --nocapture"]
fn dispatch_bench() {
let s = script();
s.run(
r#"
F = CreateFrame("Frame")
B = CreateFrame("Button")
T = F:CreateTexture()
S = F:CreateFontString()
"#,
)
.expect("fixtures");
// The minimum of `ROUNDS` runs: a mean would measure whatever else was running.
let floor = |what: &str, chunk: &str| {
let mut best = f64::INFINITY;
for _ in 0..ROUNDS {
let t = Instant::now();
s.run(chunk).expect("bench chunk");
best = best.min(ns_per(t, N));
}
println!("[dispatch] {what:>26} {best:7.1} ns");
};
// The floor: a plain table with a table `__index`, like the reference's flat C method probe.
floor(
"baseline (table meta)",
&format!(
"local base = {{ GetWidth = function() end }}
local o = setmetatable({{}}, {{ __index = base }})
for _ = 1, {N} do local m = o.GetWidth end"
),
);
floor(
"Frame hit",
&format!("for _ = 1, {N} do local m = F.GetWidth end"),
);
floor(
"Frame miss",
&format!("for _ = 1, {N} do local m = F.NoSuchMethod end"),
);
floor(
"Button hit",
&format!("for _ = 1, {N} do local m = B.GetWidth end"),
);
floor(
"Button own hit",
&format!("for _ = 1, {N} do local m = B.SetText end"),
);
floor(
"Button miss",
&format!("for _ = 1, {N} do local m = B.NoSuchMethod end"),
);
floor(
"Texture hit",
&format!("for _ = 1, {N} do local m = T.GetWidth end"),
);
floor(
"Texture miss",
&format!("for _ = 1, {N} do local m = T.NoSuchMethod end"),
);
floor(
"FontString hit",
&format!("for _ = 1, {N} do local m = S.GetWidth end"),
);
// Whole calls: `GetFrameLevel` is a frame-only binding (one Rust hop); `GetWidth` is one of
// `region_map`'s shared names, whose bridge re-enters Lua for the per-side arm.
floor(
"GetFrameLevel() call",
&format!("for _ = 1, {N} do local l = F:GetFrameLevel() end"),
);
floor(
"GetWidth() call (bridged)",
&format!("for _ = 1, {N} do local w = F:GetWidth() end"),
);
floor(
"SetPoint() call (bridged)",
&format!("for _ = 1, {N} do F:SetPoint(\"CENTER\", 0, 0) end"),
);
// Every geometry getter runs `layout_methods::settle`: free while nothing has moved, a whole
// `resolve_layout` after any layout write, so read-after-write pays one per read.
floor(
"write only",
&format!("for i = 1, {N} do F:SetPoint(\"CENTER\", i, 0) end"),
);
floor(
"write + read (settles)",
&format!("for i = 1, {N} do F:SetPoint(\"CENTER\", i, 0) local w = F:GetWidth() end"),
);
floor(
"write + read x3 (settles)",
&format!(
"for i = 1, {N} do F:SetPoint(\"CENTER\", i, 0) local w = F:GetWidth() local h = F:GetHeight() local l = F:GetLeft() end"
),
);
// Controls: `__direct` is one mlua closure, `__wrapped` one that calls another through
// `Function::call` as `region_map`'s shared arm does; their gap is one re-entrant Lua call.
{
let lua = s.lua();
let direct = lua
.create_function(|_, n: i64| Ok(n + 1))
.expect("direct fn");
lua.globals().set("__direct", direct.clone()).expect("set");
let wrapped = lua
.create_function(move |_, n: i64| direct.call::<i64>(n))
.expect("wrapped fn");
lua.globals().set("__wrapped", wrapped).expect("set");
// The same pair in the bridge's shape: a variadic `MultiValue` led by the receiver table.
let mv_direct = lua
.create_function(|_, args: mlua::MultiValue| Ok(args.len() as i64))
.expect("mv direct");
lua.globals()
.set("__mv_direct", mv_direct.clone())
.expect("set");
let mv_relay = lua
.create_function(move |_, args: mlua::MultiValue| {
mv_direct.call::<mlua::MultiValue>(args)
})
.expect("mv relay");
lua.globals().set("__mv_relay", mv_relay).expect("set");
}
floor(
"ctrl: 1 mlua hop",
&format!("for _ = 1, {N} do local v = __direct(1) end"),
);
floor(
"ctrl: mlua hop + relay",
&format!("for _ = 1, {N} do local v = __wrapped(1) end"),
);
floor(
"ctrl: variadic hop",
&format!("for _ = 1, {N} do local v = __mv_direct(F, 1, 2) end"),
);
floor(
"ctrl: variadic hop + relay",
&format!("for _ = 1, {N} do local v = __mv_relay(F, 1, 2) end"),
);
}