|
1 | | -// |
| 1 | +// |
2 | 2 | // Copyright (c) 2019-2022 Angouri. |
3 | 3 | // AngouriMath is licensed under MIT. |
4 | 4 | // Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. |
@@ -48,6 +48,7 @@ internal void InnerCompile(Compiler compiler) |
48 | 48 | cacheLine = ~cacheLine; |
49 | 49 | compiler.Cache[this] = cacheLine; |
50 | 50 | compiler.Instructions.Add(new(InstructionType.SAVE_CACHE, cacheLine)); |
| 51 | + compiler.Saved.Add(this); |
51 | 52 | } |
52 | 53 | } |
53 | 54 | } |
@@ -536,29 +537,48 @@ public partial record Providedf |
536 | 537 | { |
537 | 538 | private protected override bool CompilesItself => true; |
538 | 539 |
|
| 540 | + // The value where the predicate holds, and NaN elsewhere, the value not being computed there. |
539 | 541 | private protected override void CompileNode(Compiler compiler) |
540 | 542 | { |
| 543 | + var start = compiler.Saved.Count; |
541 | 544 | Predicate.InnerCompile(compiler); |
| 545 | + var unless = compiler.Jump(InstructionType.JUMP_UNLESS_HOLDS); |
542 | 546 | Expression.InnerCompile(compiler); |
543 | | - compiler.Instructions.Add(new(InstructionType.CALL_PROVIDED)); |
| 547 | + var past = compiler.Jump(InstructionType.JUMP); |
| 548 | + compiler.Land(unless); |
| 549 | + compiler.Instructions.Add(new(InstructionType.PUSH_CONST, Value: double.NaN)); |
| 550 | + compiler.Land(past); |
| 551 | + compiler.Forget(start); |
544 | 552 | } |
545 | 553 | } |
546 | 554 |
|
547 | | - // The first case whose predicate holds, and NaN where none does: from the last case up, |
548 | | - // each is kept where its predicate holds and the cases below it are kept elsewhere. |
| 555 | + // The first case whose predicate holds, and NaN where none does. Each predicate is tried in |
| 556 | + // turn, and the first that holds has its case computed and jumps past the rest, so that a case |
| 557 | + // not taken is not computed: one may be undefined, or costly, where it is not taken. |
549 | 558 | public partial record Piecewise |
550 | 559 | { |
551 | 560 | private protected override bool CompilesItself => true; |
552 | 561 |
|
553 | 562 | private protected override void CompileNode(Compiler compiler) |
554 | 563 | { |
555 | | - compiler.Instructions.Add(new(InstructionType.PUSH_CONST, Value: double.NaN)); |
556 | | - foreach (var @case in Cases.Reverse()) |
| 564 | + var start = compiler.Saved.Count; |
| 565 | + var past = new List<int>(); |
| 566 | + foreach (var @case in Cases) |
557 | 567 | { |
558 | 568 | @case.Predicate.InnerCompile(compiler); |
| 569 | + var unless = compiler.Jump(InstructionType.JUMP_UNLESS_HOLDS); |
| 570 | + // What the predicates computed holds for the cases after them, as reaching one |
| 571 | + // means every predicate before it was computed; what a case computed does not. |
| 572 | + var predicates = compiler.Saved.Count; |
559 | 573 | @case.Expression.InnerCompile(compiler); |
560 | | - compiler.Instructions.Add(new(InstructionType.CALL_SELECT)); |
| 574 | + compiler.Forget(predicates); |
| 575 | + past.Add(compiler.Jump(InstructionType.JUMP)); |
| 576 | + compiler.Land(unless); |
561 | 577 | } |
| 578 | + compiler.Instructions.Add(new(InstructionType.PUSH_CONST, Value: double.NaN)); |
| 579 | + foreach (var jump in past) |
| 580 | + compiler.Land(jump); |
| 581 | + compiler.Forget(start); |
562 | 582 | } |
563 | 583 | } |
564 | 584 |
|
@@ -1010,6 +1030,33 @@ public partial class FastExpression |
1010 | 1030 | internal sealed record Compiler(List<Instruction> Instructions, |
1011 | 1031 | IReadOnlyDictionary<Variable, int> VarNamespace, IDictionary<Entity, int> Cache) |
1012 | 1032 | { |
| 1033 | + /// <summary> |
| 1034 | + /// The subexpressions saved to the cache, in the order their instructions were emitted. A |
| 1035 | + /// subexpression saved where a jump may pass over it has no value in its slot after the jump, |
| 1036 | + /// so once the code that may be passed over is compiled it is <see cref="Forget"/>-ed back to |
| 1037 | + /// unsaved, and is computed again where it is next met: a slot is read only where every path |
| 1038 | + /// to the read has written it, as <see cref="FastExpression.Substitute"/> relies on. |
| 1039 | + /// </summary> |
| 1040 | + internal List<Entity> Saved { get; } = new(); |
| 1041 | + |
| 1042 | + /// <summary>Marks every subexpression saved since <paramref name="count"/> were as unsaved.</summary> |
| 1043 | + internal void Forget(int count) |
| 1044 | + { |
| 1045 | + for (var i = count; i < Saved.Count; i++) |
| 1046 | + Cache[Saved[i]] = ~Cache[Saved[i]]; |
| 1047 | + Saved.RemoveRange(count, Saved.Count - count); |
| 1048 | + } |
| 1049 | + |
| 1050 | + /// <summary>Emits a jump whose target is not yet known, and returns where it is.</summary> |
| 1051 | + internal int Jump(InstructionType type) |
| 1052 | + { |
| 1053 | + Instructions.Add(new(type)); |
| 1054 | + return Instructions.Count - 1; |
| 1055 | + } |
| 1056 | + |
| 1057 | + /// <summary>Makes the jump at <paramref name="jump"/> land on the next instruction emitted.</summary> |
| 1058 | + internal void Land(int jump) => Instructions[jump] = Instructions[jump] with { Reference = Instructions.Count }; |
| 1059 | + |
1013 | 1060 | /// <summary>Returns a compiled expression. Allows to boost substitution a lot</summary> |
1014 | 1061 | /// <param name="func">The function to be compiled</param> |
1015 | 1062 | /// <param name="variables">Must be equal to func's variables (ignoring constants)</param> |
|
0 commit comments