diff --git a/Sources/.editorconfig b/Sources/.editorconfig index de2d5e9dc..a26192573 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -1129,3 +1129,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [Tests/UnitTests/Convenience/ShadersTest.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n + +[AngouriMath/Core/Entity/Entity.Nodes.cs] +file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n + +[Tests/UnitTests/Core/NodesWalkTest.cs] +file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n diff --git a/Sources/AngouriMath/Core/Entity/Entity.Definition.cs b/Sources/AngouriMath/Core/Entity/Entity.Definition.cs index 4e84a7a53..b0baa4788 100644 --- a/Sources/AngouriMath/Core/Entity/Entity.Definition.cs +++ b/Sources/AngouriMath/Core/Entity/Entity.Definition.cs @@ -186,7 +186,9 @@ public virtual bool Equals(Entity? other) internal virtual IReadOnlyList MatchableChildren => DirectChildren; /// A depth-first enumeration is required by - /// + /// . Nothing is cached: + /// each enumeration walks the tree afresh, at the cost of one object and none per node + /// (see ). /// /// The list of all subnodes of the given expression, including its own. Traverses /// the tree the following order: @@ -212,8 +214,7 @@ public virtual bool Equals(Entity? other) /// 3 /// /// - public IEnumerable Nodes => nodes.GetValue(static @this => @this.DirectChildren.SelectMany(c => c.Nodes).Prepend(@this), this); - private LazyPropertyA> nodes; + public IEnumerable Nodes => new NodeWalk(this); /// /// Applies the given function to every node starting from the leaves @@ -775,7 +776,7 @@ private static IReadOnlyCollection BoundBy(Entity binder, params Entit /// True /// /// - public bool ContainsNode(Entity x) => x is Variable v ? VarsAndConsts.Contains(v) : Nodes.Contains(x); + public bool ContainsNode(Entity x) => x is Variable v ? VarsAndConsts.Contains(v) : WalkFinds(x); /// /// Implicit conversation from string to Entity. See diff --git a/Sources/AngouriMath/Core/Entity/Entity.Nodes.cs b/Sources/AngouriMath/Core/Entity/Entity.Nodes.cs new file mode 100644 index 000000000..5c173d635 --- /dev/null +++ b/Sources/AngouriMath/Core/Entity/Entity.Nodes.cs @@ -0,0 +1,216 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System; +using System.Buffers; +using System.Collections; +using System.Collections.Generic; + +namespace AngouriMath +{ + partial record Entity + { + /// Whether any node, this one included, satisfies , + /// asked in the order of and stopping at the first; what + /// Nodes.Any(predicate) answers, without allocating. + internal bool AnyNode(Func predicate) + { + var walk = new NodeStack(this); + try + { + while (walk.MoveNext()) + if (predicate(walk.Current)) + return true; + return false; + } + finally { walk.Release(); } + } + + /// What Nodes.All(predicate) answers, without allocating. + internal bool AllNodes(Func predicate) + { + var walk = new NodeStack(this); + try + { + while (walk.MoveNext()) + if (!predicate(walk.Current)) + return false; + return true; + } + finally { walk.Release(); } + } + + /// What Nodes.Contains(node) answers, without allocating. + private bool WalkFinds(Entity node) + { + var walk = new NodeStack(this); + try + { + while (walk.MoveNext()) + if (walk.Current == node) + return true; + return false; + } + finally { walk.Release(); } + } + + /// How many nodes, this one included, are equal to . + internal int CountNode(Entity node) + { + var walk = new NodeStack(this); + try + { + var count = 0; + while (walk.MoveNext()) + if (walk.Current == node) + count++; + return count; + } + finally { walk.Release(); } + } + + /// + /// The pre-order walk behind : the node, then each child's walk in + /// order, kept on an explicit stack rented from . + /// + /// + /// + /// It allocates nothing, per node or per walk, and keeps nothing on the tree. A chain of + /// LINQ iterators costs an iterator per node per enumeration, and caching it on every + /// node keeps that chain alive as long as the node; caching the walk's result instead is + /// quadratic in memory, since every node would hold its whole subtree (31 MB on a sum of + /// 2000 terms). + /// #534 + /// + /// + /// Every walk has its own stack, so a predicate that walks another tree while this one + /// is being walked, on this thread or another, shares nothing with it. A walk that is + /// abandoned without only fails to give its array back to the + /// pool. It is a mutable struct: copying one mid-walk would share the array, so it is + /// only ever held in a local or in . + /// + /// + private struct NodeStack + { + private Entity? next; + private Entity? current; + private Entity[]? stack; + private int count; + + internal NodeStack(Entity root) + { + next = root; + current = null; + stack = null; + count = 0; + } + + internal readonly Entity Current => current!; + + internal bool MoveNext() + { + if (next is not null) + { + current = next; + next = null; + return true; + } + if (current is null) + return false; + // The children of the node just yielded are pushed only now, so a walk stopped + // at a node never asks for its children. + var children = current.DirectChildren; + if (children.Count > 0) + { + Reserve(children.Count); + for (var i = children.Count - 1; i >= 0; i--) + stack![count++] = children[i]; + } + if (count == 0) + { + Release(); + return false; + } + current = stack![--count]; + stack[count] = null!; + return true; + } + + private void Reserve(int more) + { + if (stack is null) + stack = ArrayPool.Shared.Rent(Math.Max(16, more)); + else if (count + more > stack.Length) + { + var larger = ArrayPool.Shared.Rent(Math.Max(2 * stack.Length, count + more)); + Array.Copy(stack, larger, count); + Array.Clear(stack, 0, count); + ArrayPool.Shared.Return(stack); + stack = larger; + } + } + + internal void Release() + { + if (stack is not null) + { + // The pool hands the array to the next renter, which must not keep this + // tree alive through it. + Array.Clear(stack, 0, count); + ArrayPool.Shared.Return(stack); + stack = null; + } + count = 0; + next = null; + current = null; + } + } + + /// + /// as the that + /// publishes, at one object per enumeration. + /// + private sealed class NodeWalk : IEnumerable, IEnumerator + { + private readonly Entity root; + private readonly int ownerThread; + private bool handedOut; + private NodeStack walk; + + internal NodeWalk(Entity root) + { + this.root = root; + ownerThread = Environment.CurrentManagedThreadId; + walk = new NodeStack(root); + } + + // The first enumeration on the thread that asked for Nodes is this object itself, as + // LINQ's own iterators do, so the common Nodes.Any(...) costs one object, not two. + public IEnumerator GetEnumerator() + { + if (!handedOut && ownerThread == Environment.CurrentManagedThreadId) + { + handedOut = true; + return this; + } + return new NodeWalk(root) { handedOut = true }; + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + public Entity Current => walk.Current; + + object IEnumerator.Current => Current; + + public bool MoveNext() => walk.MoveNext(); + + public void Dispose() => walk.Release(); + + public void Reset() => throw new NotSupportedException(); + } + } +} diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs index f03b51ab0..e350809f0 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs @@ -61,12 +61,12 @@ public static Entity GetMinimumSubtree(Entity expr, Variable x) // The idea is the following: // We must get a subtree that has more occurances than 1, // But at the same time it should cover all references to `ent` - var xs = expr.Nodes.Count(child => child == x); + var xs = expr.CountNode(x); return expr.Nodes .TakeWhile(e => e != x) // Requires Entity enumeration to be depth-first!! .Where(e => e.ContainsNode(x)) // e.g. when expr is sin((x+1)^2)+3, this step results in [sin((x+1)^2)+3, sin((x+1)^2), (x+1)^2, x+1] - .LastOrDefault(sub => expr.Nodes.Count(child => child == sub) * sub.Nodes.Count(child => child == x) == xs) + .LastOrDefault(sub => expr.CountNode(sub) * sub.CountNode(x) == xs) // if `expr` contains 2 `sub`s and `sub` contains 3 `x`s, then there should be 6 `x`s in `expr` (6 == `xs`) ?? x; } diff --git a/Sources/AngouriMath/Functions/Simplification/Simplificator.cs b/Sources/AngouriMath/Functions/Simplification/Simplificator.cs index 58711a61b..ec1dc62f1 100644 --- a/Sources/AngouriMath/Functions/Simplification/Simplificator.cs +++ b/Sources/AngouriMath/Functions/Simplification/Simplificator.cs @@ -51,7 +51,7 @@ internal static Entity Simplify(Entity expr, int level) // one has no runs before it. See simplifications. Not while a limit reads an // approach, on which logarithms gather that do not gather on their own account: // the same tree simplified on two approaches, or on one and on none, is two answers. - if (simplifying == 1 || RewriteRecording.Current is not null || !expr.Nodes.All(IsElementary) + if (simplifying == 1 || RewriteRecording.Current is not null || !expr.AllNodes(IsElementary) || Algebra.LimitFunctional.IsReadingAnApproach) return SimplifyAtDepth(expr, level); var kept = simplifications ??= new(); @@ -140,7 +140,7 @@ internal static Entity Simplify(Entity expr, int level) /// internal static Entity Expanded(Entity expr) { - if (simplifying == 0 || RewriteRecording.Current is not null || !expr.Nodes.All(IsElementary)) + if (simplifying == 0 || RewriteRecording.Current is not null || !expr.AllNodes(IsElementary)) return expr.Expand(); var kept = expansions ??= new(); if (kept.TryGetValue(expr, out var known) && Convenience.SettingsState.StillHolds(known.Under)) @@ -447,11 +447,11 @@ void __IterAddHistory(Entity expr) // the same -- and a tie is settled by whichever candidate came first, which // is an accident rather than a preference. // https://github.com/asc-community/AngouriMath/issues/205 - if (res.Nodes.Any(child => child is Divf)) + if (res.AnyNode(child => child is Divf)) AddHistory(res = Simplified(recording, res.Rewrite(RewriteRules.RationalizeDenominator))); res = Simplified(recording, res.Rewrite(RewriteRules.CanonicalOrderAt(sortLevel))); - if (res.Nodes.Any(child => child is Powf)) + if (res.AnyNode(child => child is Powf)) AddHistory(res = Simplified(recording, res.Rewrite(RewriteRules.Power))); AddHistory(res = SimplifyChildren(res)); @@ -464,7 +464,7 @@ void __IterAddHistory(Entity expr) // 3x^2 (u + 2)^2 / (3x^2) is (u + 2)^2, which the rules below reach as it // stands, and u^2 + 4u + 4 only once this has expanded it. The complexity // metric is what should decide between them. - if (res.Nodes.Any(child => child is Divf)) + if (res.AnyNode(child => child is Divf)) AddHistory(Simplified(recording, res.Rewrite(RewriteRules.PolynomialGcdCancellation))); AddHistory(res = Simplified(recording, res.Rewrite(RewriteRules.PolynomialLongDivision))); @@ -478,7 +478,7 @@ void __IterAddHistory(Entity expr) AddHistory(res = Simplified(recording, res.Rewrite(RewriteRules.Trigonometric))); AddHistory(res = Simplified(recording, res.Rewrite(RewriteRules.CollapseTrigonometricFunctions))); - if (res.Nodes.Any(child => child is TrigonometricFunction)) + if (res.AnyNode(child => child is TrigonometricFunction)) { // The Pythagorean pair found anywhere in a sum -- the binary rule above sees // it only where the sort has made it a node. Inside this block, since the @@ -509,30 +509,30 @@ void __IterAddHistory(Entity expr) } - if (res.Nodes.Any(child => child is Statement)) + if (res.AnyNode(child => child is Statement)) { AddHistory(res = Simplified(recording, res.Rewrite(RewriteRules.Boolean))); } - if (res.Nodes.Any(child => child is ComparisonSign)) + if (res.AnyNode(child => child is ComparisonSign)) { AddHistory(res = Simplified(recording, res.Rewrite(RewriteRules.InequalityEquality))); } // A binomial coefficient is three factorials, and its identities live in the // gathering set. https://github.com/asc-community/AngouriMath/issues/1409 - if (res.Nodes.Any(child => child is Factorialf or Binomialf)) + if (res.AnyNode(child => child is Factorialf or Binomialf)) { AddHistory(res = Simplified(recording, res.Rewrite(RewriteRules.ExpandFactorialDivisions))); AddHistory(res = Simplified(recording, res.Rewrite(RewriteRules.FactorizeFactorialMultiplications))); } - if (res.Nodes.Any(child => child is Powf or Logf)) + if (res.AnyNode(child => child is Powf or Logf)) AddHistory(res = Simplified(recording, res.Rewrite(RewriteRules.Power))); - if (res.Nodes.Any(child => child is Set)) + if (res.AnyNode(child => child is Set)) { var replaced = res.Rewrite(RewriteRules.SetOperator); @@ -540,7 +540,7 @@ void __IterAddHistory(Entity expr) } - if (res.Nodes.Any(child => child is Phif)) + if (res.AnyNode(child => child is Phif)) AddHistory(res = Simplified(recording, res.Rewrite(RewriteRules.PhiFunction))); Entity? possiblePoly = null; diff --git a/Sources/Tests/DotnetBenchmark/performance-baseline.json b/Sources/Tests/DotnetBenchmark/performance-baseline.json index 525ae45e7..b9c89d332 100644 --- a/Sources/Tests/DotnetBenchmark/performance-baseline.json +++ b/Sources/Tests/DotnetBenchmark/performance-baseline.json @@ -1,94 +1,94 @@ { "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.", "benchmark": "CommonFunctionsInterVersion", - "commit": "6790f7ca40adde0d5e83253aac9cd6e6c9e2cadb", - "measuredOn": "2026-11-15", + "commit": "6dfa9200ef039228f1588a3db3a53fefbe16bc38", + "measuredOn": "2026-11-16", "runtime": ".NET 10.0.15", "machine": "Ubuntu 26.04.1 LTS, X64, 4 logical cores", "cases": { "CompileEasy": { - "allocatedBytes": 15490, - "meanNanoseconds": 411819.7227864583 + "allocatedBytes": 11346, + "meanNanoseconds": 338028.84748186386 }, "CompileHard": { - "allocatedBytes": 31383, - "meanNanoseconds": 511184.2274088542 + "allocatedBytes": 20046, + "meanNanoseconds": 409314.61321614584 }, "Derivate": { - "allocatedBytes": 57227, - "meanNanoseconds": 29320.19936726888 + "allocatedBytes": 56201, + "meanNanoseconds": 23194.351304117838 }, "EvalEasy": { "allocatedBytes": 0, - "meanNanoseconds": 3.739564356704553 + "meanNanoseconds": 3.0131985463202 }, "EvalPolynomialFresh": { - "allocatedBytes": 12392, - "meanNanoseconds": 8231.022261555989 + "allocatedBytes": 12200, + "meanNanoseconds": 6498.997478485107 }, "EvalPolynomialFresh15Digits": { - "allocatedBytes": 11920, - "meanNanoseconds": 9042.05249633789 + "allocatedBytes": 11528, + "meanNanoseconds": 6546.591541544596 }, "EvalTranscendentalFresh": { - "allocatedBytes": 34640, - "meanNanoseconds": 30352.334001813615 + "allocatedBytes": 34480, + "meanNanoseconds": 24497.13707624163 }, "EvalTrig": { - "allocatedBytes": 69888, - "meanNanoseconds": 59684.38562950721 + "allocatedBytes": 69728, + "meanNanoseconds": 48697.815450032555 }, "EvalTrigPrecise": { - "allocatedBytes": 336498, - "meanNanoseconds": 719311.40625 + "allocatedBytes": 336298, + "meanNanoseconds": 607074.7646484375 }, "ParseEasy": { - "allocatedBytes": 18436, - "meanNanoseconds": 10903.259682288537 + "allocatedBytes": 18354, + "meanNanoseconds": 8140.952856445312 }, "ParseHard": { - "allocatedBytes": 59927, - "meanNanoseconds": 53732.40408325195 + "allocatedBytes": 59449, + "meanNanoseconds": 37792.418892996655 }, "RunEasy": { "allocatedBytes": 0, - "meanNanoseconds": 48.66310466527939 + "meanNanoseconds": 29.99794231057167 }, "RunHard": { "allocatedBytes": 0, - "meanNanoseconds": 502.88996543884275 + "meanNanoseconds": 376.0579309096703 }, "RunMedium": { "allocatedBytes": 0, - "meanNanoseconds": 304.26167697906493 + "meanNanoseconds": 232.18012067476909 }, "SimplifyEasy": { - "allocatedBytes": 79785, - "meanNanoseconds": 90259.30957500752 + "allocatedBytes": 29097, + "meanNanoseconds": 43978.96368844168 }, "SimplifyHard": { - "allocatedBytes": 161716160, - "meanNanoseconds": 170258847.5172414 + "allocatedBytes": 142790328, + "meanNanoseconds": 114961392.04545455 }, "SolveEasy": { - "allocatedBytes": 443343, - "meanNanoseconds": 456071.09214564733 + "allocatedBytes": 428119, + "meanNanoseconds": 316744.0382161458 }, "SolveEasyMedium": { - "allocatedBytes": 66009, - "meanNanoseconds": 37714.56470133464 + "allocatedBytes": 63937, + "meanNanoseconds": 27338.372973632813 }, "SolveHard": { - "allocatedBytes": 8514589, - "meanNanoseconds": 110748807.29333334 + "allocatedBytes": 8167912, + "meanNanoseconds": 94044513.44117647 }, "SolveMedium": { - "allocatedBytes": 457352, - "meanNanoseconds": 503471.8545735677 + "allocatedBytes": 446063, + "meanNanoseconds": 428874.7633463542 }, "SolveMediumHard": { - "allocatedBytes": 1346172, - "meanNanoseconds": 14091807.230208334 + "allocatedBytes": 1261793, + "meanNanoseconds": 10994685.073958334 } }, "ungated": { diff --git a/Sources/Tests/UnitTests/Core/NodesWalkTest.cs b/Sources/Tests/UnitTests/Core/NodesWalkTest.cs new file mode 100644 index 000000000..b97dd1fd9 --- /dev/null +++ b/Sources/Tests/UnitTests/Core/NodesWalkTest.cs @@ -0,0 +1,124 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Xunit; + +namespace AngouriMath.Tests.Core +{ + /// + /// and the internal walks over it visit every node in pre-order -- + /// the node, then each child's nodes in order -- whether the walk is repeated, abandoned, + /// nested inside another or run on several threads at once, and whatever the depth. + /// https://github.com/asc-community/AngouriMath/issues/534 + /// + [Trait("Area", "Core")] + public sealed class NodesWalkTest + { + private static List PreOrder(Entity e) + { + var list = new List { e }; + foreach (var child in e.DirectChildren) + list.AddRange(PreOrder(child)); + return list; + } + + public static TheoryData Expressions => new() + { + "x", + "a + b / 2 ^ 3", + "sin(x)^2 + cos(x)^2 + x/x + (x+1)*(x-1)", + "log(sqrt(x), a + b) + integral(sin(y) * x, y) + { 1, 2, x }", + "[[x, 1], [y + 1, sin(z)]] * [[1], [2]]", + "{ x in RR : x > 0 } \\/ [1; 2]", + }; + + [Theory, MemberData(nameof(Expressions))] + public void NodesArePreOrder(string expression) + { + Entity expr = expression; + var expected = PreOrder(expr); + Assert.Equal(expected, expr.Nodes.ToList(), ReferenceEqualityComparer.Instance); + var nodes = expr.Nodes; + Assert.Equal(expected, nodes.ToList(), ReferenceEqualityComparer.Instance); + Assert.Equal(expected, nodes.ToList(), ReferenceEqualityComparer.Instance); + } + + [Theory, MemberData(nameof(Expressions))] + public void InternalWalksAgreeWithNodes(string expression) + { + Entity expr = expression; + foreach (var node in expr.Nodes) + { + Assert.Equal(expr.Nodes.Count(n => n == node), expr.CountNode(node)); + // A variable is answered from VarsAndConsts, which leaves out the placeholder a + // set builder publishes for its bound name, so only the walk is asked here. + if (node is not Entity.Variable) + Assert.True(expr.ContainsNode(node)); + Assert.True(expr.AnyNode(n => ReferenceEquals(n, node))); + Assert.False(expr.AllNodes(n => !ReferenceEquals(n, node))); + } + Assert.False(expr.ContainsNode("w + 17")); + Assert.True(expr.AllNodes(_ => true)); + Assert.False(expr.AnyNode(_ => false)); + } + + [Fact] + public void AnyStopsAtTheFirstMatchInOrder() + { + Entity expr = "(a + b) * (c + d)"; + var asked = new List(); + Assert.True(expr.AnyNode(n => { asked.Add(n); return n == "b"; })); + Assert.Equal(PreOrder(expr).Take(asked.Count), asked); + Assert.Equal(MathS.Var("b"), asked[^1]); + } + + [Fact] + public void AnAbandonedWalkLeavesTheNextOneWhole() + { + Entity expr = "sin(x)^2 + cos(x)^2 + x/x + (x+1)*(x-1)"; + using (var e = expr.Nodes.GetEnumerator()) + for (var i = 0; i < 5; i++) + Assert.True(e.MoveNext()); + var abandoned = expr.Nodes.GetEnumerator(); + abandoned.MoveNext(); + abandoned.MoveNext(); + Assert.Equal(PreOrder(expr), expr.Nodes.ToList(), ReferenceEqualityComparer.Instance); + Assert.True(abandoned.MoveNext()); + } + + [Fact] + public void AWalkInsideAWalk() + { + Entity expr = "sin(x)^2 + cos(x)^2 + x/x + (x+1)*(x-1)"; + var expected = PreOrder(expr).SelectMany(n => PreOrder(n)).ToList(); + var actual = new List(); + foreach (var node in expr.Nodes) + foreach (var inner in node.Nodes) + actual.Add(inner); + Assert.Equal(expected, actual, ReferenceEqualityComparer.Instance); + Assert.True(expr.AllNodes(n => n.AllNodes(m => expr.ContainsNode(m)))); + } + + [Fact] + public void ADeepTreeOnSeveralThreads() + { + Entity sum = "t_0"; + for (var i = 1; i < 2000; i++) + sum += MathS.Var("t_" + i); + var expected = PreOrder(sum); + Parallel.For(0, 8, _ => + { + for (var k = 0; k < 20; k++) + Assert.Equal(expected, sum.Nodes.ToList(), ReferenceEqualityComparer.Instance); + }); + Assert.Equal(1, sum.CountNode(MathS.Var("t_1999"))); + } + } +}