<commit_before>/// <reference path="../../collections/set.ts" />
/// <reference path="../../flow.ts" />

namespace Styx.Passes {
    export function optimizeEdges(graph: ControlFlowGraph) {
        let optimizedNodes = new Collections.Set<number>();
        optimizeNode(graph.entry, optimizedNodes);
    }
    
    function optimizeNode(node: FlowNode, optimizedNodes: Collections.Set<number>) {
        if (optimizedNodes.contains(node.id)) {
            return;
        }
        
        optimizedNodes.add(node.id);
        
        let isTransitNode = node.incomingEdges.length === 1 && node.outgoingEdges.length === 1; 
        let isEntryNode = node.id === 1;
        
        if (isTransitNode && !isEntryNode) {
            let outgoingEdge = node.outgoingEdges[0];
            let target = outgoingEdge.target;
            
            if (outgoingEdge.type === EdgeType.Epsilon) {
                let sourceAlreadyConnectedToTarget = false;
                
                for (let incomingTargetEdges of target.incomingEdges) {
                     if (incomingTargetEdges.source.id === node.incomingEdges[0].source.id) {
                         sourceAlreadyConnectedToTarget = true;
                         break;
                     }
                }
                
                if (!sourceAlreadyConnectedToTarget) {                
                    let incomingEdge = node.incomingEdges[0];
                    
                    // Redirect edge
                    incomingEdge.target = target;
                    target.incomingEdges = [incomingEdge];
                    
                    // Clear node
                    node.incomingEdges = [];
                    node.outgoingEdges = [];
                
                    // Recursively optimize
                    optimizeNode(target, optimizedNodes);
                }
            }
        }
        <commit_msg>Always optimize target node<commit_after>/// <reference path="../../collections/set.ts" />
/// <reference path="../../flow.ts" />

namespace Styx.Passes {
    export function optimizeEdges(graph: ControlFlowGraph) {
        let optimizedNodes = new Collections.Set<number>();
        optimizeNode(graph.entry, optimizedNodes);
    }
    
    function optimizeNode(node: FlowNode, optimizedNodes: Collections.Set<number>) {
        if (optimizedNodes.contains(node.id)) {
            return;
        }
        
        optimizedNodes.add(node.id);
        
        let isTransitNode = node.incomingEdges.length === 1 && node.outgoingEdges.length === 1; 
        let isEntryNode = node.id === 1;
        
        if (isTransitNode && !isEntryNode) {
            let outgoingEdge = node.outgoingEdges[0];
            let target = outgoingEdge.target;
            
            if (outgoingEdge.type === EdgeType.Epsilon) {
                let sourceAlreadyConnectedToTarget = false;
                
                for (let incomingTargetEdges of target.incomingEdges) {
                     if (incomingTargetEdges.source.id === node.incomingEdges[0].source.id) {
                         sourceAlreadyConnectedToTarget = true;
                         break;
                     }
                }
                
                if (!sourceAlreadyConnectedToTarget) {
                    let incomingEdge = node.incomingEdges[0];
                    
                    // Redirect edge
                    incomingEdge.target = target;
                    target.incomingEdges = [incomingEdge];
                    
                    // Clear node
                    node.incomingEdges = [];
                    node.outgoingEdges = [];
                }
                
                // Recursively optimize
                optimizeNode(target, optimizedNodes);
            }
        }
        
{'fraction_non_alphanumeric': 0.09628466771323914, 'fraction_numerical': 0.0031397174254317113, 'mean_word_length': 1.3002406738868832, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1869609', 'n_tokens_mistral': 979, 'n_tokens_neox': 904, 'n_words': 165}