Skip to content

Commit 7e35a9e

Browse files
committed
Cast and OfType moved to instance method
1 parent 3d09d31 commit 7e35a9e

File tree

16 files changed

+1237
-1129
lines changed

16 files changed

+1237
-1129
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ var root = new DirectoryInfo("C:\\Program Files (x86)\\Steam");
167167
// FileSystemInfo(FileInfo/DirectoryInfo) can call `Ancestors`, `Children`, `Descendants`, `BeforeSelf`, `AfterSelf`
168168
var allDlls = root
169169
.Descendants()
170-
.OfType(default(FileInfo)!)
170+
.OfType<FileInfo>()
171171
.Where(x => x.Extension == ".dll");
172172

173173
var grouped = allDlls
@@ -242,7 +242,7 @@ var json = JsonNode.Parse("""
242242
var origin = json!["nesting"]!["level1"]!["level2"]!;
243243

244244
// JsonNode axis, Children, Descendants, Anestors, BeforeSelf, AfterSelf and ***Self.
245-
foreach (var item in origin.Descendants().Select(x => x.Node).OfType(default(JsonArray)!))
245+
foreach (var item in origin.Descendants().Select(x => x.Node).OfType<JsoArray>())
246246
{
247247
// [true, false, true], ["fast", "accurate", "balanced"], [1, 1, 2, 3, 5, 8, 13]
248248
Console.WriteLine(item.ToJsonString(JsonSerializerOptions.Web));
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Configs;
3+
using BenchmarkDotNet.Order;
4+
using ZLinq;
5+
6+
namespace Benchmark;
7+
8+
9+
[ShortRunJob]
10+
[MemoryDiagnoser]
11+
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
12+
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
13+
public class Select4
14+
{
15+
int[] src = Enumerable.Range(1, 1000000).ToArray();
16+
17+
[Benchmark]
18+
[BenchmarkCategory(Categories.LINQ)]
19+
public void LinqSelect1()
20+
{
21+
foreach (var _ in src.Select(x => x + x))
22+
{
23+
24+
}
25+
}
26+
27+
[Benchmark]
28+
[BenchmarkCategory(Categories.LINQ)]
29+
public void LinqSelect2()
30+
{
31+
foreach (var _ in src.Select(x => x + x).Select(x => x + x))
32+
{
33+
34+
}
35+
}
36+
37+
[Benchmark]
38+
[BenchmarkCategory(Categories.LINQ)]
39+
public void LinqSelect3()
40+
{
41+
foreach (var _ in src.Select(x => x + x).Select(x => x + x).Select(x => x + x))
42+
{
43+
44+
}
45+
}
46+
47+
[Benchmark]
48+
[BenchmarkCategory(Categories.LINQ)]
49+
public void LinqSelect4()
50+
{
51+
foreach (var _ in src.Select(x => x + x).Select(x => x + x).Select(x => x + x).Select(x => x + x))
52+
{
53+
54+
}
55+
}
56+
57+
[Benchmark]
58+
[BenchmarkCategory(Categories.ZLinq)]
59+
public void ZLinqSelect1()
60+
{
61+
foreach (var _ in src.AsValueEnumerable().Select(x => x + x))
62+
{
63+
64+
}
65+
}
66+
67+
[Benchmark]
68+
[BenchmarkCategory(Categories.ZLinq)]
69+
public void ZLinqSelect2()
70+
{
71+
foreach (var _ in src.AsValueEnumerable().Select(x => x + x).Select(x => x + x))
72+
{
73+
74+
}
75+
}
76+
77+
[Benchmark]
78+
[BenchmarkCategory(Categories.ZLinq)]
79+
public void ZLinqSelect3()
80+
{
81+
foreach (var _ in src.AsValueEnumerable().Select(x => x + x).Select(x => x + x).Select(x => x + x))
82+
{
83+
84+
}
85+
}
86+
87+
[Benchmark]
88+
[BenchmarkCategory(Categories.ZLinq)]
89+
public void ZLinqSelect4()
90+
{
91+
foreach (var _ in src.AsValueEnumerable().Select(x => x + x).Select(x => x + x).Select(x => x + x).Select(x => x + x))
92+
{
93+
94+
}
95+
}
96+
}

sandbox/Benchmark/Properties/launchSettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"Default": {
44
"commandName": "Project",
5-
"commandLineArgs": "--filter Benchmark.IterateBenchmark.*",
5+
// "commandLineArgs": "--filter Benchmark.IterateBenchmark.*",
66

77
// Available benchmarks:
88
// "commandLineArgs": "--filter Benchmark.IterateBenchmark.*",
@@ -13,6 +13,7 @@
1313
// "commandLineArgs": "--filter Benchmark.LinqPerfBenchmarks.Order00.*",
1414
// "commandLineArgs": "--filter Benchmark.LinqPerfBenchmarks.Where00.*",
1515
// "commandLineArgs": "--filter Benchmark.LinqPerfBenchmarks.Where01.*",
16+
"commandLineArgs": "--filter Benchmark.Select4.*",
1617
// "commandLineArgs": "--filter Benchmark.SimdRange.*",
1718
// "commandLineArgs": "--filter Benchmark.SimdSelect.*",
1819
// "commandLineArgs": "--filter Benchmark.LookupBattle.*",

sandbox/ConsoleApp/Program.cs

Lines changed: 76 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,35 @@
2222
//byte.MaxValue
2323
// 2147483647
2424

25-
[assembly: ZLinq.ZLinqDropInAttribute("", ZLinq.DropInGenerateTypes.Everything, DisableEmitSource = false)]
25+
[assembly: ZLinq.ZLinqDropInAttribute("", ZLinq.DropInGenerateTypes.Everything, DisableEmitSource = true)]
2626

27-
var l = new int[] { 84 }.AsValueEnumerable().Prepend(42).Last();
28-
Console.WriteLine(l);
2927

28+
Enumerable.Range(1, 10).Cast<object>().AsValueEnumerable();
3029

31-
//var root = new DirectoryInfo("C:\\Program Files (x86)\\Steam");
30+
var root = new DirectoryInfo("C:\\Program Files (x86)\\Steam");
3231

33-
//var allDlls = root
34-
// .Descendants()
35-
// .OfType(default(FileInfo)!)
36-
// .Where(x => x.Extension == ".dll");
32+
var allDlls = root
33+
.Descendants()
34+
.OfType<FileInfo>()
35+
.Where(x => x.Extension == ".dll");
3736

38-
//var grouped = allDlls
39-
// .GroupBy(x => x.Name)
40-
// .Select(x => new { FileName = x.Key, Count = x.Count() })
41-
// .OrderByDescending(x => x.Count);
37+
var grouped = allDlls
38+
.GroupBy(x => x.Name)
39+
.Select(x => new { FileName = x.Key, Count = x.Count() })
40+
.OrderByDescending(x => x.Count);
4241

43-
//foreach (var item in grouped)
44-
//{
45-
// Console.WriteLine(item);
46-
//}
42+
foreach (var item in grouped)
43+
{
44+
Console.WriteLine(item);
45+
}
4746

48-
//static IEnumerable<T> Iterate<T>(IEnumerable<T> source)
49-
//{
50-
// foreach (var item in source)
51-
// {
52-
// yield return item;
53-
// }
54-
//}
47+
static IEnumerable<T> Iterate<T>(IEnumerable<T> source)
48+
{
49+
foreach (var item in source)
50+
{
51+
yield return item;
52+
}
53+
}
5554

5655

5756
//Console.WriteLine(a);
@@ -61,62 +60,62 @@
6160

6261

6362
// System.Text.Json's JsonNode is the target of LINQ to JSON(not JsonDocument/JsonElement).
64-
//var json = JsonNode.Parse("""
65-
//{
66-
// "nesting": {
67-
// "level1": {
68-
// "description": "First level of nesting",
69-
// "value": 100,
70-
// "level2": {
71-
// "description": "Second level of nesting",
72-
// "flags": [true, false, true],
73-
// "level3": {
74-
// "description": "Third level of nesting",
75-
// "coordinates": {
76-
// "x": 10.5,
77-
// "y": 20.75,
78-
// "z": -5.0
79-
// },
80-
// "level4": {
81-
// "description": "Fourth level of nesting",
82-
// "metadata": {
83-
// "created": "2025-02-15T14:30:00Z",
84-
// "modified": null,
85-
// "version": 2.1
86-
// },
87-
// "level5": {
88-
// "description": "Fifth level of nesting",
89-
// "settings": {
90-
// "enabled": true,
91-
// "threshold": 0.85,
92-
// "options": ["fast", "accurate", "balanced"],
93-
// "config": {
94-
// "timeout": 30000,
95-
// "retries": 3,
96-
// "deepSetting": {
97-
// "algorithm": "advanced",
98-
// "parameters": [1, 1, 2, 3, 5, 8, 13]
99-
// }
100-
// }
101-
// }
102-
// }
103-
// }
104-
// }
105-
// }
106-
// }
107-
// }
108-
//}
109-
//""");
63+
var json = JsonNode.Parse("""
64+
{
65+
"nesting": {
66+
"level1": {
67+
"description": "First level of nesting",
68+
"value": 100,
69+
"level2": {
70+
"description": "Second level of nesting",
71+
"flags": [true, false, true],
72+
"level3": {
73+
"description": "Third level of nesting",
74+
"coordinates": {
75+
"x": 10.5,
76+
"y": 20.75,
77+
"z": -5.0
78+
},
79+
"level4": {
80+
"description": "Fourth level of nesting",
81+
"metadata": {
82+
"created": "2025-02-15T14:30:00Z",
83+
"modified": null,
84+
"version": 2.1
85+
},
86+
"level5": {
87+
"description": "Fifth level of nesting",
88+
"settings": {
89+
"enabled": true,
90+
"threshold": 0.85,
91+
"options": ["fast", "accurate", "balanced"],
92+
"config": {
93+
"timeout": 30000,
94+
"retries": 3,
95+
"deepSetting": {
96+
"algorithm": "advanced",
97+
"parameters": [1, 1, 2, 3, 5, 8, 13]
98+
}
99+
}
100+
}
101+
}
102+
}
103+
}
104+
}
105+
}
106+
}
107+
}
108+
""");
110109

111-
//// JsonNode
112-
//var origin = json!["nesting"]!["level1"]!["level2"]!;
110+
// JsonNode
111+
var origin = json!["nesting"]!["level1"]!["level2"]!;
113112

114-
//// JsonNode axis, Children, Descendants, Anestors, BeforeSelf, AfterSelf and ***Self.
115-
//foreach (var item in origin.Descendants().Select(x => x.Node).OfType(default(JsonArray)!))
116-
//{
117-
// // [true, false, true], ["fast", "accurate", "balanced"], [1, 1, 2, 3, 5, 8, 13]
118-
// Console.WriteLine(item.ToJsonString(JsonSerializerOptions.Web));
119-
//}
113+
// JsonNode axis, Children, Descendants, Anestors, BeforeSelf, AfterSelf and ***Self.
114+
foreach (var item in origin.Descendants().Select(x => x.Node).OfType<JsonArray>())
115+
{
116+
// [true, false, true], ["fast", "accurate", "balanced"], [1, 1, 2, 3, 5, 8, 13]
117+
Console.WriteLine(item.ToJsonString(JsonSerializerOptions.Web));
118+
}
120119

121120

122121
class Person

0 commit comments

Comments
 (0)