狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

ZLinq:意在替代Linq的高性能.Net開源庫

admin
2025年3月20日 18:8 本文熱度 366

推薦一個開源庫,其功能已經(jīng)完全包含LINQ的所有方法,完全可以替代Linq。而且其有更高的性能和低內(nèi)存占用的特點。

?

01

項目簡介

ZLinq 是一個由 Cysharp 團(tuán)隊開發(fā)的開源項目,目標(biāo)是為所有 .NET 平臺和 Unity 提供零分配的 LINQ 實現(xiàn)。它通過利用 Span 和 SIMD 技術(shù),優(yōu)化了 LINQ 的性能,同時提供了對樹形結(jié)構(gòu)(如文件系統(tǒng)、JSON、游戲?qū)ο蟮龋┑牟樵冎С帧?/span>

通過一行代碼,調(diào)用AsValueEnumerable() 方法,用戶可以將任何Linq轉(zhuǎn)換為 ZLinq。

using ZLinq;


var seq = source

    .AsValueEnumerable() // 添加此代碼

    .Where(x => x % 2 == 0)

    .Select(x => x * 3);

02

核心特征

零分配 LINQ

傳統(tǒng)的 LINQ 實現(xiàn)雖然強大,但在處理大量數(shù)據(jù)時可能會因為頻繁的內(nèi)存分配而導(dǎo)致性能瓶頸。ZLinq 通過結(jié)構(gòu)體(struct)的方式實現(xiàn)可枚舉集合,避免了傳統(tǒng) LINQ 中由于頻繁創(chuàng)建對象而導(dǎo)致的內(nèi)存分配問題。

對 Span 的支持

ZLinq 充分利用了 .NET 9/C# 13 中引入的 allows ref struct 特性,支持對 Span 的操作。這意味著用戶可以在支持該特性的環(huán)境中,對 Span 類型進(jìn)行高效的 LINQ 查詢操作。

LINQ to SIMD

ZLinq 自動應(yīng)用 SIMD(單指令多數(shù)據(jù))優(yōu)化,以提升性能。用戶可以通過自定義方式進(jìn)一步優(yōu)化 SIMD 的使用,以滿足特定需求。

LINQ to Tree

ZLinq 擴(kuò)展了 LINQ 的概念,使其能夠應(yīng)用于樹形結(jié)構(gòu)的查詢。它提供了類似 LINQ to XML 的軸操作(如 Ancestors、Children、Descendants、BeforeSelf 和 AfterSelf),可以對文件系統(tǒng)、JSON、Unity 的 GameObject 等樹形結(jié)構(gòu)進(jìn)行查詢。


03

入門指南

1、AsValueEnumerable(),即可使用 ZLinq 的零分配 LINQ。

using ZLinq;


var source = new int[] { 1, 2, 3, 4, 5 };


// 調(diào)用 AsValueEnumerable 以應(yīng)用 ZLinq

var seq1 = source.AsValueEnumerable().Where(x => x % 2 == 0);


// 也可以應(yīng)用于 Span(僅在支持 allows ref struct 的 .NET 9/C# 13 環(huán)境中)

Span<int> span = stackalloc int[5] { 1, 2, 3, 4, 5 };

var seq2 = span.AsValueEnumerable().Select(x => x * x);

2、LINQ to Tree
LINQ to XML 將圍繞軸進(jìn)行查詢的概念引入到了 C# 中。即使你不使用 XML,類似的 API 也被納入了 Roslyn,并有效地用于探索語法樹。ZLinq 擴(kuò)展了這一概念,使其適用于任何可以被視為樹形結(jié)構(gòu)的對象,允許應(yīng)用 Ancestors、Children、Descendants、BeforeSelf 和 AfterSelf。
具體來說,通過定義一個實現(xiàn)了以下接口的結(jié)構(gòu)體,即可使其可迭代:

public interface ITraverser<TTraverser, T> : IDisposable

    where TTraverser : struct, ITraverser<TTraverser, T> // 自身

{

    T Origin { get; }

    TTraverser ConvertToTraverser(T next); // 用于 Descendants

    bool TryGetHasChild(out bool hasChild); // 可選:優(yōu)化 Descendants 的使用

    bool TryGetChildCount(out int count);   // 可選:優(yōu)化 Children 的使用

    bool TryGetParent(out T parent); // 用于 Ancestors

    bool TryGetNextChild(out T child); // 用于 Children | Descendants

    bool TryGetNextSibling(out T next); // 用于 AfterSelf

    bool TryGetPreviousSibling(out T previous); // BeforeSelf

}

3、文件系統(tǒng)

//安裝依賴庫

dotnet add package ZLinq.FileSystem?

using ZLinq;


var root = new DirectoryInfo("C:\\Program Files (x86)\\Steam");


// FileSystemInfo(FileInfo/DirectoryInfo) 可以調(diào)用 `Ancestors`、`Children`、`Descendants`、`BeforeSelf`、`AfterSelf`

var allDlls = root

    .Descendants()

    .OfType(default(FileInfo)!)

    .Where(x => x.Extension == ".dll");


var grouped = allDlls

    .GroupBy(x => x.Name)

    .Select(x => new { FileName = x.Key, Count = x.Count() })

    .OrderByDescending(x => x.Count);


foreach (var item in grouped)

{

    Console.WriteLine(item);

}

4、JSON(System.Text.Json)

//安裝依賴庫

dotnet add package ZLinq.Json

using ZLinq;


// System.Text.Json 的 JsonNode 是 LINQ to JSON 的目標(biāo)(不是 JsonDocument/JsonElement)。

var json = JsonNode.Parse("""

{

    "nesting": {

      "level1": {

        "description": "First level of nesting",

        "value": 100,

        "level2": {

          "description": "Second level of nesting",

          "flags": [true, false, true],

          "level3": {

            "description": "Third level of nesting",

            "coordinates": {

              "x": 10.5,

              "y": 20.75,

              "z": -5.0

            },

            "level4": {

              "description": "Fourth level of nesting",

              "metadata": {

                "created": "2025-02-15T14:30:00Z",

                "modified": null,

                "version": 2.1

              },

              "level5": {

                "description": "Fifth level of nesting",

                "settings": {

                  "enabled": true,

                  "threshold": 0.85,

                  "options": ["fast", "accurate", "balanced"],

                  "config": {

                    "timeout": 30000,

                    "retries": 3,

                    "deepSetting": {

                      "algorithm": "advanced",

                      "parameters": [1, 1, 2, 3, 5, 8, 13]

                    }

                  }

                }

              }

            }

          }

        }

      }

    }

}

""");


// JsonNode

var origin = json!["nesting"]!["level1"]!["level2"]!;


// JsonNode axis, Children, Descendants, Anestors, BeforeSelf, AfterSelf and ***Self.

foreach (var item in origin.Descendants().Select(x => x.Node).OfType<JsoArray>())

{

    // [true, false, true], ["fast", "accurate", "balanced"], [1, 1, 2, 3, 5, 8, 13]

    Console.WriteLine(item.ToJsonString(JsonSerializerOptions.Web));

}


04

項目地址

https://github.com/Cysharp/ZLinq


- End -

該文章在 2025/3/21 12:46:42 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved