Skip to content

Commit 178efe9

Browse files
Rafael-SOWNetclaude
andcommitted
A sum, difference, product or quotient caches the flat list of its terms, and SimplifyHard allocates 5.4% less
#224. LinearChildren flattened a nested sum or product into a lazy chain of Concat and Select enumerables, built afresh on every enumeration -- and a difference's subtracted terms were each multiplied by -1 anew, a quotient's divisor factors each raised to -1 anew -- while every rule that reads a sum as a list reads it again for every candidate. A node is immutable, so the flat list is a property of the node: it is built once, as an array, from its parts' own cached lists, and handed out as an IReadOnlyList<Entity> from then on. Memory is linear in the tree, since a nested node's array is built from the arrays cached on its children rather than by walking down again, and no enumeration is repeated. The benchmark gate measured it: SimplifyHard allocates 171,275,272 B per operation against the 181,019,056 B recorded at 6e5986a, 5.4% less, and 8.1% less than the 186,284,672 B of the commit before this one; the other eighteen gated rows are within its tolerance, ParseHard carrying the +1.6% the quantifier grammar cost in #1421. Recorded in performance-baseline.json in the same change, as the gate's own instructions say an improvement must be. Also corrects the Modf record's summary, which said the remainder takes the sign of the dividend as C#'s % does: it is the floored remainder, (-7) mod 3 is 2, as the evaluation and Syntax.md have said since #708. Closes #224. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 89d3b0a commit 178efe9

2 files changed

Lines changed: 101 additions & 52 deletions

File tree

‎Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Operators.Classes.cs‎

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77

88
using System;
9+
using System.Collections.Generic;
10+
using HonkSharp.Laziness;
911

1012
namespace AngouriMath
1113
{
@@ -58,13 +60,33 @@ private Sumf New(Entity augend, Entity addend) =>
5860
/// would return
5961
/// <code>{ 1, x, (-1) * a / 2, b, (-1) * 4 }</code>
6062
/// </summary>
61-
internal static IEnumerable<Entity> LinearChildren(Entity tree) => tree switch
63+
/// <remarks>
64+
/// Cached on the node it is asked of, since a node is immutable and the flattening
65+
/// is asked for many times over: every rule that reads a sum as a list reads it
66+
/// again for each candidate. A nested sum's list is built from its parts' lists,
67+
/// which are cached on those parts, so the memory is linear in the tree and no
68+
/// enumeration is repeated. https://github.com/asc-community/AngouriMath/issues/224
69+
/// </remarks>
70+
internal static IReadOnlyList<Entity> LinearChildren(Entity tree) => tree switch
6271
{
63-
Sumf(var augend, var addend) => LinearChildren(augend).Concat(LinearChildren(addend)),
64-
Minusf(var minuend, var subtrahend) =>
65-
LinearChildren(minuend).Concat(LinearChildren(subtrahend).Select(entity => -1 * entity)),
72+
Sumf sum => sum.Linear,
73+
Minusf difference => difference.Linear,
6674
_ => new[] { tree }
6775
};
76+
77+
private IReadOnlyList<Entity> Linear => linear.GetValue(static @this => Flatten(LinearChildren(@this.Augend), LinearChildren(@this.Addend)), this);
78+
private LazyPropertyA<IReadOnlyList<Entity>> linear;
79+
80+
/// <summary>The two lists as one array.</summary>
81+
internal static Entity[] Flatten(IReadOnlyList<Entity> left, IReadOnlyList<Entity> right)
82+
{
83+
var flat = new Entity[left.Count + right.Count];
84+
for (var i = 0; i < left.Count; i++)
85+
flat[i] = left[i];
86+
for (var i = 0; i < right.Count; i++)
87+
flat[left.Count + i] = right[i];
88+
return flat;
89+
}
6890
}
6991

7092
/// <summary>
@@ -87,6 +109,17 @@ private Minusf New(Entity minuend, Entity subtrahend) =>
87109
public override Entity Replace(Func<Entity, Entity> func) => func(New(Minuend.Replace(func), Subtrahend.Replace(func)));
88110
/// <inheritdoc/>
89111
protected override Entity[] InitDirectChildren() => new[] { Minuend, Subtrahend };
112+
113+
/// <summary>The terms as a sum reads them: the subtrahend's terms each times -1. See <see cref="Sumf.LinearChildren"/>.</summary>
114+
internal IReadOnlyList<Entity> Linear => linear.GetValue(static @this =>
115+
{
116+
var subtracted = Sumf.LinearChildren(@this.Subtrahend);
117+
var negated = new Entity[subtracted.Count];
118+
for (var i = 0; i < negated.Length; i++)
119+
negated[i] = -1 * subtracted[i];
120+
return Sumf.Flatten(Sumf.LinearChildren(@this.Minuend), negated);
121+
}, this);
122+
private LazyPropertyA<IReadOnlyList<Entity>> linear;
90123
}
91124

