0

它似乎是这样工作的,但它是在规范中的某处说明还是只是我不能真正依赖的实现细节?我试图通过只创建一次表达式树并缓存它来加快属性/字段名称的提取速度。我通过将树包装在 lambda 中并将其用作缓存的键来做到这一点。如果运行时决定每次遇到相同的 lambda 表达式时都创建一个新的委托,那么它将会非常糟糕。

// KeyValuePair<string, T> GetPair<T>(Func<Expression<Func<T>>> val)...
var item = new Item { Num = 42 };
var pair = GetPair(() => () => item.Num); // guaranteed to be the same instance?
// pair.Key = "Num"
// pair.Value = 42

编辑: 好的,这是完整的东西。似乎它有效,并且在此过程中似乎没有产生任何垃圾。

另一个编辑: 好的,改变它,这似乎没有捕获任何东西,而且它工作得更快!

using System;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;

class Program
{
    static void Main(string[] args) {
        var pair = new Pair<int>();
        var pair2 = new Pair<string>();
        var item = new Item { Num = 42, Word = "Answer" };
        double ratio = 1;
        var sw = Stopwatch.StartNew();
        for (int i = 0;; i++) {
            if ((i & 0xFFF) == 0 && sw.ElapsedMilliseconds > 2000) {
                Console.WriteLine("literal: {0:N0}", i);
                ratio *= i;
                break;
            }
            Assign(pair, "Num", item.Num); Assign(pair2, "Word", item.Word);
            Assign(pair, "Num", item.Num); Assign(pair2, "Word", item.Word);
            Assign(pair, "Num", item.Num); Assign(pair2, "Word", item.Word);
            Assign(pair, "Num", item.Num); Assign(pair2, "Word", item.Word);
        }
        sw = Stopwatch.StartNew();
        for (int i = 0; ; i++) {
            if ((i & 0xFFF) == 0 && sw.ElapsedMilliseconds > 2000) {
                item = new Item { Num = 42, Word = "Answer" };
                Console.WriteLine("expression: {0:N0}", i);
                ratio /= i;
                break;
            }
            Assign4(pair, item, () => it => it.Num); Assign4(pair2, item, () => it => it.Word);
            Assign4(pair, item, () => it => it.Num); Assign4(pair2, item, () => it => it.Word);
            Assign4(pair, item, () => it => it.Num); Assign4(pair2, item, () => it => it.Word);
            Assign4(pair, item, () => it => it.Num); Assign4(pair2, item, () => it => it.Word);

        }
        Console.WriteLine(ratio.ToString("F3"));
        Console.ReadLine();
    }

    static void Assign<T>(Pair<T> pair, string name, T value) {
        pair.Name = name;
        pair.Value = value;
    }

    static void Assign4<T, U>(Pair<T> pair, U item, Func<Expression<Func<U, T>>> value,
        [CallerFilePath]string path = "", [CallerLineNumber]int line = 0) {

        int key = ((path.Length << 20) + line) % Cache<U, T>.Length;
//        int key = value.GetHashCode() % Cache<T>.Length;
        while (true) {
            var bucket = Cache<U, T>.Records[key];
            if (bucket.Literal == null) break;
            if (object.ReferenceEquals(bucket.Literal, value)) {
                pair.Name = bucket.FieldName;
                pair.Value = bucket.Getter(item);
                return;
            }
            key += 1;
            if (key == Cache<U, T>.Length) key = 0;
        }
        var tree = value();
        var getter = tree.Compile();
        string name = (tree.Body as MemberExpression).Member.Name;
        Cache<U, T>.Records[key] = new Cache<U, T>.Record {
            Literal = value,
            FieldName = name,
            Getter = getter,
        };
        pair.Name = name;
        pair.Value = getter(item);
    }
}

class Cache<U, T>
{
    public struct Record
    {
        public Func<Expression<Func<U, T>>> Literal;
        public string FieldName;
        public Func<U, T> Getter;
    }
    public const int Length = 997;
    public static Record[] Records = new Record[Length];
}

class Pair<T>
{
    public string Name;
    public T Value;
}

class Item
{
    public int Num;
    public string Word;
}
4

1 回答 1

4

在这种情况下,它不能是同一个实例 -item每次执行这对行时,捕获的变量 ( ) 都会不同。

即使它可以是同一个实例,也不能保证。根据我对 MS C# 编译器的记忆,不捕获任何变量(甚至不捕获this)的 lambda 表达式将缓存在静态变量中 - 但我不确定是否还有其他内容。

于 2013-07-31T12:11:01.300 回答