Get rid of Mapper interface in favour of Function

This commit is contained in:
Alexey Andreev 2019-03-11 13:27:34 +03:00
parent f08faba3d2
commit 94322e5f6e
10 changed files with 85 additions and 105 deletions

View File

@ -24,7 +24,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.teavm.common.Mapper;
import java.util.function.Function;
import org.teavm.model.ClassHolder;
import org.teavm.model.ClassReader;
import org.teavm.model.ClassReaderSource;
@ -33,7 +33,7 @@ import org.teavm.model.ReferenceCache;
public class MemoryCachedClassReaderSource implements ClassReaderSource, CacheStatus {
private Map<String, Entry> cache = new HashMap<>();
private Mapper<String, ClassHolder> mapper;
private Function<String, ClassHolder> provider;
private ClassIO classIO;
private final Set<String> freshClasses = new HashSet<>();
@ -42,8 +42,8 @@ public class MemoryCachedClassReaderSource implements ClassReaderSource, CacheSt
classIO = new ClassIO(referenceCache, symbolTable, fileTable, varTable);
}
public void setMapper(Mapper<String, ClassHolder> mapper) {
this.mapper = mapper;
public void setProvider(Function<String, ClassHolder> provider) {
this.provider = provider;
}
@Override
@ -59,7 +59,7 @@ public class MemoryCachedClassReaderSource implements ClassReaderSource, CacheSt
@Override
public ClassReader get(String name) {
Entry entry = cache.computeIfAbsent(name, className -> {
ClassHolder cls = mapper.map(name);
ClassHolder cls = provider.apply(name);
Entry en = new Entry();
if (cls != null) {
ByteArrayOutputStream output = new ByteArrayOutputStream();

View File

@ -15,10 +15,16 @@
*/
package org.teavm.common;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public class CachedMapper<T, R> implements Mapper<T, R> {
private Mapper<T, R> innerMapper;
public class CachedFunction<T, R> implements Function<T, R> {
private Function<T, R> innerFunction;
private Map<T, Wrapper<R>> cache = new LinkedHashMap<>();
private List<KeyListener<T>> keyListeners = new ArrayList<>();
@ -27,8 +33,8 @@ public class CachedMapper<T, R> implements Mapper<T, R> {
boolean computed;
}
public CachedMapper(Mapper<T, R> innerMapper) {
this.innerMapper = innerMapper;
public CachedFunction(Function<T, R> innerFunction) {
this.innerFunction = innerFunction;
}
public R getKnown(T preimage) {
@ -37,15 +43,15 @@ public class CachedMapper<T, R> implements Mapper<T, R> {
}
@Override
public R map(T preimage) {
Wrapper<R> wrapper = cache.get(preimage);
public R apply(T t) {
Wrapper<R> wrapper = cache.get(t);
if (wrapper == null) {
wrapper = new Wrapper<>();
cache.put(preimage, wrapper);
wrapper.value = innerMapper.map(preimage);
cache.put(t, wrapper);
wrapper.value = innerFunction.apply(t);
wrapper.computed = true;
for (KeyListener<T> listener : keyListeners) {
listener.keyAdded(preimage);
listener.keyAdded(t);
}
}
if (!wrapper.computed) {

View File

@ -1,26 +0,0 @@
/*
* Copyright 2013 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;
/**
*
* @author Alexey Andreev
* @param <T> which type this mapper takes.
* @param <R> which type this mapper produces.
*/
public interface Mapper<T, R> {
R map(T preimage);
}

View File

@ -32,12 +32,12 @@ import java.util.Map;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.function.Function;
import org.objectweb.asm.tree.ClassNode;
import org.teavm.cache.IncrementalDependencyProvider;
import org.teavm.cache.IncrementalDependencyRegistration;
import org.teavm.callgraph.CallGraph;
import org.teavm.common.CachedMapper;
import org.teavm.common.Mapper;
import org.teavm.common.CachedFunction;
import org.teavm.common.ServiceRepository;
import org.teavm.diagnostics.Diagnostics;
import org.teavm.interop.PlatformMarker;
@ -84,12 +84,12 @@ public abstract class DependencyAnalyzer implements DependencyInfo {
private DependencyClassSource classSource;
private ClassLoader classLoader;
private Map<String, Map<MethodDescriptor, Optional<MethodHolder>>> methodReaderCache = new HashMap<>(1000, 0.5f);
private Mapper<FieldReference, FieldHolder> fieldReaderCache;
private Function<FieldReference, FieldHolder> fieldReaderCache;
private Map<String, Map<MethodDescriptor, MethodDependency>> methodCache = new HashMap<>();
private Set<MethodReference> reachedMethods = new LinkedHashSet<>();
private Set<MethodReference> readonlyReachedMethods = Collections.unmodifiableSet(reachedMethods);
private CachedMapper<FieldReference, FieldDependency> fieldCache;
private CachedMapper<String, ClassDependency> classCache;
private CachedFunction<FieldReference, FieldDependency> fieldCache;
private CachedFunction<String, ClassDependency> classCache;
private List<DependencyListener> listeners = new ArrayList<>();
private ServiceRepository services;
private Deque<Transition> pendingTransitions = new ArrayDeque<>();
@ -121,11 +121,11 @@ public abstract class DependencyAnalyzer implements DependencyInfo {
classHierarchy = new ClassHierarchy(this.classSource);
this.classLoader = classLoader;
this.services = services;
fieldReaderCache = new CachedMapper<>(preimage -> this.classSource.resolveMutable(preimage));
fieldCache = new CachedMapper<>(preimage -> {
FieldReader field = fieldReaderCache.map(preimage);
fieldReaderCache = new CachedFunction<>(preimage -> this.classSource.resolveMutable(preimage));
fieldCache = new CachedFunction<>(preimage -> {
FieldReader field = fieldReaderCache.apply(preimage);
if (field != null && !field.getReference().equals(preimage)) {
return fieldCache.map(field.getReference());
return fieldCache.apply(field.getReference());
}
FieldDependency node = createFieldNode(preimage, field);
if (field != null && field.getInitialValue() instanceof String) {
@ -134,7 +134,7 @@ public abstract class DependencyAnalyzer implements DependencyInfo {
return node;
});
classCache = new CachedMapper<>(this::createClassDependency);
classCache = new CachedFunction<>(this::createClassDependency);
agent = new DependencyAgent(this);
}
@ -399,7 +399,7 @@ public abstract class DependencyAnalyzer implements DependencyInfo {
if (completing && getClass(className) == null) {
throw new IllegalStateException("Can't link class during completion phase");
}
ClassDependency dep = classCache.map(className);
ClassDependency dep = classCache.apply(className);
if (!dep.activated) {
dep.activated = true;
if (!dep.isMissing()) {
@ -524,7 +524,7 @@ public abstract class DependencyAnalyzer implements DependencyInfo {
}
public FieldDependency linkField(FieldReference fieldRef) {
FieldDependency dep = fieldCache.map(fieldRef);
FieldDependency dep = fieldCache.apply(fieldRef);
if (!dep.activated) {
dep.activated = true;
if (!dep.isMissing()) {

View File

@ -17,7 +17,7 @@ package org.teavm.model;
import java.util.LinkedHashMap;
import java.util.Map;
import org.teavm.common.Mapper;
import java.util.function.Function;
import org.teavm.model.optimization.GlobalValueNumbering;
import org.teavm.model.optimization.UnusedVariableElimination;
@ -43,8 +43,8 @@ public class PreOptimizingClassHolderSource implements ClassHolderSource {
return cls;
}
public static ClassHolder optimize(Mapper<String, ClassHolder> innerSource, String name) {
ClassHolder cls = innerSource.map(name);
public static ClassHolder optimize(Function<String, ClassHolder> innerSource, String name) {
ClassHolder cls = innerSource.apply(name);
if (cls == null) {
return cls;
}

View File

@ -17,7 +17,7 @@ package org.teavm.parsing;
import java.util.Arrays;
import java.util.Map;
import org.teavm.common.Mapper;
import java.util.function.Function;
import org.teavm.interop.Remove;
import org.teavm.interop.Rename;
import org.teavm.interop.Superclass;
@ -52,15 +52,15 @@ import org.teavm.model.instructions.PutFieldInstruction;
public class ClassRefsRenamer extends AbstractInstructionVisitor {
private ReferenceCache referenceCache;
private Mapper<String, String> classNameMapper;
private Function<String, String> classNameMapper;
public ClassRefsRenamer(ReferenceCache referenceCache, Mapper<String, String> classNameMapper) {
public ClassRefsRenamer(ReferenceCache referenceCache, Function<String, String> classNameMapper) {
this.referenceCache = referenceCache;
this.classNameMapper = classNameMapper;
}
public ClassHolder rename(ClassHolder cls) {
ClassHolder renamedCls = new ClassHolder(classNameMapper.map(cls.getName()));
ClassHolder renamedCls = new ClassHolder(classNameMapper.apply(cls.getName()));
renamedCls.getModifiers().addAll(cls.getModifiers());
renamedCls.setLevel(cls.getLevel());
String parent = cls.getParent();
@ -71,7 +71,7 @@ public class ClassRefsRenamer extends AbstractInstructionVisitor {
parent = null;
}
}
renamedCls.setParent(parent != null ? classNameMapper.map(parent) : null);
renamedCls.setParent(parent != null ? classNameMapper.apply(parent) : null);
if (renamedCls.getName().equals(renamedCls.getParent())) {
renamedCls.setParent(null);
}
@ -85,11 +85,11 @@ public class ClassRefsRenamer extends AbstractInstructionVisitor {
renamedCls.addField(rename(field));
}
if (cls.getOwnerName() != null) {
renamedCls.setOwnerName(classNameMapper.map(cls.getOwnerName()));
renamedCls.setOwnerName(classNameMapper.apply(cls.getOwnerName()));
}
rename(cls.getAnnotations(), renamedCls.getAnnotations());
for (String iface : cls.getInterfaces()) {
String mappedIfaceName = classNameMapper.map(iface);
String mappedIfaceName = classNameMapper.apply(iface);
if (!mappedIfaceName.equals(renamedCls.getName())) {
renamedCls.getInterfaces().add(mappedIfaceName);
}
@ -133,7 +133,7 @@ public class ClassRefsRenamer extends AbstractInstructionVisitor {
return referenceCache.getCached(ValueType.arrayOf(rename(itemType)));
} else if (type instanceof ValueType.Object) {
String className = ((ValueType.Object) type).getClassName();
return referenceCache.getCached(ValueType.object(classNameMapper.map(className)));
return referenceCache.getCached(ValueType.object(classNameMapper.apply(className)));
} else {
return type;
}
@ -159,31 +159,31 @@ public class ClassRefsRenamer extends AbstractInstructionVisitor {
private MethodHandle rename(MethodHandle handle) {
switch (handle.getKind()) {
case GET_FIELD:
return MethodHandle.fieldGetter(classNameMapper.map(handle.getClassName()), handle.getName(),
return MethodHandle.fieldGetter(classNameMapper.apply(handle.getClassName()), handle.getName(),
rename(handle.getValueType()));
case GET_STATIC_FIELD:
return MethodHandle.staticFieldGetter(classNameMapper.map(handle.getClassName()), handle.getName(),
return MethodHandle.staticFieldGetter(classNameMapper.apply(handle.getClassName()), handle.getName(),
rename(handle.getValueType()));
case PUT_FIELD:
return MethodHandle.fieldSetter(classNameMapper.map(handle.getClassName()), handle.getName(),
return MethodHandle.fieldSetter(classNameMapper.apply(handle.getClassName()), handle.getName(),
rename(handle.getValueType()));
case PUT_STATIC_FIELD:
return MethodHandle.staticFieldSetter(classNameMapper.map(handle.getClassName()), handle.getName(),
return MethodHandle.staticFieldSetter(classNameMapper.apply(handle.getClassName()), handle.getName(),
rename(handle.getValueType()));
case INVOKE_VIRTUAL:
return MethodHandle.virtualCaller(classNameMapper.map(handle.getClassName()), handle.getName(),
return MethodHandle.virtualCaller(classNameMapper.apply(handle.getClassName()), handle.getName(),
rename(handle.signature()));
case INVOKE_STATIC:
return MethodHandle.staticCaller(classNameMapper.map(handle.getClassName()), handle.getName(),
return MethodHandle.staticCaller(classNameMapper.apply(handle.getClassName()), handle.getName(),
rename(handle.signature()));
case INVOKE_SPECIAL:
return MethodHandle.specialCaller(classNameMapper.map(handle.getClassName()), handle.getName(),
return MethodHandle.specialCaller(classNameMapper.apply(handle.getClassName()), handle.getName(),
rename(handle.signature()));
case INVOKE_CONSTRUCTOR:
return MethodHandle.constructorCaller(classNameMapper.map(handle.getClassName()), handle.getName(),
return MethodHandle.constructorCaller(classNameMapper.apply(handle.getClassName()), handle.getName(),
rename(handle.signature()));
case INVOKE_INTERFACE:
return MethodHandle.interfaceCaller(classNameMapper.map(handle.getClassName()), handle.getName(),
return MethodHandle.interfaceCaller(classNameMapper.apply(handle.getClassName()), handle.getName(),
rename(handle.signature()));
default:
break;
@ -201,7 +201,7 @@ public class ClassRefsRenamer extends AbstractInstructionVisitor {
}
private AnnotationHolder rename(AnnotationHolder annot) {
AnnotationHolder renamedAnnot = new AnnotationHolder(classNameMapper.map(annot.getType()));
AnnotationHolder renamedAnnot = new AnnotationHolder(classNameMapper.apply(annot.getType()));
for (Map.Entry<String, AnnotationValue> entry : annot.getValues().entrySet()) {
renamedAnnot.getValues().put(entry.getKey(), entry.getValue());
}
@ -216,7 +216,7 @@ public class ClassRefsRenamer extends AbstractInstructionVisitor {
}
for (TryCatchBlock tryCatch : basicBlock.getTryCatchBlocks()) {
if (tryCatch.getExceptionType() != null) {
tryCatch.setExceptionType(classNameMapper.map(tryCatch.getExceptionType()));
tryCatch.setExceptionType(classNameMapper.apply(tryCatch.getExceptionType()));
}
}
}
@ -239,7 +239,7 @@ public class ClassRefsRenamer extends AbstractInstructionVisitor {
@Override
public void visit(ConstructInstruction insn) {
insn.setType(classNameMapper.map(insn.getType()));
insn.setType(classNameMapper.apply(insn.getType()));
}
@Override
@ -249,20 +249,20 @@ public class ClassRefsRenamer extends AbstractInstructionVisitor {
@Override
public void visit(GetFieldInstruction insn) {
String className = classNameMapper.map(insn.getField().getClassName());
String className = classNameMapper.apply(insn.getField().getClassName());
insn.setField(referenceCache.getCached(new FieldReference(className, insn.getField().getFieldName())));
}
@Override
public void visit(PutFieldInstruction insn) {
String className = classNameMapper.map(insn.getField().getClassName());
String className = classNameMapper.apply(insn.getField().getClassName());
if (className != insn.getField().getClassName()) {
insn.setField(referenceCache.getCached(new FieldReference(className, insn.getField().getFieldName())));
}
}
@Override
public void visit(InvokeInstruction insn) {
String className = classNameMapper.map(insn.getMethod().getClassName());
String className = classNameMapper.apply(insn.getMethod().getClassName());
ValueType[] signature = insn.getMethod().getSignature();
boolean changed = true;
for (int i = 0; i < signature.length; ++i) {
@ -298,6 +298,6 @@ public class ClassRefsRenamer extends AbstractInstructionVisitor {
@Override
public void visit(InitClassInstruction insn) {
insn.setClassName(classNameMapper.map(insn.getClassName()));
insn.setClassName(classNameMapper.apply(insn.getClassName()));
}
}

View File

@ -21,15 +21,15 @@ import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
import org.teavm.common.CachedMapper;
import org.teavm.common.Mapper;
import java.util.function.Function;
import org.teavm.common.CachedFunction;
import org.teavm.model.ClassHolder;
import org.teavm.model.ReferenceCache;
public class ClasspathResourceMapper implements Mapper<String, ClassHolder>, ClassDateProvider {
public class ClasspathResourceMapper implements Function<String, ClassHolder>, ClassDateProvider {
private static final String PACKAGE_PREFIX = "packagePrefix.";
private static final String CLASS_PREFIX = "classPrefix.";
private Mapper<String, ClassHolder> innerMapper;
private Function<String, ClassHolder> innerMapper;
private List<Transformation> transformations = new ArrayList<>();
private ClassRefsRenamer renamer;
private ClassLoader classLoader;
@ -44,8 +44,8 @@ public class ClasspathResourceMapper implements Mapper<String, ClassHolder>, Cla
}
public ClasspathResourceMapper(ClassLoader classLoader, ReferenceCache referenceCache,
Mapper<String, ClassHolder> innerMapper) {
this.innerMapper = innerMapper;
Function<String, ClassHolder> provider) {
this.innerMapper = provider;
this.referenceCache = referenceCache;
try {
Enumeration<URL> resources = classLoader.getResources("META-INF/teavm.properties");
@ -62,18 +62,18 @@ public class ClasspathResourceMapper implements Mapper<String, ClassHolder>, Cla
} catch (IOException e) {
throw new RuntimeException("Error reading resources", e);
}
renamer = new ClassRefsRenamer(referenceCache, new CachedMapper<>(classNameMapper));
renamer = new ClassRefsRenamer(referenceCache, new CachedFunction<>(classNameMapper));
this.classLoader = classLoader;
}
public ClasspathResourceMapper(Properties properties, ReferenceCache referenceCache,
Mapper<String, ClassHolder> innerMapper) {
Function<String, ClassHolder> innerMapper) {
this.innerMapper = innerMapper;
this.referenceCache = referenceCache;
Map<String, Transformation> transformationMap = new HashMap<>();
loadProperties(properties, transformationMap);
transformations.addAll(transformationMap.values());
renamer = new ClassRefsRenamer(referenceCache, new CachedMapper<>(classNameMapper));
renamer = new ClassRefsRenamer(referenceCache, new CachedFunction<>(classNameMapper));
}
private void loadProperties(Properties properties, Map<String, Transformation> cache) {
@ -103,13 +103,13 @@ public class ClasspathResourceMapper implements Mapper<String, ClassHolder>, Cla
}
@Override
public ClassHolder map(String name) {
public ClassHolder apply(String name) {
for (Transformation transformation : transformations) {
if (name.startsWith(transformation.packageName)) {
int index = name.lastIndexOf('.');
String className = name.substring(index + 1);
String packageName = index > 0 ? name.substring(0, index) : "";
ClassHolder classHolder = innerMapper.map(transformation.packagePrefix + packageName
ClassHolder classHolder = innerMapper.apply(transformation.packagePrefix + packageName
+ "." + transformation.classPrefix + className);
if (classHolder != null) {
classHolder = renamer.rename(classHolder);
@ -117,7 +117,7 @@ public class ClasspathResourceMapper implements Mapper<String, ClassHolder>, Cla
return classHolder;
}
}
return innerMapper.map(name);
return innerMapper.apply(name);
}
private String renameClass(String name) {
@ -136,7 +136,7 @@ public class ClasspathResourceMapper implements Mapper<String, ClassHolder>, Cla
return name;
}
private Mapper<String, String> classNameMapper = this::renameClass;
private Function<String, String> classNameMapper = this::renameClass;
@Override
public Date getModificationDate(String className) {

View File

@ -15,20 +15,20 @@
*/
package org.teavm.parsing.resource;
import org.teavm.common.CachedMapper;
import org.teavm.common.Mapper;
import java.util.function.Function;
import org.teavm.common.CachedFunction;
import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderSource;
public class MapperClassHolderSource implements ClassHolderSource {
private Mapper<String, ClassHolder> mapper;
private Function<String, ClassHolder> mapper;
public MapperClassHolderSource(Mapper<String, ClassHolder> mapper) {
this.mapper = new CachedMapper<>(mapper);
public MapperClassHolderSource(Function<String, ClassHolder> mapper) {
this.mapper = new CachedFunction<>(mapper);
}
@Override
public ClassHolder get(String name) {
return mapper.map(name);
return mapper.apply(name);
}
}

View File

@ -17,14 +17,14 @@ package org.teavm.parsing.resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Function;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
import org.teavm.common.Mapper;
import org.teavm.model.ClassHolder;
import org.teavm.model.ReferenceCache;
import org.teavm.parsing.Parser;
public class ResourceClassHolderMapper implements Mapper<String, ClassHolder> {
public class ResourceClassHolderMapper implements Function<String, ClassHolder> {
private Parser parser;
private ResourceReader resourceReader;
@ -34,7 +34,7 @@ public class ResourceClassHolderMapper implements Mapper<String, ClassHolder> {
}
@Override
public ClassHolder map(String name) {
public ClassHolder apply(String name) {
ClassNode clsNode = new ClassNode();
String resourceName = name.replace('.', '/') + ".class";
if (!resourceReader.hasResource(resourceName)) {

View File

@ -42,6 +42,7 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@ -71,7 +72,6 @@ import org.teavm.cache.InMemoryMethodNodeCache;
import org.teavm.cache.InMemoryProgramCache;
import org.teavm.cache.InMemorySymbolTable;
import org.teavm.cache.MemoryCachedClassReaderSource;
import org.teavm.common.Mapper;
import org.teavm.debugging.information.DebugInformation;
import org.teavm.debugging.information.DebugInformationBuilder;
import org.teavm.dependency.FastDependencyAnalyzer;
@ -745,9 +745,9 @@ public class CodeServlet extends HttpServlet {
ClassLoader classLoader = initClassLoader();
ClasspathResourceReader reader = new ClasspathResourceReader(classLoader);
ResourceClassHolderMapper rawMapper = new ResourceClassHolderMapper(reader, referenceCache);
Mapper<String, ClassHolder> classPathMapper = new ClasspathResourceMapper(classLoader, referenceCache,
Function<String, ClassHolder> classPathMapper = new ClasspathResourceMapper(classLoader, referenceCache,
rawMapper);
classSource.setMapper(name -> PreOptimizingClassHolderSource.optimize(classPathMapper, name));
classSource.setProvider(name -> PreOptimizingClassHolderSource.optimize(classPathMapper, name));
long startTime = System.currentTimeMillis();
JavaScriptTarget jsTarget = new JavaScriptTarget();