92125
/// <summary>
@@ -133,13 +166,16 @@ private Mulf New(Entity multiplier, Entity multiplicand) =>
133166
/// would return
134167
/// <code>{ 1, x, (a^2)^(-1), b, 4^(-1) }</code>
135168
/// </summary>
136-
internal static IEnumerable<Entity> LinearChildren(Entity tree) => tree switch
169+
/// <remarks>Cached on the node, as <see cref="Sumf.LinearChildren"/> is.</remarks>
170+
internal static IReadOnlyList<Entity> LinearChildren(Entity tree) => tree switch
137171
{
138-
Mulf(var multiplier, var multiplicand) => LinearChildren(multiplier).Concat(LinearChildren(multiplicand)),
139-
Divf(var dividend, var divisor) =>
140-
LinearChildren(dividend).Concat(LinearChildren(divisor).Select(entity => new Powf(entity, -1))),
172+
Mulf product => product.Linear,
173+
Divf quotient => quotient.Linear,
141174
_ => new[] { tree }
142175
};
176+
177+
private IReadOnlyList<Entity> Linear => linear.GetValue(static @this => Sumf.Flatten(LinearChildren(@this.Multiplier), LinearChildren(@this.Multiplicand)), this);
178+
private LazyPropertyA<IReadOnlyList<Entity>> linear;
143179
}
144180

145181
/// <summary>
@@ -162,13 +198,26 @@ internal Divf New(Entity dividend, Entity divisor) =>
162198
public override Entity Replace(Func<Entity, Entity> func) => func(New(Dividend.Replace(func), Divisor.Replace(func)));
163199
/// <inheritdoc/>
164200
protected override Entity[] InitDirectChildren() => new[] { Dividend, Divisor };
201+
202+
/// <summary>The factors as a product reads them: the divisor's factors each to the power -1. See <see cref="Mulf.LinearChildren"/>.</summary>
203+
internal IReadOnlyList<Entity> Linear => linear.GetValue(static @this =>
204+
{
205+
var divided = Mulf.LinearChildren(@this.Divisor);
206+
var inverted = new Entity[divided.Count];
207+
for (var i = 0; i < inverted.Length; i++)
208+
inverted[i] = new Powf(divided[i], -1);
209+
return Sumf.Flatten(Mulf.LinearChildren(@this.Dividend), inverted);
210+
}, this);
211+
private LazyPropertyA<IReadOnlyList<Entity>> linear;
165212
}
166213

