languagetool-org / languagetool

Style and Grammar Checker for 25+ Languages
https://languagetool.org
GNU Lesser General Public License v2.1
12.46k stars 1.4k forks source link

Memory and performance optimizations #11027

Closed hurricup closed 5 days ago

hurricup commented 1 week ago

We did get a report that when the French language is active, a lot of memory consumed. I did investigate and there are a lot of things that could be optimized. I made some improvements and will split them into several PRs to make it easier to review.

In this PR:

My intent is to do the same for all languages in LT. This improves the loading timing and reduce the memory footprint. Currently, LT has a lot of unnecessary duplicated objects while working.

Here is the synthetic test I am using for the snapshots:

class MeasureMemory {
  public static void main(String[] args) throws Exception {
    List<JLanguageTool> tools = new ArrayList<>();
    Arrays.asList(
      GermanyGerman.getInstance(),
      French.getInstance()
    ).forEach(lang -> {
      System.err.println("LOADING: " + lang);
      try {
        tools.add(loadLanguage(lang));
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    });

    HotSpotDiagnosticMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(),
      "com.sun.management:type=HotSpotDiagnostic",
      HotSpotDiagnosticMXBean.class);
    String file = "/home/hurricup/tmp/lt-heap-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("KK-mm-d.MM")) + ".hprof";
    mxBean.dumpHeap(file, true);

    System.out.println("Done: " + tools + ", " + file);
  }

  @NotNull
  private static JLanguageTool loadLanguage(Language language) throws IOException {
    language.setChunker(new NoopChunker());
    JLanguageTool tool = new JLanguageTool(language);
    tool.check("Hello Hello great great Dolly");
    return tool;
  }
}

Total results of all changes I made with (English, German, French): Before: image

After: image

Summary by CodeRabbit

coderabbitai[bot] commented 1 week ago

Walkthrough

The pull request introduces significant modifications across various classes in the LanguageTool project, primarily focusing on the transition from direct instantiation of language classes to a singleton pattern using getInstance() methods. This change affects classes related to both German and French language processing, ensuring consistent management of language instances. Additionally, several import statements have been reorganized for clarity, and some methods have been simplified to enhance readability and maintainability. Overall, the changes aim to improve the efficiency and consistency of language instance usage throughout the codebase.

Changes

