Skip to content
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

hunspell (minor): reduce allocations when reading the dictionary's morphological data #12323

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ private int mergeDictionaries(
// if we haven't seen any custom morphological data, try to parse one
if (!hasCustomMorphData) {
int morphStart = line.indexOf(MORPH_SEPARATOR);
if (morphStart >= 0 && morphStart < line.length()) {
if (morphStart >= 0) {
String data = line.substring(morphStart + 1);
hasCustomMorphData =
splitMorphData(data).stream().anyMatch(s -> !s.startsWith("ph:"));
Expand Down Expand Up @@ -1321,14 +1321,22 @@ private List<String> splitMorphData(String morphData) {
if (morphData.isBlank()) {
return Collections.emptyList();
}
return Arrays.stream(morphData.split("\\s+"))
.filter(
s ->
s.length() > 3
&& Character.isLetter(s.charAt(0))
&& Character.isLetter(s.charAt(1))
&& s.charAt(2) == ':')
.collect(Collectors.toList());

List<String> result = null;
int start = 0;
for (int i = 0; i <= morphData.length(); i++) {
if (i == morphData.length() || Character.isWhitespace(morphData.charAt(i))) {
if (i - start > 3
&& Character.isLetter(morphData.charAt(start))
&& Character.isLetter(morphData.charAt(start + 1))
&& morphData.charAt(start + 2) == ':') {
if (result == null) result = new ArrayList<>();
result.add(morphData.substring(start, i));
}
start = i + 1;
}
}
return result == null ? List.of() : result;
}

boolean hasFlag(IntsRef forms, char flag) {
Expand Down