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.
Rust agent-skillsai-agentsclaude-codeclaude-code-plugincodexdeveloper-toolsffimcpmcp-servernapi-rsnodejsoptimizationperformanceprofilerpyo3pythonrewrite-it-in-rustrubyrusttypescript
php: derive edge cases while recording
Mohit Mishra151c4f4b84dd
diff --git a/src/php.rs b/src/php.rsindex 2a05ff3..d82ac97 100644--- a/src/php.rs+++ b/src/php.rs@@ -354,6 +354,63 @@ pub fn disable(ctx: &Ctx, port: &Port) -> Result<()> {mod tests {use super::*;+ #[test]+ fn the_recorder_adds_edge_cases() {+ use serde_json::json;+ if !Command::new("php").arg("-v").output().is_ok_and(|o| o.status.success()) {+ eprintln!("no php on PATH, skipping");+ return;+ }+ let dir = std::env::temp_dir().join(format!("fa-php-edges-test-{}", std::process::id()));+ fs::create_dir_all(&dir).unwrap();+ fs::write(dir.join("rec.php"), REC).unwrap();+ // width throws on an emoji: inputs it throws on aren't fair tests and must be left out+ let check = r#"<?php+ require __DIR__ . '/rec.php';+ function width(string $s, array $xs): int+ {+ if (str_contains($s, "\u{1f44d}")) throw new InvalidArgumentException('emoji');+ return preg_match_all('/./su', $s) + count($xs);+ }+ $s = str_repeat('word ', 40);+ for ($k = 0; $k < 3; $k++) {+ FastAnythingRec::record('width', [$s, array_fill(0, 20 + $k, 0)], 'width');+ }+ "#;+ fs::write(dir.join("check.php"), check).unwrap();+ let out = dir.join("rec.jsonl");+ let run = Command::new("php")+ .arg("check.php")+ .current_dir(&dir)+ .env("FAST_ANYTHING_RECORD_OUT", &out)+ .env("FAST_ANYTHING_RECORD_META", dir.join("meta.json"))+ .output()+ .unwrap();+ let text = fs::read_to_string(&out).unwrap_or_default();+ let _ = fs::remove_dir_all(&dir);+ assert!(+ run.status.success(),+ "{}{}",+ String::from_utf8_lossy(&run.stdout),+ String::from_utf8_lossy(&run.stderr)+ );+ let edges: Vec<Value> = text+ .lines()+ .map(|l| serde_json::from_str::<Value>(l).unwrap())+ .filter(|c| c["derived"] == "edge")+ .collect();+ let has_arg = |want: &Value, i: usize| edges.iter().any(|c| &c["args"][i] == want);+ assert!(has_arg(&json!(""), 0) && has_arg(&json!([]), 1), "empty inputs: {text}");+ // non-ASCII text, added to the short version of the recorded input (64 characters, 8 items)+ let short: String = "word ".repeat(40).chars().take(64).collect();+ assert!(has_arg(&json!(format!("{short}\u{65e5}\u{672c}")), 0), "{text}");+ assert!(!text.contains('\u{1f44d}'), "an input the original throws on");+ assert!(edges.iter().any(|c| c["args"][1].as_array().is_some_and(|a| a.len() == 8 * 16)), "bigger arrays");+ assert!(edges.iter().any(|c| c["args"][1][0] == json!(-1)), "special numbers in arrays");+ assert!(edges.iter().all(|c| c["result"].is_i64()), "every edge has the original's answer");+ assert!(edges.len() <= 100);+ }+#[test]fn php_bits() {assert!(is_php("php") && is_php("php8.3") && is_php("C:\\php\\php.exe") && !is_php("phpunit"));diff --git a/src/php/rec.php b/src/php/rec.phpindex dfd50c7..ec32909 100644--- a/src/php/rec.php+++ b/src/php/rec.php@@ -100,18 +100,129 @@ final class FastAnythingRecreturn $out;}+ // an accent, CJK, an emoji with a modifier, a combining mark+ private const NON_ASCII = ["\u{e9}", "\u{65e5}\u{672c}", "\u{1f44d}\u{1f3fd}", "e\u{301}"];++ /** Inputs where ports and originals tend to part ways, of the same type as the recorded value. */+ private static function edges($v): array+ {+ $out = [];+ if (is_int($v)) {+ foreach ([0, 1, -1] as $x) if ($x !== $v) $out[] = $x;+ } elseif (is_float($v)) {+ foreach ([0.0, 1.0, -1.0, 1e300, 5e-324] as $x) if ($x !== $v) $out[] = $x;+ } elseif (is_string($v)) {+ $chars = preg_split('//u', $v, -1, PREG_SPLIT_NO_EMPTY);+ if ($chars === false) return [];+ $mid = intdiv(count($chars), 2);+ $a = self::NON_ASCII;+ $head = implode('', array_slice($chars, 0, $mid));+ $tail = implode('', array_slice($chars, $mid));+ foreach ([$head . $a[0] . $tail, $v . $a[1], $a[2] . $v, $v . $a[3], ''] as $x) if ($x !== $v) $out[] = $x;+ } elseif (is_array($v) && $v && array_is_list($v)) {+ $out[] = [];+ foreach (array_slice(self::edges($v[0]), 0, 4) as $e) {+ $c = $v;+ $c[0] = $e;+ $out[] = $c;+ }+ }+ return $out;+ }++ /** A short version of a recorded value, so edge cases stay cheap when the real inputs are big. */+ private static function small($v)+ {+ if (is_string($v)) {+ $chars = preg_split('//u', $v, -1, PREG_SPLIT_NO_EMPTY);+ return $chars !== false && count($chars) > 64 ? implode('', array_slice($chars, 0, 64)) : $v;+ }+ return is_array($v) && array_is_list($v) && count($v) > 8 ? array_slice($v, 0, 8) : $v;+ }++ /** The value repeated $factor times (null past 4096 items or characters). */+ private static function bigger($v, int $factor)+ {+ if (is_string($v)) {+ $n = preg_match_all('/./su', $v);+ return $n && $n * $factor <= 4096 ? str_repeat($v, $factor) : null;+ }+ if (is_array($v) && $v && array_is_list($v) && count($v) * $factor <= 4096) {+ return array_merge(...array_fill(0, $factor, $v));+ }+ return null;+ }++ /** How long the original took on $args (ns), or null if it threw: outside its domain, not a fair test. */+ private static function attempt(array $args, array &$derived, array &$seen, string $tag): ?int+ {+ try {+ $enc = self::enc($args);+ if (isset($seen[$enc])) return null;+ $seen[$enc] = true;+ $t = hrtime(true);+ $r = (self::$fn)(...$args);+ $took = hrtime(true) - $t;+ $derived[] = '{"args":' . $enc . ',"result":' . self::enc($r) . ',"derived":' . $tag . '}';+ return $took;+ } catch (Throwable $e) {+ return null;+ }+ }+public static function save(): void{self::$inside = true;$derived = [];$seen = [];+ // Edge cases first, from a sample of recorded calls: special numbers, non-ASCII text, empty and+ // much bigger inputs. The original answers them; inputs it throws on are skipped.+ $start = hrtime(true);+ $edgeLeft = fn() => 1.5e9 - (hrtime(true) - $start);+ $grown = [];+ $sampled = 0;+ if (self::$fn !== null) {+ foreach (self::$args as $args) {+ if ($args === null) continue;+ if (++$sampled > 20) break;+ $base = array_map(fn($a) => self::small($a), $args);+ foreach ($base as $i => $arg) {+ foreach (self::edges($arg) as $e) {+ if (count($derived) >= 100 || $edgeLeft() <= 0) break 3;+ $next = $base;+ $next[$i] = $e;+ self::attempt($next, $derived, $seen, '"edge"');+ }+ if (isset($grown[$i]) || self::bigger($arg, 4) === null) continue;+ // much bigger inputs, only while their time (assumed quadratic) fits the budget+ $grown[$i] = true;+ try {+ $t = hrtime(true);+ (self::$fn)(...$base);+ $took = hrtime(true) - $t;+ } catch (Throwable $e) {+ continue;+ }+ // 4x bigger each step, from the short version of the input, up to 4096 items or characters+ foreach ([4, 16, 64, 256, 512] as $factor) {+ $big = self::bigger($arg, $factor);+ if ($big === null || count($derived) >= 100 || $took * 16 > $edgeLeft() / 2) break;+ $next = $base;+ $next[$i] = $big;+ $took = self::attempt($next, $derived, $seen, '"edge"');+ if ($took === null) break;+ }+ }+ }+ }+ $edges = count($derived);$start = hrtime(true);if (self::$fn !== null) {foreach (self::$args as $args) {if ($args === null) continue;foreach ($args as $i => $arg) {foreach (self::variants($arg) as $smaller) {- if (count($derived) >= 200 || hrtime(true) - $start > 3e9) break 3;+ if (count($derived) >= 200 + $edges || hrtime(true) - $start > 3e9) break 3;$next = $args;$next[$i] = $smaller;try {