Skip to content

Commit

Permalink
hunspell (minor): reduce allocations when processing compound rules (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
donnerpeter authored May 19, 2023
1 parent 84e2e3a commit a454388
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public class Dictionary {
boolean checkCompoundCase, checkCompoundDup, checkCompoundRep;
boolean checkCompoundTriple, simplifiedTriple;
int compoundMin = 3, compoundMax = Integer.MAX_VALUE;
List<CompoundRule> compoundRules; // nullable
CompoundRule[] compoundRules; // nullable
List<CheckCompoundPattern> checkCompoundPatterns = new ArrayList<>();

// ignored characters (dictionary, affix, inputs)
Expand Down Expand Up @@ -601,11 +601,11 @@ private String[] splitBySpace(LineNumberReader reader, String line, int minParts
return parts;
}

private List<CompoundRule> parseCompoundRules(LineNumberReader reader, int num)
private CompoundRule[] parseCompoundRules(LineNumberReader reader, int num)
throws IOException, ParseException {
List<CompoundRule> compoundRules = new ArrayList<>();
CompoundRule[] compoundRules = new CompoundRule[num];
for (int i = 0; i < num; i++) {
compoundRules.add(new CompoundRule(singleArgument(reader, reader.readLine()), this));
compoundRules[i] = new CompoundRule(singleArgument(reader, reader.readLine()), this);
}
return compoundRules;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ private boolean checkCompoundRules(
if (forms != null) {
words.add(forms);

if (dictionary.compoundRules.stream().anyMatch(r -> r.mayMatch(words))) {
if (mayHaveCompoundRule(words)) {
if (checkLastCompoundPart(wordChars, offset + breakPos, length - breakPos, words)) {
return true;
}
Expand All @@ -467,6 +467,15 @@ private boolean checkCompoundRules(
return false;
}

private boolean mayHaveCompoundRule(List<IntsRef> words) {
for (CompoundRule rule : dictionary.compoundRules) {
if (rule.mayMatch(words)) {
return true;
}
}
return false;
}

private boolean checkLastCompoundPart(
char[] wordChars, int start, int length, List<IntsRef> words) {
IntsRef ref = new IntsRef(new int[1], 0, 1);
Expand All @@ -475,7 +484,12 @@ private boolean checkLastCompoundPart(
Stemmer.RootProcessor stopOnMatching =
(stem, formID, morphDataId, outerPrefix, innerPrefix, outerSuffix, innerSuffix) -> {
ref.ints[0] = formID;
return dictionary.compoundRules.stream().noneMatch(r -> r.fullyMatches(words));
for (CompoundRule r : dictionary.compoundRules) {
if (r.fullyMatches(words)) {
return false;
}
}
return true;
};
boolean found = !stemmer.doStem(wordChars, start, length, COMPOUND_RULE_END, stopOnMatching);
words.remove(words.size() - 1);
Expand Down

0 comments on commit a454388

Please sign in to comment.