From 81326b06babe26477c64cb11b85758b4304f0699 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 27 Sep 2026 17:51:04 +0000 Subject: [PATCH] Evaled is cached with the precision frame it was computed in The cache was stamped with the evaluation epoch, one counter for the whole process that every change of the precision advanced, while a precision scope belongs to one flow: a flow that opened a scope and evaluated a node another flow shares left the value current for every flow, so a flow at a hundred digits read pi at three hundred. The stamp is now the precision setting's frame for the flow, compared by reference; the value, the node it was computed for and the frame are one object published in one store. The epoch had no other reader and is gone, with its increment on every Set and every scope's end. Until a precision scope has been opened somewhere, no flow has a frame, and the async-local read is skipped. Closes #1505. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- Sources/AngouriMath/Convenience/MathS.cs | 2 +- .../AngouriMath/Convenience/SettingClass.cs | 44 ++++----------- .../Evaluation/Evaluation.Definition.cs | 56 ++++++++++++------- .../Core/HighPrecisionFunctionsTest.cs | 33 +++++++++++ 4 files changed, 81 insertions(+), 54 deletions(-) diff --git a/Sources/AngouriMath/Convenience/MathS.cs b/Sources/AngouriMath/Convenience/MathS.cs index 0aca8ad1b..49fb28a4e 100644 --- a/Sources/AngouriMath/Convenience/MathS.cs +++ b/Sources/AngouriMath/Convenience/MathS.cs @@ -6105,7 +6105,7 @@ internal static EDecimal DowncastingTolerance /// Settings for precisions of PeterO.Numbers /// public static Setting DecimalPrecisionContext { get; } = - new(new EContext(100, ERounding.HalfUp, -100, 1000, false)) { AdvancesEvaluationEpoch = true }; + new(new EContext(100, ERounding.HalfUp, -100, 1000, false)); /// /// Whether functions are being read as real-valued or complex-valued. It is a diff --git a/Sources/AngouriMath/Convenience/SettingClass.cs b/Sources/AngouriMath/Convenience/SettingClass.cs index 6afb457b7..db0a27829 100644 --- a/Sources/AngouriMath/Convenience/SettingClass.cs +++ b/Sources/AngouriMath/Convenience/SettingClass.cs @@ -92,28 +92,6 @@ internal static bool StillHolds(object?[] snapshot) } } - /// - /// A count of the changes to what a numerical evaluation depends on -- the precision - /// context, set or its scope ended, on any thread. An records the - /// epoch its was computed at and recomputes when it has moved, - /// since a hundred digits of pi cached at one precision are the wrong answer at another. - /// One counter for every thread: a change on another thread costs a recomputation and - /// never a stale value. The downcasting setting does not advance it: the integrator's - /// sampled checks turn the downcasting off and on around every candidate, and an epoch - /// moved by those made every cached evaluation in the process stale each time -- the - /// unit suite took four times as long. What the downcasting changed in a cached value - /// that mattered, a constant's type, no longer depends on it. - /// https://github.com/asc-community/AngouriMath/issues/1367 - /// - internal static class EvaluationEpoch - { - [ConcurrentField] private static int current; - - internal static int Current => Volatile.Read(ref current); - - internal static void Advance() => Interlocked.Increment(ref current); - } - /// /// This class for configuring some internal mechanisms from outside /// @@ -161,6 +139,7 @@ private sealed class Frame private readonly AsyncLocal frames = new(); private long lastId; + private bool everScoped; internal Setting(T defaultValue) { @@ -168,12 +147,6 @@ internal Setting(T defaultValue) SettingsState.Register(this); } - /// - /// Whether a change of this setting advances the : the - /// precision context does, since every cached evaluation depends on it. - /// - internal bool AdvancesEvaluationEpoch { get; init; } - /// /// The frame this setting currently reads from, as the identity of its state. Released /// in order — which using guarantees — a scope restores the very frame that was @@ -183,6 +156,14 @@ internal Setting(T defaultValue) /// object? ISettingState.CurrentState => frames.Value; + /// + /// Whether a scope of this setting has been opened, in any flow, since the process + /// started, so that a reader can skip asking for the flow's frame while none can exist. + /// It never goes back: a child flow that captured a scope keeps it after its parent + /// disposes it, so a count of open scopes returning to zero would not mean there are none. + /// + internal bool EverScoped => Volatile.Read(ref everScoped); + /// /// Sets the new value for the setting /// @@ -201,9 +182,10 @@ internal Setting(T defaultValue) public AutoBackRollableTemporarySettingUnit Set(T value) { var id = Interlocked.Increment(ref lastId); + // Before the frame, so that no flow can see the frame without the flag. + if (!everScoped) + Volatile.Write(ref everScoped, true); frames.Value = new Frame(id, value, frames.Value); - if (AdvancesEvaluationEpoch) - EvaluationEpoch.Advance(); return new AutoBackRollableTemporarySettingUnit(this, id); } @@ -220,8 +202,6 @@ private void Remove(long id) var top = frames.Value; if (top is null) return; - if (AdvancesEvaluationEpoch) - EvaluationEpoch.Advance(); if (top.Id == id) { frames.Value = top.Next; diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs index 34c900f88..5dbfde217 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs @@ -184,33 +184,47 @@ public Entity Evaled { get { - // Cached for the epoch it was computed under -- the precision context and - // the downcasting: a hundred digits of pi are the wrong answer at five - // hundred, and an expression held across a change of the settings used to - // answer from the first ones. The epoch is read before the computation and - // recorded with its result, so that a scope opened and closed inside the - // computation -- the integrator's sampled checks turn the downcasting off -- - // leaves a value stamped as stale, to be recomputed on the next read, and - // never a slot cleared under a computation still running; the lazy slot it - // was, reset on a stale epoch, was cleared that way and read back as null. + // Cached with the precision it was computed at, and read back only at that + // precision: a hundred digits of pi are the wrong answer at five hundred. The + // stamp is the precision setting's frame for this flow, compared by reference. + // Frames are immutable, a scope opened and closed in order restores the very + // frame that was under it, and a flow outside every scope has none. It was a + // count of precision changes for the whole process, which could not tell one + // flow's scope from another's, so a flow at a hundred digits read pi at three + // hundred while another flow's scope was open. + // https://github.com/asc-community/AngouriMath/issues/1505 + // The frame is read before the computation, so a scope opened and closed inside + // it leaves the value stamped with the frame it was asked in. // https://github.com/asc-community/AngouriMath/issues/1367 // And stamped with its owner: a record's `with` -- WithCodomain, and the - // derivative's rewrites -- copies every field, this one included, and a - // value computed for the original is not the copy's; the lazy slot carried - // the same stamp. - var epoch = Convenience.EvaluationEpoch.Current; - if (evaled is { } cached && evaledEpoch == epoch && ReferenceEquals(evaledOwner, this)) - return cached; + // derivative's rewrites -- copies every field, this one included, and a value + // computed for the original is not the copy's. + // The value and its stamps are one object, published in one store, so that no + // flow reads one flow's value under another's stamp. + // Until a precision scope has been opened somewhere, no flow has a frame, and a + // cached value is read without asking for one: the flow's frame is an async-local + // read, several times the cost of the rest of this path. + var precisionSetting = MathS.Settings.DecimalPrecisionContext; + var precision = precisionSetting.EverScoped ? ((Convenience.ISettingState)precisionSetting).CurrentState : null; + if (System.Threading.Volatile.Read(ref evaled) is { } cached + && ReferenceEquals(cached.Owner, this) && ReferenceEquals(cached.Precision, precision)) + return cached.Value; var computed = InnerSimplifyWithCheck(false); - evaledEpoch = epoch; - evaledOwner = this; - evaled = computed; + System.Threading.Volatile.Write(ref evaled, new EvaledValue(computed, this, precision)); return computed; } } - private Entity? evaled; - private Entity? evaledOwner; - private int evaledEpoch; + + /// A value of , with the node and the precision it was computed for. + private sealed class EvaledValue + { + internal readonly Entity Value; + internal readonly Entity Owner; + internal readonly object? Precision; + internal EvaledValue(Entity value, Entity owner, object? precision) + => (Value, Owner, Precision) = (value, owner, precision); + } + private EvaledValue? evaled; /// /// This is the result of naive simplifications, but not creating imprecise values unlike . In other diff --git a/Sources/Tests/UnitTests/Core/HighPrecisionFunctionsTest.cs b/Sources/Tests/UnitTests/Core/HighPrecisionFunctionsTest.cs index 76de5dfe4..308c03ff2 100644 --- a/Sources/Tests/UnitTests/Core/HighPrecisionFunctionsTest.cs +++ b/Sources/Tests/UnitTests/Core/HighPrecisionFunctionsTest.cs @@ -262,6 +262,39 @@ public void ConstantsAndCachedEvaluationsFollowThePrecision() Assert.Equal(100, DigitsOf(held.EvalNumerical())); } + /// + /// The same across two flows at once: one flow in a raised precision evaluates a shared + /// node while another flow, at the default, reads it. The cache was stamped with the + /// process's epoch and not with the flow's precision, so the default flow read pi at + /// three hundred digits until the other scope closed. + /// https://github.com/asc-community/AngouriMath/issues/1505 + /// + [Fact] + public void AnotherFlowsPrecisionIsNotServedHere() + { + using var evaluated = new System.Threading.ManualResetEventSlim(); + using var release = new System.Threading.ManualResetEventSlim(); + // A thread of its own, so that a busy pool cannot hold it back past the waits below. + var other = System.Threading.Tasks.Task.Factory.StartNew(() => + { + using var _ = MathS.Settings.DecimalPrecisionContext.Set(threeHundredDigits); + var there = DigitsOf(MathS.pi.EvalNumerical()); + evaluated.Set(); + release.Wait(System.TimeSpan.FromSeconds(30)); + return there; + }, System.Threading.Tasks.TaskCreationOptions.LongRunning); + try + { + Assert.True(evaluated.Wait(System.TimeSpan.FromSeconds(30))); + Assert.Equal(100, DigitsOf(MathS.pi.EvalNumerical())); + } + finally + { + release.Set(); + } + Assert.Equal(300, other.Result); + } + /// /// e evaluated with the downcasting off used to be a complex number with a zero /// imaginary part, and the cache on the constant kept it that way for the rest of the