-
Notifications
You must be signed in to change notification settings - Fork 109
Improve speed at which language files are loaded #914
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
tiffit
wants to merge
3
commits into
master
Choose a base branch
from
improve-lang-load
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
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
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
64 changes: 64 additions & 0 deletions
64
src/main/java/com/mitchej123/hodgepodge/mixins/early/minecraft/fastload/MixinLocale.java
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,64 @@ | ||
| package com.mitchej123.hodgepodge.mixins.early.minecraft.fastload; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.nio.charset.Charset; | ||
| import java.util.Collections; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
|
|
||
| import net.minecraft.client.resources.Locale; | ||
|
|
||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.injection.At; | ||
| import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
|
||
| import com.google.common.base.Splitter; | ||
| import com.llamalad7.mixinextras.sugar.Local; | ||
| import com.mitchej123.hodgepodge.util.OffThreadLineIterator; | ||
|
|
||
| @Mixin(Locale.class) | ||
| public class MixinLocale { | ||
|
|
||
| @Redirect( | ||
| method = "loadLocaleData(Ljava/io/InputStream;)V", | ||
| at = @At( | ||
| value = "INVOKE", | ||
| target = "Lorg/apache/commons/io/IOUtils;readLines(Ljava/io/InputStream;Ljava/nio/charset/Charset;)Ljava/util/List;", | ||
| remap = false)) | ||
| private List<String> readNoLines(InputStream input, Charset encoding) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| @Redirect( | ||
| method = "loadLocaleData(Ljava/io/InputStream;)V", | ||
| at = @At(value = "INVOKE", target = "Ljava/util/List;iterator()Ljava/util/Iterator;", remap = false)) | ||
| private Iterator<String> createOffThreadIterator(List<String> instance, @Local(argsOnly = true) InputStream input) { | ||
| return new OffThreadLineIterator(input); | ||
| } | ||
|
|
||
| @Redirect( | ||
| method = "loadLocaleData(Ljava/io/InputStream;)V", | ||
| at = @At( | ||
| value = "INVOKE", | ||
| target = "Lcom/google/common/base/Splitter;split(Ljava/lang/CharSequence;)Ljava/lang/Iterable;", | ||
| remap = false)) | ||
| private Iterable<String> dontSplit(Splitter instance, CharSequence sequence) { | ||
| return null; | ||
| } | ||
|
|
||
| @Redirect( | ||
| method = "loadLocaleData(Ljava/io/InputStream;)V", | ||
| at = @At( | ||
| value = "INVOKE", | ||
| target = "Lcom/google/common/collect/Iterables;toArray(Ljava/lang/Iterable;Ljava/lang/Class;)[Ljava/lang/Object;", | ||
| remap = false)) | ||
| private Object[] split(Iterable<String> iterable, Class<String> type, @Local String s) { | ||
| for (int i = 0; i < s.length() - 1; i++) { | ||
| if (s.charAt(i) == '=') { | ||
| return new String[] { s.substring(0, i), s.substring(i + 1) }; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| } |
65 changes: 65 additions & 0 deletions
65
.../java/com/mitchej123/hodgepodge/mixins/early/minecraft/fastload/MixinStringTranslate.java
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,65 @@ | ||
| package com.mitchej123.hodgepodge.mixins.early.minecraft.fastload; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.nio.charset.Charset; | ||
| import java.util.Collections; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
|
|
||
| import net.minecraft.util.StringTranslate; | ||
|
|
||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.injection.At; | ||
| import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
|
||
| import com.google.common.base.Splitter; | ||
| import com.llamalad7.mixinextras.sugar.Local; | ||
| import com.mitchej123.hodgepodge.util.OffThreadLineIterator; | ||
|
|
||
| @Mixin(value = StringTranslate.class, remap = false) | ||
| public class MixinStringTranslate { | ||
|
|
||
| @Redirect( | ||
| method = "parseLangFile", | ||
| at = @At( | ||
| value = "INVOKE", | ||
| target = "Lorg/apache/commons/io/IOUtils;readLines(Ljava/io/InputStream;Ljava/nio/charset/Charset;)Ljava/util/List;", | ||
| remap = false)) | ||
| private static List<String> readNoLines(InputStream input, Charset encoding) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| @Redirect( | ||
| method = "parseLangFile", | ||
| at = @At(value = "INVOKE", target = "Ljava/util/List;iterator()Ljava/util/Iterator;", remap = false)) | ||
| private static Iterator<String> createOffThreadIterator(List<String> instance, | ||
| @Local(argsOnly = true) InputStream input) { | ||
| return new OffThreadLineIterator(input); | ||
| } | ||
|
|
||
| @Redirect( | ||
| method = "parseLangFile", | ||
| at = @At( | ||
| value = "INVOKE", | ||
| target = "Lcom/google/common/base/Splitter;split(Ljava/lang/CharSequence;)Ljava/lang/Iterable;", | ||
| remap = false)) | ||
| private static Iterable<String> dontSplit(Splitter instance, CharSequence sequence) { | ||
| return null; | ||
| } | ||
|
|
||
| @Redirect( | ||
| method = "parseLangFile", | ||
| at = @At( | ||
| value = "INVOKE", | ||
| target = "Lcom/google/common/collect/Iterables;toArray(Ljava/lang/Iterable;Ljava/lang/Class;)[Ljava/lang/Object;", | ||
| remap = false)) | ||
| private static Object[] split(Iterable<String> iterable, Class<String> type, @Local String s) { | ||
| for (int i = 0; i < s.length() - 1; i++) { | ||
| if (s.charAt(i) == '=') { | ||
|
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. Why not use |
||
| return new String[] { s.substring(0, i), s.substring(i + 1) }; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| } | ||
74 changes: 74 additions & 0 deletions
74
src/main/java/com/mitchej123/hodgepodge/util/OffThreadLineIterator.java
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,74 @@ | ||
| package com.mitchej123.hodgepodge.util; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.InputStreamReader; | ||
| import java.util.Iterator; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.LinkedBlockingQueue; | ||
|
|
||
| import org.apache.commons.io.Charsets; | ||
|
|
||
| import com.mitchej123.hodgepodge.Common; | ||
|
|
||
| /** | ||
| * Iterator which reads lines of an InputStream is a different thread so that the stream can continue to be read while | ||
| * earlier lines are being processed. | ||
| */ | ||
| public class OffThreadLineIterator implements Iterator<String> { | ||
|
|
||
| private static final ExecutorService executor = Executors | ||
| .newSingleThreadExecutor(OffThreadLineIterator::createThread);; | ||
|
|
||
| private final BufferedReader reader; | ||
| private final LinkedBlockingQueue<String> pendingLines = new LinkedBlockingQueue<>(); | ||
| private volatile boolean finished; | ||
| private String next; | ||
|
|
||
| public OffThreadLineIterator(InputStream stream) { | ||
| reader = new BufferedReader(new InputStreamReader(stream, Charsets.toCharset(Charsets.UTF_8))); | ||
| executor.submit(this::run); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| try { | ||
| next = pendingLines.take(); | ||
| } catch (InterruptedException e) { | ||
| return false; | ||
| } | ||
| return !finished || !pendingLines.isEmpty(); | ||
| } | ||
|
|
||
| @Override | ||
| public String next() { | ||
| return next; | ||
| } | ||
|
|
||
| private void run() { | ||
| try { | ||
| String line = reader.readLine(); | ||
| while (line != null) { | ||
| pendingLines.add(line); | ||
| line = reader.readLine(); | ||
| } | ||
| } catch (Exception ex) { | ||
| Common.log.error("Error reading line", ex); | ||
| } | ||
| finished = true; | ||
| pendingLines.add(""); | ||
| try { | ||
| reader.close(); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private static Thread createThread(Runnable r) { | ||
| Thread thread = new Thread(r, "OffThreadLineIterator"); | ||
| thread.setDaemon(true); | ||
| return thread; | ||
| } | ||
| } |
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.
Wait a moment, how does this work? Instead of performing blocking IO operations on the main thread, we create one new off-thread and block the main thread while the off-thread is performing IO operations?
The only possible benefit I see from this is being able to process the lines while the other thread is waiting on IO, but that's a pretty minor benefit assuming this process is IO-bound.