为LINQ提速的i4o和增强功能的SLINQ

i4o是对LINQ的一个扩展,通过允许我们在对象上添加“索引”来提高LINQ运算速度,作者号称使用i4o后速度提升often over one thousand times。

我们在进行数据库查询优化时,往往第一想到的就是给Tables添加合适的Index来大幅度提升执行效率,i4o的实现也是类似这个方式,我们只要给class添加一个Indexable属性,然后使用IndexableCollection<T>来实现一个使用“索引”的类的集合就可以了,这样比起顺序性的搜索就在一定程度上提高了速度。

比如我们可以这样用:

1 ExpandedBlockStart.gif ContractedBlock.gif var customers  =   new  IndexableCollection < CnblogUser > ()  dot.gif
2ExpandedSubBlockStart.gifContractedSubBlock.gif                new Customer dot.gif{Key = 1, Name = "fanweixiao" },
3ExpandedSubBlockStart.gifContractedSubBlock.gif                new Customer dot.gif{Key = 2, Name = "lovewangshu" }
4ExpandedBlockEnd.gif}
;

i4o中对Where的扩展

 1 None.gif // extend the where when we are working with indexable collections! 
 2 None.gif          public   static  IEnumerable < T >  Where < T >
 3 None.gif        (
 4 None.gif             this  IndexableCollection < T >  sourceCollection,
 5 None.gif            Expression < Func < T,  bool >>  expr
 6 None.gif            )
 7 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 8InBlock.gif            //our indexes work from the hash values of that which is indexed, regardless of type
 9InBlock.gif            int? hashRight = null;
10InBlock.gif            bool noIndex = true;
11InBlock.gif            //indexes only work on equality expressions here
12InBlock.gif            if (expr.Body.NodeType == ExpressionType.Equal)
13ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
14InBlock.gif                //Equality is a binary expression
15InBlock.gif                BinaryExpression binExp = (BinaryExpression)expr.Body;
16InBlock.gif                //Get some aliases for either side
17InBlock.gif                Expression leftSide = binExp.Left;
18InBlock.gif                Expression rightSide = binExp.Right;
19InBlock.gif
20InBlock.gif                hashRight = GetHashRight(leftSide, rightSide);
21InBlock.gif
22InBlock.gif                //if we were able to create a hash from the right side (likely)
23InBlock.gif                if (hashRight.HasValue && HasIndexablePropertyOnLeft<T>(leftSide,sourceCollection))
24ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
25InBlock.gif                    //cast to MemberExpression - it allows us to get the property
26InBlock.gif                    MemberExpression propExp = (MemberExpression)leftSide;
27InBlock.gif                    string property = propExp.Member.Name;
28InBlock.gif                    Dictionary<int, List<T>> myIndex =
29InBlock.gif                            sourceCollection.GetIndexByProperty(property);
30InBlock.gif                    if (myIndex.ContainsKey(hashRight.Value))
31ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
32InBlock.gif                        IEnumerable<T> sourceEnum = myIndex[hashRight.Value].AsEnumerable<T>();
33InBlock.gif                        IEnumerable<T> result = sourceEnum.Where<T>(expr.Compile());
34InBlock.gif                        foreach (T item in result)
35InBlock.gif                            yield return item;
36ExpandedSubBlockEnd.gif                    }

37InBlock.gif                    noIndex = false//we found an index, whether it had values or not is another matter
38ExpandedSubBlockEnd.gif                }

39InBlock.gif
40ExpandedSubBlockEnd.gif            }

41InBlock.gif            if (noIndex) //no index?  just do it the normal slow way thendot.gif
42ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
43InBlock.gif                IEnumerable<T> sourceEnum = sourceCollection.AsEnumerable<T>();
44InBlock.gif                IEnumerable<T> result = sourceEnum.Where<T>(expr.Compile());
45InBlock.gif                foreach (T resultItem in result)
46InBlock.gif                    yield return resultItem;
47ExpandedSubBlockEnd.gif            }

48InBlock.gif
49ExpandedBlockEnd.gif        }



SLINQ则是可以让LINQ作用于streaming data上的,目前这个项目只算是个Demo版本,实现方式是为LINQ添加了一系列的扩展方法,有兴趣的朋友可以down下sourcecode来看看,需要注意的是要安装Visual Studio Orcas beta 1。

顺便帖两个codeplex上与LINQ相关的项目:

  1. LINQ是怎么来的?看LINQ-SQO
  2. 在C++/CLI上用LINQ:LINQ Extensions
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值