167214
/// <summary>
168-
/// A node of modulus, that is, the remainder after division. Follows the sign of
169-
/// the dividend, as C#'s own <c>%</c> does and as
170-
/// PeterO.Numbers does underneath: -7 % 3 is -1,
171-
/// not 2.
215+
/// A node of modulus, that is, the floored remainder after division,
216+
/// <c>a - b * floor(a / b)</c>, which takes the sign of the <em>divisor</em>:
217+
/// <c>(-7) mod 3</c> is <c>2</c> and <c>7 mod (-3)</c> is <c>-2</c>, the convention of
218+
/// SymPy, Mathematica and Maxima and the one under which the residues modulo <c>n</c>
219+
/// are <c>0</c> to <c>n - 1</c>. Not C#'s <c>%</c>, which truncates; this comment said
220+
/// the opposite of what the evaluation and the documentation do.
172221
/// </summary>
173222
public sealed partial record Modf(Entity Dividend, Entity Divisor) : ContinuousNode, IBinaryNode
174223
{

‎Sources/Tests/DotnetBenchmark/performance-baseline.json‎

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,94 @@
11
{
22
"comment": "Allocated bytes and mean nanoseconds per operation for the popular use cases of https://github.com/asc-community/AngouriMath/issues/746. Checked by PerformanceGate.cs; see Sources/AngouriMath/Docs/WhatsNew/version_performance_control.md for when updating it is legitimate.",
33
"benchmark": "CommonFunctionsInterVersion",
4-
"commit": "6e5986a79fdc06992b8a825dbc921f0950b0a2c2",
5-
"measuredOn": "2026-09-16",
4+
"commit": "89d3b0a77db4d1301a2316c3de17d048fa9b51c9",
5+
"measuredOn": "2026-09-18",
66
"runtime": ".NET 10.0.10",
77
"machine": "Ubuntu 26.04 LTS, X64, 8 logical cores",
88
"cases": {
99
"CompileEasy": {
10-
"allocatedBytes": 11047,
11-
"meanNanoseconds": 204705.0737109375
10+
"allocatedBytes": 11023,
11+
"meanNanoseconds": 185840.99401041667
1212
},
1313
"CompileHard": {
14-
"allocatedBytes": 20434,
15-
"meanNanoseconds": 328377.93669782364
14+
"allocatedBytes": 20721,
15+
"meanNanoseconds": 309511.94666466344
1616
},
1717
"Derivate": {
18-
"allocatedBytes": 53528,
19-
"meanNanoseconds": 16009.772457427978
18+
"allocatedBytes": 54519,
19+
"meanNanoseconds": 13597.12696126302
2020
},
2121
"EvalEasy": {
2222
"allocatedBytes": 0,
23-
"meanNanoseconds": 1.9644719183977162
23+
"meanNanoseconds": 1.8870621549968536
2424
},
2525
"EvalPolynomialFresh": {
26-
"allocatedBytes": 12424,
27-
"meanNanoseconds": 4162.247797648112
26+
"allocatedBytes": 12488,
27+
"meanNanoseconds": 4097.668252563477
2828
},
2929
"EvalPolynomialFresh15Digits": {
30-
"allocatedBytes": 11752,
31-
"meanNanoseconds": 4420.195562882857
30+
"allocatedBytes": 11816,
31+
"meanNanoseconds": 4256.141095987956
3232
},
3333
"EvalTranscendentalFresh": {
34-
"allocatedBytes": 34688,
35-
"meanNanoseconds": 17244.48943721887
34+
"allocatedBytes": 34720,
35+
"meanNanoseconds": 16155.153727213541
3636
},
3737
"EvalTrig": {
38-
"allocatedBytes": 69872,
39-
"meanNanoseconds": 36505.40553977273
38+
"allocatedBytes": 69904,
39+
"meanNanoseconds": 32074.484501139323
4040
},
4141
"EvalTrigPrecise": {
42-
"allocatedBytes": 336440,
43-
"meanNanoseconds": 428454.6749173678
42+
"allocatedBytes": 336472,
43+
"meanNanoseconds": 410500.50826822914
4444
},
4545
"ParseEasy": {
46-
"allocatedBytes": 18205,
47-
"meanNanoseconds": 6093.238420213972
46+
"allocatedBytes": 18405,
47+
"meanNanoseconds": 6152.299904959543
4848
},
4949
"ParseHard": {
50-
"allocatedBytes": 3555346,
51-
"meanNanoseconds": 1761245.3062065972
50+
"allocatedBytes": 3611690,
51+
"meanNanoseconds": 1439501.3920200893
5252
},
5353
"RunEasy": {
5454
"allocatedBytes": 0,
55-
"meanNanoseconds": 21.0467557828216
55+
"meanNanoseconds": 20.049601478236063
5656
},
5757
"RunHard": {
5858
"allocatedBytes": 0,
59-
"meanNanoseconds": 296.7305618066054
59+
"meanNanoseconds": 297.0782078107198
6060
},
6161
"RunMedium": {
6262
"allocatedBytes": 0,
63-
"meanNanoseconds": 164.83484596865517
63+
"meanNanoseconds": 163.4010741029467
6464
},
6565
"SimplifyEasy": {
66-
"allocatedBytes": 78802,
67-
"meanNanoseconds": 47969.87247258967
66+
"allocatedBytes": 79026,
67+
"meanNanoseconds": 45967.05927734375
6868
},
6969
"SimplifyHard": {
70-
"allocatedBytes": 181019056,
71-
"meanNanoseconds": 128834309.01041667
70+
"allocatedBytes": 171275272,
71+
"meanNanoseconds": 104278574.53333333
7272
},
7373
"SolveEasy": {
74-
"allocatedBytes": 453364,
75-
"meanNanoseconds": 221822.51813083224
74+
"allocatedBytes": 452941,
75+
"meanNanoseconds": 201415.92288643974
7676
},
7777
"SolveEasyMedium": {
78-
"allocatedBytes": 65602,
79-
"meanNanoseconds": 19954.031829833984
78+
"allocatedBytes": 65768,
79+
"meanNanoseconds": 18065.21890258789
8080
},
8181
"SolveHard": {
82-
"allocatedBytes": 10118288,
83-
"meanNanoseconds": 77972042.29347827
82+
"allocatedBytes": 10142472,
83+
"meanNanoseconds": 64934047
8484
},
8585
"SolveMedium": {
86-
"allocatedBytes": 449581,
87-
"meanNanoseconds": 336183.44418304134
86+
"allocatedBytes": 451845,
87+
"meanNanoseconds": 258112.27294921875
8888
},
8989
"SolveMediumHard": {
90-
"allocatedBytes": 1382724,
91-
"meanNanoseconds": 8940742.8125
90+
"allocatedBytes": 1393044,
91+
"meanNanoseconds": 8230367.158333333
9292
}
9393
},
9494
"ungated": {

0 commit comments

Comments
 (0)