<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace NMF.Expressions.Execution
{
    public class OnDemandExecutionEngine : ExecutionEngine
    {
        private HashSet<INotifiable> changeSources = new HashSet<INotifiable>();

        public void Execute()
        {
            NotifyLoop(changeSources);
            changeSources.Clear();
        }

        protected override void OnPropertyChanged(HashSet<INotifiable> handler)
        {
            foreach (var source in handler)
                changeSources.Add(source);
        }

        private void NotifyLoop(HashSet<INotifiable> sources)
        {
            MarkAffectedNodes(sources);

            foreach (var source in sources)
            {
                int currentValue = 1;
                bool reevaluating = true;
                var node = source;

                while (node != null)
                {
                    node.ExecutionMetaData.RemainingVisits -= currentValue;
                    if (node.ExecutionMetaData.RemainingVisits == 0)
                    {
                        INotificationResult result = null;
                        if (reevaluating)
                        {
                            result = node.Notify(node.ExecutionMetaData.Sources);
                            reevaluating = result.Changed;
                            currentValue = node.ExecutionMetaData.TotalVisits;
                            node.ExecutionMetaData.TotalVisits = 0;
                            node.ExecutionMetaData.Sources.Clear();
                        }
                        
                        node = node.Successors[0];
                        if (node != null && result != null && reevaluating)
                            node.ExecutionMetaData.Sources.Add(result);
                    }
                    else
                        break;
                }
            }
        }

        private void MarkAffectedNodes(HashSet<INotifiable> sources)
        {
            foreach (var node in sources)<commit_msg>Made the NotifyLoop tail-recursive. Performance is now better than master once again.<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace NMF.Expressions.Execution
{
    public class OnDemandExecutionEngine : ExecutionEngine
    {
        private HashSet<INotifiable> changeSources = new HashSet<INotifiable>();

        public void Execute()
        {
            NotifyLoop(changeSources);
            changeSources.Clear();
        }

        protected override void OnPropertyChanged(HashSet<INotifiable> handler)
        {
            foreach (var source in handler)
                changeSources.Add(source);
        }

        private void NotifyLoop(HashSet<INotifiable> sources)
        {
            MarkAffectedNodes(sources);

            foreach (var source in sources)
            {
                NotifyNode(source, 1, true);
            }
        }

        private void NotifyNode(INotifiable node, int currentValue, bool evaluating)
        {
            if (node == null)
                return;

            node.ExecutionMetaData.RemainingVisits -= currentValue;
            if (node.ExecutionMetaData.RemainingVisits > 0)
                return;
            
            INotificationResult result = null;
            if (evaluating)
            {
                result = node.Notify(node.ExecutionMetaData.Sources);
                evaluating = result.Changed;
                currentValue = node.ExecutionMetaData.TotalVisits;
                node.ExecutionMetaData.TotalVisits = 0;
                node.ExecutionMetaData.Sources.Clear();
            }

            var nextNode = node.Successors[0];
            if (nextNode != null)
            {
                if (result != null && evaluating)
                    nextNode.ExecutionMetaData.Sources.Add(result);

                NotifyNode(nextNode, currentValue, evaluating);
            }
        }

        private void MarkAffectedNodes(HashSet<INotifiable> sources)
        {
            foreach (var node in sources)
{'fraction_non_alphanumeric': 0.07234345351043643, 'fraction_numerical': 0.0018975332068311196, 'mean_word_length': 1.487905604719764, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, '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': '978999', 'n_tokens_mistral': 1004, 'n_tokens_neox': 955, 'n_words': 213}