Repository navigation
perf(hermitcrab): share trailing analysis template slot batches #546
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/shared-template-slot-batches
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
8bbbcf4
perf(hermitcrab): share trailing affix-template slot batches
johnml1135 3b59dc9
Address review: drop the unreachable limit check, explain the fallbac…
johnml1135 cc75163
Shorten the shared-slot fallback comment to the hygiene budget
johnml1135 5ed55e0
Compare shared-walk outputs with AnalysisMergeKey
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
154 changes: 154 additions & 0 deletions
154
src/SIL.Machine.Morphology.HermitCrab/AnalysisAffixTemplatesRule.cs
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 |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using SIL.Machine.Annotations; | ||
| using SIL.Machine.Rules; | ||
| using SIL.ObjectModel; | ||
|
|
||
| namespace SIL.Machine.Morphology.HermitCrab | ||
| { | ||
| internal class AnalysisAffixTemplatesRule : RuleBatch<Word, ShapeNode> | ||
| { | ||
| private readonly Morpher _morpher; | ||
| private readonly AnalysisAffixTemplateRule[] _templates; | ||
| private readonly bool[][] _slotOptionality; | ||
| private readonly SlotNode _root = new SlotNode(); | ||
| private readonly bool _hasSharedSlots; | ||
|
|
||
| public AnalysisAffixTemplatesRule(Morpher morpher, IEnumerable<IRule<Word, ShapeNode>> rules) | ||
| : base(rules, false, FreezableEqualityComparer<Word>.Default) | ||
| { | ||
| _morpher = morpher; | ||
| _templates = Rules.OfType<AnalysisAffixTemplateRule>().ToArray(); | ||
| if (_templates.Length != Rules.Count) | ||
| return; | ||
|
|
||
| _slotOptionality = _templates.Select(t => t.Template.Slots.Select(s => s.Optional).ToArray()).ToArray(); | ||
| for (int t = 0; t < _templates.Length; t++) | ||
| { | ||
| AnalysisAffixTemplateRule template = _templates[t]; | ||
| SlotNode node = _root; | ||
| for (int s = template.Template.Slots.Count - 1; s >= 0; s--) | ||
| { | ||
| AffixTemplateSlot slot = template.Template.Slots[s]; | ||
| SlotNode child = node.Children.FirstOrDefault(n => n.Matches(slot)); | ||
| if (child == null) | ||
| { | ||
| child = new SlotNode | ||
| { | ||
| Slot = slot, | ||
| Optional = slot.Optional, | ||
| Rule = template.SlotRules[s], | ||
| }; | ||
| node.Children.Add(child); | ||
| } | ||
| else | ||
| { | ||
| _hasSharedSlots = true; | ||
| } | ||
| child.Templates.Add(t); | ||
| node = child; | ||
| } | ||
| node.Terminals.Add(t); | ||
| } | ||
| } | ||
|
|
||
| public override IEnumerable<Word> Apply(Word input) | ||
| { | ||
| // Use the original walk when no tail is shared, when tracing (per-template Begin/End events), for | ||
| // parallel slots, under an alternative limit, or if slot optionality changed after the trie was built. | ||
| if ( | ||
| !_hasSharedSlots | ||
| || _morpher.TraceManager.IsTracing | ||
| || _morpher.MaxDegreeOfParallelism != 1 | ||
| || MaxAlternatives > 0 | ||
| || !SlotsUnchanged() | ||
| ) | ||
| return base.Apply(input); | ||
|
|
||
| var outputs = new HashSet<Word>[_templates.Length]; | ||
| for (int t = 0; t < _templates.Length; t++) | ||
| { | ||
| if (_templates[t].CanApply(input)) | ||
| outputs[t] = new HashSet<Word>(Comparer); | ||
| } | ||
|
|
||
| if (outputs.Any(o => o != null)) | ||
| { | ||
| Word inWord = input.Clone(); | ||
| inWord.Freeze(); | ||
| Walk(_root, inWord, outputs); | ||
| } | ||
|
|
||
| // Template order determines which equal word survives and the order seen by analysis folding. | ||
| var output = new HashSet<Word>(Comparer); | ||
| foreach (HashSet<Word> templateOutput in outputs) | ||
| { | ||
| if (templateOutput != null) | ||
| output.UnionWith(templateOutput); | ||
| } | ||
| return output; | ||
| } | ||
|
|
||
| private bool SlotsUnchanged() | ||
| { | ||
| for (int t = 0; t < _templates.Length; t++) | ||
| { | ||
| IList<AffixTemplateSlot> slots = _templates[t].Template.Slots; | ||
| bool[] optionality = _slotOptionality[t]; | ||
| if (slots.Count != optionality.Length) | ||
| return false; | ||
| for (int s = 0; s < slots.Count; s++) | ||
| { | ||
| if (slots[s].Optional != optionality[s]) | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private static void Walk(SlotNode node, Word input, HashSet<Word>[] outputs) | ||
| { | ||
| foreach (int t in node.Terminals) | ||
| outputs[t]?.Add(input); | ||
|
|
||
| foreach (SlotNode child in node.Children) | ||
| { | ||
| if (!AnyAdmitted(child.Templates, outputs)) | ||
| continue; | ||
|
|
||
| foreach (Word word in child.Rule.Apply(input)) | ||
| Walk(child, word, outputs); | ||
| if (child.Optional) | ||
| Walk(child, input, outputs); | ||
| } | ||
| } | ||
|
|
||
| private static bool AnyAdmitted(List<int> templates, HashSet<Word>[] outputs) | ||
| { | ||
| for (int i = 0; i < templates.Count; i++) | ||
| { | ||
| if (outputs[templates[i]] != null) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private sealed class SlotNode | ||
| { | ||
| public AffixTemplateSlot Slot { get; set; } | ||
| public bool Optional { get; set; } | ||
| public IRule<Word, ShapeNode> Rule { get; set; } | ||
| public List<SlotNode> Children { get; } = new List<SlotNode>(); | ||
| public List<int> Templates { get; } = new List<int>(); | ||
| public List<int> Terminals { get; } = new List<int>(); | ||
|
|
||
| public bool Matches(AffixTemplateSlot slot) | ||
| { | ||
| if (Optional != slot.Optional || Slot.Rules.Count != slot.Rules.Count) | ||
| return false; | ||
|
|
||
| return Slot.Rules.Zip(slot.Rules, (a, b) => ReferenceEquals(a, b)).All(equal => equal); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: F4. A default
Morphernever takes the shared walk:maxDegreeOfParallelismdefaults to 0 (Morpher.cs:29), so this guard sends every call tobase.Applyand only callers who pass degree 1 get fewer batches. The PR body defers parallel sharing but presents the speedup as general; say in the description that it needs degree 1, or say why the default should not be covered.