Repository navigation
Prune affix template partials with a required-slot look-ahead #575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
johnml1135
wants to merge
4
commits into
master
Choose a base branch
from
perf/hc-required-slot-lookahead
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0bf9e64
Prune affix template partials whose required slot cannot find its mat…
johnml1135 28cbee1
Check required slot material in order and at the word edges before wa…
johnml1135 faa2499
The required-slot look-ahead keeps testing behind a slot whose unmatc…
johnml1135 91ee3da
Make required-slot pruning switchable; treat untyped and boundary-onl…
johnml1135 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ internal class AnalysisAffixTemplateRule : IRule<Word, ShapeNode> | |
| private readonly Morpher _morpher; | ||
| private readonly AffixTemplate _template; | ||
| private readonly List<IRule<Word, ShapeNode>> _rules; | ||
| private readonly RequiredSlotMaterial _lookahead; | ||
|
|
||
| public AnalysisAffixTemplateRule(Morpher morpher, AffixTemplate template) | ||
| { | ||
|
|
@@ -26,6 +27,7 @@ public AnalysisAffixTemplateRule(Morpher morpher, AffixTemplate template) | |
| FreezableEqualityComparer<Word>.Default | ||
| )) | ||
| ); | ||
| _lookahead = new RequiredSlotMaterial(template); | ||
| } | ||
|
|
||
| public IEnumerable<Word> Apply(Word input) | ||
|
|
@@ -61,24 +63,98 @@ public IEnumerable<Word> Apply(Word input) | |
| return Enumerable.Empty<Word>(); | ||
| } | ||
|
|
||
| int[] fired = null; | ||
| if (RequiredSlotCannotUnapply(input, _rules.Count - 1, ref fired)) | ||
| { | ||
| if (_morpher.TraceManager.IsTracing) | ||
| _morpher.TraceManager.EndUnapplyTemplate(_template, input, false, FailureReason.None); | ||
| return Enumerable.Empty<Word>(); | ||
| } | ||
|
|
||
| Word inWord = input.Clone(); | ||
| inWord.Freeze(); | ||
|
|
||
| var output = new HashSet<Word>(FreezableEqualityComparer<Word>.Default); | ||
| if (_morpher.MaxDegreeOfParallelism == 1) | ||
| ApplySlots(inWord, _rules.Count - 1, output); | ||
| ApplySlots(inWord, _rules.Count - 1, output, fired); | ||
| else | ||
| ParallelApplySlots(inWord, output); | ||
| ParallelApplySlots(inWord, output, fired); | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| private void ApplySlots(Word inWord, int index, HashSet<Word> output) | ||
| // Testing runs from the next slot to unapply and stops after an impure slot, which makes the test | ||
| // for any slot behind it unsound. Shadow mode prunes nothing and carries the fired slots in fired. | ||
| private bool RequiredSlotCannotUnapply(Word word, int lastSlot, ref int[] fired) | ||
| { | ||
| RequiredSlotLookaheadMode mode = _morpher.RequiredSlotLookahead; | ||
| if (mode == RequiredSlotLookaheadMode.Off || _morpher.TraceManager.IsTracing) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: F8. Dead trace branches: this method now returns false whenever |
||
| return false; | ||
|
|
||
| long unifications = 0; | ||
| bool cannot = false; | ||
| SlotWalk walk = SlotWalk.Start(); | ||
| for (int j = lastSlot; j >= 0; j--) | ||
| { | ||
| if ( | ||
| !_template.Slots[j].Optional | ||
| && !IsFired(fired, j) | ||
| && _lookahead.CannotUnapply(j, word.Shape, ref walk, ref unifications) | ||
| ) | ||
| { | ||
| if (mode == RequiredSlotLookaheadMode.On) | ||
| { | ||
| cannot = true; | ||
| break; | ||
| } | ||
| fired = fired == null ? new[] { j } : fired.Append(j).ToArray(); | ||
| _morpher.RequiredSlotShadow?.Invoke("fired", _template, j); | ||
| } | ||
| _lookahead.Advance(j, ref walk); | ||
| if (!_lookahead.IsPure(j)) | ||
| break; | ||
| } | ||
| if (unifications != 0) | ||
| _morpher.RequiredSlotCheckObserver?.Invoke(unifications); | ||
| return cannot; | ||
| } | ||
|
|
||
| private static bool IsFired(int[] fired, int slot) | ||
| { | ||
| return fired != null && Array.IndexOf(fired, slot) >= 0; | ||
| } | ||
|
|
||
| private void ReportShadow(string kind, int[] fired, int slot) | ||
| { | ||
| if (fired == null || _morpher.RequiredSlotShadow == null) | ||
| return; | ||
| if (slot < 0) | ||
| { | ||
| foreach (int j in fired) | ||
| _morpher.RequiredSlotShadow(kind, _template, j); | ||
| } | ||
| else if (IsFired(fired, slot)) | ||
| { | ||
| _morpher.RequiredSlotShadow(kind, _template, slot); | ||
| } | ||
| } | ||
|
|
||
| private void ApplySlots(Word inWord, int index, HashSet<Word> output, int[] fired) | ||
| { | ||
| for (int i = index; i >= 0; i--) | ||
| { | ||
| foreach (Word outWord in _rules[i].Apply(inWord)) | ||
| ApplySlots(outWord, i - 1, output); | ||
| { | ||
| ReportShadow("violated", fired, i); | ||
| int[] outFired = fired; | ||
| if (RequiredSlotCannotUnapply(outWord, i - 1, ref outFired)) | ||
| { | ||
| if (_morpher.TraceManager.IsTracing) | ||
| _morpher.TraceManager.EndUnapplyTemplate(_template, outWord, false, FailureReason.None); | ||
| continue; | ||
| } | ||
| ApplySlots(outWord, i - 1, output, outFired); | ||
| } | ||
|
|
||
| if (!_template.Slots[i].Optional) | ||
| { | ||
|
|
@@ -90,16 +166,17 @@ private void ApplySlots(Word inWord, int index, HashSet<Word> output) | |
|
|
||
| if (_morpher.TraceManager.IsTracing) | ||
| _morpher.TraceManager.EndUnapplyTemplate(_template, inWord, true, FailureReason.None); | ||
| ReportShadow("completed-under-fired", fired, -1); | ||
| output.Add(inWord); | ||
| } | ||
|
|
||
| private void ParallelApplySlots(Word inWord, HashSet<Word> output) | ||
| private void ParallelApplySlots(Word inWord, HashSet<Word> output, int[] fired) | ||
| { | ||
| ParallelOptions parallelOptions = _morpher.CreateParallelOptions(); | ||
| var outStack = new ConcurrentStack<Word>(); | ||
| var from = new ConcurrentStack<Tuple<Word, int>>(); | ||
| from.Push(Tuple.Create(inWord, _rules.Count - 1)); | ||
| var to = new ConcurrentStack<Tuple<Word, int>>(); | ||
| var from = new ConcurrentStack<Tuple<Word, int, int[]>>(); | ||
| from.Push(Tuple.Create(inWord, _rules.Count - 1, fired)); | ||
| var to = new ConcurrentStack<Tuple<Word, int, int[]>>(); | ||
| while (!from.IsEmpty) | ||
| { | ||
| to.Clear(); | ||
|
|
@@ -111,9 +188,25 @@ private void ParallelApplySlots(Word inWord, HashSet<Word> output) | |
| bool add = true; | ||
| for (int i = work.Item2; i >= 0; i--) | ||
| { | ||
| Tuple<Word, int>[] workItems = _rules[i] | ||
| int next = i - 1; | ||
| Tuple<Word, int, int[]>[] workItems = _rules[i] | ||
| .Apply(work.Item1) | ||
| .Select(res => Tuple.Create(res, i - 1)) | ||
| .Select(res => | ||
| { | ||
| ReportShadow("violated", work.Item3, i); | ||
| int[] resFired = work.Item3; | ||
| if (!RequiredSlotCannotUnapply(res, next, ref resFired)) | ||
| return Tuple.Create(res, next, resFired); | ||
| if (_morpher.TraceManager.IsTracing) | ||
| _morpher.TraceManager.EndUnapplyTemplate( | ||
| _template, | ||
| res, | ||
| false, | ||
| FailureReason.None | ||
| ); | ||
| return null; | ||
| }) | ||
| .Where(item => item != null) | ||
| .ToArray(); | ||
| if (workItems.Length > 0) | ||
| to.PushRange(workItems); | ||
|
|
@@ -141,11 +234,12 @@ private void ParallelApplySlots(Word inWord, HashSet<Word> output) | |
| true, | ||
| FailureReason.None | ||
| ); | ||
| ReportShadow("completed-under-fired", work.Item3, -1); | ||
| outStack.Push(work.Item1); | ||
| } | ||
| } | ||
| ); | ||
| ConcurrentStack<Tuple<Word, int>> temp = from; | ||
| ConcurrentStack<Tuple<Word, int, int[]>> temp = from; | ||
| from = to; | ||
| to = temp; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.