From 09eb28ee1dbbcaab5200b40c9f9dfacec7366229 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Wed, 23 Apr 2025 14:20:48 +0200 Subject: [PATCH] LibRegex: Better estimate the cost of laying out alts as a chain Previously we were counting the total number of *nodes* in the tree for the chain cost, which greatly underestimated its cost when large bytecode entries were present, This commit switches to estimating it using the total bytecode *size*, which is a closer value to the true cost than the tree node count. This corresponds to a ~4x perf improvement on / alternatives // This is really only worth it if we don't blow up the size by the 2-extra-instruction-per-node scheme, similarly, if no nodes are shared, we're better off not using a tree. auto tree_cost = (total_nodes - common_hits) * 2; - auto chain_cost = total_nodes + alternatives.size() * 2; + auto chain_cost = total_bytecode_entries_in_tree + alternatives.size() * 2; dbgln_if(REGEX_DEBUG, "Total nodes: {}, common hits: {} (tree cost = {}, chain cost = {})", total_nodes, common_hits, tree_cost, chain_cost); if (common_hits == 0 || tree_cost > chain_cost) {