File Path Change Summary
languagetool-core/src/main/java/org/languagetool/Languages.java Updated import statements and modified initialization of staticAndDynamicLanguages. Retained existing methods.
languagetool-dev/src/main/java/org/languagetool/dev/GenderWordFinder.java Changed instantiation of GermanyGerman to use getInstance(). Logic remains unchanged.
languagetool-dev/src/main/java/org/languagetool/dev/GermanOldSpellingFinder.java Updated instantiation of GermanyGerman to use getInstance(). Logic remains unchanged.
languagetool-dev/src/main/java/org/languagetool/dev/MissingGermanCompoundAdjectiveFinder.java Changed instantiation of GermanyGerman to use getInstance(). Logic remains unchanged.
languagetool-dev/src/main/java/org/languagetool/dev/MissingGermanCompoundsFinder.java Updated instantiation of GermanSpellerRule to use getInstance(). Logic remains unchanged.
languagetool-dev/src/main/java/org/languagetool/dev/MissingGermanWords.java Changed instantiation of GermanSpellerRule to use getInstance(). Logic remains unchanged.
languagetool-dev/src/main/java/org/languagetool/dev/archive/GermanTaggerEnhancer.java Updated instantiation of Tagger to use getInstance(). Reorganized import statements.
languagetool-dev/src/main/java/org/languagetool/dev/archive/SimpleRuleCounter.java Removed unused import and changed instantiation of GermanyGerman to use getInstance().
languagetool-dev/src/main/java/org/languagetool/dev/bigdata/GermanReflexiveVerbGuesser.java Updated initialization of synthesizer to use getInstance(). Logic remains unchanged.
languagetool-dev/src/test/java/org/languagetool/dev/bigdata/CommonCrawlToNgramTest.java Changed instantiation of CommonCrawlToNgram to use getInstance(). Reordered import statements.
languagetool-language-modules/de-DE-x-simple-language/src/main/java/org/languagetool/language/SimpleGerman.java Updated instantiation of GermanRuleDisambiguator to use getInstance(). Reorganized imports.
languagetool-language-modules/de/src/main/java/org/languagetool/language/German.java Added new imports, updated getDefaultLanguageVariant() to use getInstance(), and introduced a new constant.
languagetool-language-modules/de/src/main/java/org/languagetool/language/GermanyGerman.java Added GERMANY_GERMAN_SHORT_CODE, deprecated constructor, and introduced getInstance().
languagetool-language-modules/de/src/main/java/org/languagetool/rules/de/GermanCompoundRule.java Updated access to GermanyGerman instance to use getInstance().
languagetool-language-modules/de/src/main/java/org/languagetool/rules/de/GermanNumberInWordFilter.java Changed instance access to getInstance(). Logic remains unchanged.
languagetool-language-modules/de/src/main/java/org/languagetool/rules/de/GermanRepeatedWordsRule.java Updated constructor to use getInstance(). Simplified isException method.
languagetool-language-modules/de/src/main/java/org/languagetool/rules/de/MissingCommaRelativeClauseRule.java Updated access to GermanyGerman instance to use getInstance().
languagetool-language-modules/de/src/main/java/org/languagetool/rules/de/PotentialCompoundFilter.java Changed instance access to getInstance(). Logic remains unchanged.
languagetool-language-modules/de/src/main/java/org/languagetool/rules/de/ProhibitedCompoundRule.java Updated instance access to getInstance(). Logic remains unchanged.
languagetool-language-modules/de/src/main/java/org/languagetool/rules/de/RemoveUnknownCompoundsFilter.java Changed access to GermanyGerman instance to use getInstance(). Logic remains unchanged.
languagetool-language-modules/de/src/main/java/org/languagetool/rules/de/ValidWordFilter.java Updated access to GermanSpellerRule instance to use getInstance(). Logic remains unchanged.
languagetool-language-modules/de/src/test/java/org/languagetool/GermanyGermanConcurrencyTest.java Updated createLanguage method to use getInstance().
languagetool-language-modules/de/src/test/java/org/languagetool/JLanguageToolTest.java Changed instantiation of GermanyGerman to use getInstance() in multiple test methods.
languagetool-language-modules/de/src/test/java/org/languagetool/rules/de/AgreementRule2Test.java Updated instantiation of JLanguageTool and AgreementRule2 to use getInstance().
languagetool-language-modules/fr/src/main/java/org/languagetool/language/BelgianFrench.java Added instantiationTrace and updated constructor to check for existing instances.
languagetool-language-modules/fr/src/main/java/org/languagetool/language/CanadianFrench.java Added instantiationTrace and updated constructor for existing instance check.
languagetool-language-modules/fr/src/main/java/org/languagetool/language/French.java Added FRENCH_SHORT_CODE, updated constructors, and modified methods to use Collections.singletonList().
languagetool-language-modules/fr/src/main/java/org/languagetool/language/SwissFrench.java Added instantiationTrace and updated constructor for existing instance check.
languagetool-language-modules/fr/src/main/java/org/languagetool/rules/fr/FindSuggestionsFilter.java Updated instantiation of MorfologikFrenchSpellerRule to use French.getInstance().
languagetool-language-modules/fr/src/main/java/org/languagetool/rules/fr/FrenchNumberInWordFilter.java Updated instantiation of French to use getInstance().
languagetool-language-modules/fr/src/main/java/org/languagetool/rules/fr/FrenchPartialPosTagFilter.java Changed instantiation of disambiguator to use getInstance().
languagetool-language-modules/fr/src/main/java/org/languagetool/rules/fr/FrenchRepeatedWordsRule.java Updated constructor to use getInstance() and simplified isException method.
languagetool-language-modules/fr/src/main/java/org/languagetool/rules/fr/InterrogativeVerbFilter.java Updated morfologikRule to be final and changed instantiation to getInstance().
languagetool-language-modules/fr/src/main/java/org/languagetool/tagging/disambiguation/fr/FrenchHybridDisambiguator.java Updated instantiation of disambiguator to use getInstance().
languagetool-language-modules/fr/src/test/java/org/languagetool/FrenchConcurrencyTest.java Updated createLanguage method to use getInstance().
languagetool-language-modules/fr/src/test/java/org/languagetool/JLanguageToolTest.java Changed instantiation of French to use getInstance() in multiple test methods.
languagetool-language-modules/fr/src/test/java/org/languagetool/rules/fr/FrenchTest.java Updated instantiation of French to use getInstance() in multiple test methods.
languagetool-language-modules/fr/src/test/java/org/languagetool/rules/fr/GenericUnpairedBracketsRuleTest.java Updated instantiation of JLanguageTool to use getInstance().
languagetool-language-modules/fr/src/test/java/org/languagetool/rules/fr/MorfologikFrenchSpellerRuleTest.java Updated JLanguageTool and MorfologikFrenchSpellerRule instantiation to use getInstance().
languagetool-language-modules/fr/src/test/java/org/languagetool/rules/fr/QuestionWhitespaceRuleTest.java Updated instantiation of French to use getInstance().
languagetool-language-modules/fr/src/test/java/org/languagetool/rules/fr/QuestionWhitespaceStrictRuleTest.java Updated instantiation of French to use getInstance().
languagetool-language-modules/fr/src/test/java/org/languagetool/rules/fr/SimpleReplaceRuleTest.java Updated JLanguageTool instantiation to use getInstance().
languagetool-language-modules/fr/src/test/java/org/languagetool/rules/spelling/hunspell/TestFrenchDisambiguator.java Updated instantiation of French to use getInstance().
languagetool-language-modules/fr/src/test/java/org/languagetool/tagging/disambiguation/rules/fr/FrenchRuleDisambiguatorTest.java Updated instantiation of French to use getInstance().
languagetool-language-modules/fr/src/test/java/org/languagetool/tagging/fr/FrenchTaggerTest.java Updated instantiation of French to use getInstance().
languagetool-language-modules/fr/src/test/java/org/languagetool/tokenizers/fr/FrenchSentenceTokenizerTest.java Updated instantiation of SRXSentenceTokenizer to use getInstance().
languagetool-server/src/test/java/org/languagetool/server/HTTPSServerTest.java Updated instantiation of GermanyGerman to use getInstance(). Modified assertions in runTests.
languagetool-server/src/test/java/org/languagetool/server/HTTPServerTest.java Changed instantiation of GermanyGerman to use getInstance(). Modified assertions in various methods.
languagetool-standalone/src/test/java/org/languagetool/JLanguageToolTest.java Updated instantiation of GermanyGerman and French to use getInstance().
languagetool-standalone/src/test/java/org/languagetool/LanguageTest.java Updated equality checks to use getInstance() for GermanyGerman.
languagetool-standalone/src/test/java/org/languagetool/dev/MultiThreadingTest2.java Updated LANG initialization to use getInstance().
languagetool-standalone/src/test/java/org/languagetool/gui/ConfigurationTest.java Updated instantiation of French to use getInstance().
languagetool-standalone/src/test/java/org/languagetool/rules/patterns/StartupTimePerformanceTest.java Updated instantiation of GermanyGerman to use getInstance().
languagetool-wikipedia/src/test/java/org/languagetool/dev/index/IndexerSearcherTest.java Updated instantiation of French and GermanyGerman to use getInstance().
languagetool-wikipedia/src/test/java/org/languagetool/dev/wikipedia/SuggestionReplacerTest.java Updated instantiation of GermanyGerman to use getInstance(). Added suppression annotation for deprecated usage.
languagetool-wikipedia/src/test/java/org/languagetool/dev/wikipedia/WikipediaQuickCheckTest.java Updated instantiation of GermanyGerman to use getInstance().

Suggested labels

French

Suggested reviewers


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
🪧 Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit , please review it.` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (Invoked using PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. ### Other keywords and placeholders - Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. - Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description. - Add `@coderabbitai` anywhere in the PR title to generate the title automatically. ### CodeRabbit Configuration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit. - Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.