コード例 #1
0
ファイル: ExpressionCounter.cs プロジェクト: evilguest/linq2d
        public static Dictionary <Expression, (int usageCount, int cost)> Evaluate(Expression expression)
        {
            var c = new ExpressionCounter();

            c.Visit(expression);
            return(c.Expressions);
        }
コード例 #2
0
        public static Expression EliminateCommonSubexpressions(this Expression expression, ref IEnumerable <ParameterExpression> variablePool)
        {
            var pool = new VariablePool(variablePool);

            do
            {
                var q = from e in ExpressionCounter.Evaluate(expression)
                        where e.Value.usageCount > 1 && e.Value.cost > 0
                        orderby e.Value.usageCount descending, e.Value.cost descending
                select e.Key;
                var mostExpensive = q.FirstOrDefault();
                if (mostExpensive == null)
                {
                    break;
                }
                // introduce a variable for mostExpensive
                var t = pool.GetVariable(mostExpensive.Type);
                // TODO: fix the order of the variables. Assignments should go after all depends
                expression = MergeBlocks(t, mostExpensive, ExpressionReplacer.Replace(expression, mostExpensive, t, new CodeComparer()));
            } while (true);
            variablePool = pool.Variables;
            return(expression);
        }