Further optimization for multiple threads

This commit is contained in:
konsoletyper 2014-01-27 17:35:42 +04:00
parent 915add4d97
commit 619c50729b
3 changed files with 119 additions and 8 deletions

View File

@ -0,0 +1,93 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.common;
import java.io.IOException;
import java.io.Writer;
/**
*
* @author Alexey Andreev
*/
public class CommutatedWriter extends Writer {
private ThreadLocal<StringBuilder> buffer = new ThreadLocal<>();
private Writer innerWriter;
public CommutatedWriter(Writer innerWriter) {
this.innerWriter = innerWriter;
}
private StringBuilder getBuffer() {
if (buffer.get() == null) {
buffer.set(new StringBuilder());
}
return buffer.get();
}
@Override
public void write(int c) throws IOException {
getBuffer().append(c);
}
@Override
public void write(char[] cbuf) throws IOException {
getBuffer().append(cbuf);
}
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
getBuffer().append(cbuf, off, len);
}
@Override
public void write(String str) throws IOException {
getBuffer().append(str);
}
@Override
public void write(String str, int off, int len) throws IOException {
getBuffer().append(str, off, len + off);
}
@Override
public Writer append(CharSequence csq) throws IOException {
getBuffer().append(csq);
return this;
}
@Override
public Writer append(CharSequence csq, int start, int end) throws IOException {
getBuffer().append(csq, start, end);
return this;
}
@Override
public Writer append(char c) throws IOException {
getBuffer().append(c);
return this;
}
@Override
public void flush() throws IOException {
}
@Override
public void close() throws IOException {
StringBuilder sb = getBuffer();
innerWriter.write(sb.toString());
buffer.set(null);
}
}

View File

@ -104,9 +104,11 @@ public class JavascriptBuilder {
executor.complete(); executor.complete();
ListableClassHolderSource classSet = dependencyChecker.cutUnachievableClasses(); ListableClassHolderSource classSet = dependencyChecker.cutUnachievableClasses();
Decompiler decompiler = new Decompiler(classSet, classLoader); Decompiler decompiler = new Decompiler(classSet, classLoader);
ClassSetOptimizer optimizer = new ClassSetOptimizer(); ClassSetOptimizer optimizer = new ClassSetOptimizer(executor);
optimizer.optimizeAll(classSet); optimizer.optimizeAll(classSet);
executor.complete();
allocateRegisters(classSet); allocateRegisters(classSet);
executor.complete();
if (bytecodeLogging) { if (bytecodeLogging) {
try { try {
logBytecode(new PrintWriter(new OutputStreamWriter(logStream, "UTF-8")), classSet); logBytecode(new PrintWriter(new OutputStreamWriter(logStream, "UTF-8")), classSet);
@ -134,12 +136,16 @@ public class JavascriptBuilder {
} }
private void allocateRegisters(ListableClassHolderSource classes) { private void allocateRegisters(ListableClassHolderSource classes) {
RegisterAllocator allocator = new RegisterAllocator();
for (String className : classes.getClassNames()) { for (String className : classes.getClassNames()) {
ClassHolder cls = classes.getClassHolder(className); ClassHolder cls = classes.getClassHolder(className);
for (MethodHolder method : cls.getMethods()) { for (final MethodHolder method : cls.getMethods()) {
if (method.getProgram() != null && method.getProgram().basicBlockCount() > 0) { if (method.getProgram() != null && method.getProgram().basicBlockCount() > 0) {
allocator.allocateRegisters(method); executor.execute(new Runnable() {
@Override public void run() {
RegisterAllocator allocator = new RegisterAllocator();
allocator.allocateRegisters(method);
}
});
} }
} }
} }

View File

@ -17,6 +17,7 @@ package org.teavm.optimization;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor;
import org.teavm.model.ClassHolder; import org.teavm.model.ClassHolder;
import org.teavm.model.ListableClassHolderSource; import org.teavm.model.ListableClassHolderSource;
import org.teavm.model.MethodHolder; import org.teavm.model.MethodHolder;
@ -28,15 +29,26 @@ import org.teavm.model.MethodHolder;
public class ClassSetOptimizer { public class ClassSetOptimizer {
private List<MethodOptimization> optimizations = Arrays.<MethodOptimization>asList( private List<MethodOptimization> optimizations = Arrays.<MethodOptimization>asList(
new CommonSubexpressionElimination(), new UnusedVariableElimination()); new CommonSubexpressionElimination(), new UnusedVariableElimination());
private Executor executor;
public ClassSetOptimizer(Executor executor) {
super();
this.executor = executor;
}
public void optimizeAll(ListableClassHolderSource classSource) { public void optimizeAll(ListableClassHolderSource classSource) {
for (String className : classSource.getClassNames()) { for (String className : classSource.getClassNames()) {
ClassHolder cls = classSource.getClassHolder(className); ClassHolder cls = classSource.getClassHolder(className);
for (MethodHolder method : cls.getMethods()) { for (final MethodHolder method : cls.getMethods()) {
if (method.getProgram() != null && method.getProgram().basicBlockCount() > 0) { if (method.getProgram() != null && method.getProgram().basicBlockCount() > 0) {
for (MethodOptimization optimization : optimizations) { executor.execute(new Runnable() {
optimization.optimize(method); @Override
} public void run() {
for (MethodOptimization optimization : optimizations) {
optimization.optimize(method);
}
}
});
} }
} }
} }