TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

“ZLinq”, a Zero-Allocation LINQ Library for .NET

269 点作者 cempaka5 天前

12 条评论

zamalek4 天前
<i>In theory</i> .Net 10 should make this obsolete, the headline features[1] are basically all about this. In practice, well, it&#x27;s heuristics, I&#x27;m adding this to a particularly performance sensitive project right now :)<p>Edit: what&#x27;s also nice is that C# recognizes Linq as a contract. So long as this has the correct method names and signatures (it does), the Linq <i>syntax</i> will light up automatically. You can also use this trick for your own home-grown things (add Select, Join, Where, etc. overloads) if the Linq syntax is something you like.<p>[1]: <a href="https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;core&#x2F;whats-new&#x2F;dotnet-10&#x2F;runtime" rel="nofollow">https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;core&#x2F;whats-new&#x2F;dotn...</a>
评论 #44047308 未加载
jasonthorsness4 天前
This is great. I&#x27;ve worked on production .NET services and we often had to avoid LINQ in hot paths because of the allocations. Reimplementing functions with for-loops and other structures was time-consuming and error-prone compared to LINQ method chaining. Chaining LINQ methods is extremely powerful; like filter, map, and reduce in JS but with a bunch of other operators and optimizations. I wish more languages had something like it.
评论 #44050073 未加载
评论 #44047621 未加载
bigmattystyles4 天前
This is cool - excited to try it - I would note that I&#x27;ve been a dotnet grunt for almost 15 years now. I am good at it, I know how to use the language, I know the ecosystem - this level of familiarity with the language is just not within my grasp. I can understand the code (mostly) reading it, but I never would have been able to conjure up, let alone implement this. Props to the author.
martijn_himself4 天前
Could anyone more across the detail of this chime in on what this means for the &#x27;average&#x27; .NET developer?<p>I rely heavily on LINQ calls in a .NET (Core) Web App, should I replace these with Zlinq calls?<p>Or is this only helpful in case you want to do LINQ operations on let&#x27;s say 1000&#x27;s of objects that would get garbage collected at the end of a game loop?
评论 #44050084 未加载
评论 #44052053 未加载
评论 #44049765 未加载
KallDrexx4 天前
This is neat, but how does this get away with being zero allocation? It appears to use `Func&lt;T,U&gt;` for its predicates, and since `Func&lt;T&gt;` is a reference type this will allocate. The IL definitely generates definitely seems like it&#x27;s forming a reference type from what I can tell.
评论 #44047662 未加载
HexDecOctBin4 天前
What features does C# has that makes LINQ possible in it and not in other languages?
评论 #44049433 未加载
评论 #44047279 未加载
评论 #44048348 未加载
评论 #44047839 未加载
评论 #44047633 未加载
评论 #44051213 未加载
评论 #44047544 未加载
评论 #44047277 未加载
stuaxo4 天前
I don&#x27;t use .NET, but always thought LINQ a really interesting part of it.
incoming12115 天前
Is there a reason these sort of improvements cannot be contributed back into .NET itself?
评论 #44046940 未加载
评论 #44046808 未加载
评论 #44047457 未加载
评论 #44047943 未加载
评论 #44048323 未加载
评论 #44046793 未加载
srean4 天前
I don&#x27;t know if anyone remembers expression templates of C++ nowadays. Once upon a time I had written a library of expression templates primitives to chain streams of computations and maps so that the sub-streams do not need to be fully realized in memory - essentially the old idea of Unix pipelines.<p>Writing it was a lot of fun. Debugging compiler errors were a lot less fun because the template language of C++ has no static typing, so errors would be triggered very deep in the expression tree. The expression tree got processed and inlined at compile time so there was no to minimal overhead at runtime.<p>I was very impressed with GCC&#x27;s inlining and vectorization. Especially the messages that explained why it could not vectorize.
ImHereToVote4 天前
Why is LINQ allocating memory in the first place?
评论 #44048810 未加载
评论 #44048978 未加载
torginus4 天前
Is it just me, or does this have a case of false advertising?<p>One of the biggest sources of allocation is lambda captures, like when you write something like this<p><pre><code> var myPerson = people.First(x=&gt;x.Name==myPersonName); </code></pre> in this case, a phantom object is allocated, that captures the myPersonName variable, for which a delegate is allocated, which is then passed to the First() method, making the number of allocations per call a minimum of 2. I don&#x27;t see ZLinq doing anything about this.
garganzol4 天前
Interesting approach, but it should be .NET Runtime (or JIT) who would optimize small memory allocations preferring the stack rather than the heap when possible.<p>.NET 10 takes a step in that direction [1].<p>[1] <a href="https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;core&#x2F;whats-new&#x2F;dotnet-10&#x2F;runtime#stack-allocation-of-small-arrays-of-value-types" rel="nofollow">https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;core&#x2F;whats-new&#x2F;dotn...</a>