Significantly reduce cost of Access#expandAccessValues and RoutingIslandsTest#getDefaultAccessTags

CPU samples and memory allocations for those two methods was reduced by ~1/3.

JVM method CPU samples were reduced by ~1/4.

Signed-off-by: Taylor Smock <tsmock@meta.com>
pull/28/head
Taylor Smock 2023-07-20 11:25:17 -06:00
rodzic 9064644785
commit 8c609c0ab2
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 233BB2E466604E27
2 zmienionych plików z 26 dodań i 9 usunięć

Wyświetl plik

@ -42,6 +42,11 @@ import org.openstreetmap.josm.tools.Pair;
public class RoutingIslandsTest extends Test {
private static final Map<Integer, Severity> SEVERITY_MAP = new HashMap<>();
/**
* A map of &lt;direction, &lt;mode, direction:mode&gt;&gt; to reduce concat
* costs
*/
private static final Map<String, Map<String, String>> DIRECTION_MAP = new HashMap<>(3);
/** The code for the routing island validation test */
public static final int ROUTING_ISLAND = 55000;
/** The code for ways that are not connected to other ways, and are routable */
@ -174,11 +179,9 @@ public class RoutingIslandsTest extends Test {
.filter(way -> !incomingWays.contains(way) || !outgoingWays.contains(way))
.filter(way -> Access.getPositiveAccessValues().contains(
getDefaultAccessTags(way).getOrDefault(currentTransportMode, Access.AccessTags.NO.getKey())))
.collect(Collectors.toSet()))
.stream()
.map(way -> new Pair<>(
(incomingWays.containsAll(way) ? marktr("outgoing") : marktr("incoming")), way))
.collect(Collectors.toList());
.collect(Collectors.toSet())).stream()
.map(way -> new Pair<>((incomingWays.containsAll(way) ? marktr("outgoing") : marktr("incoming")), way))
.collect(Collectors.toList());
createErrors(problematic, currentTransportMode);
}
@ -441,12 +444,23 @@ public class RoutingIslandsTest extends Test {
tags.putAll(Access.expandAccessValues(tags));
for (String direction : Arrays.asList("", "forward:", "backward:")) {
Access.getTransportModes().stream().map(direction::concat).filter(tags::containsKey)
.forEach(mode -> access.put(mode, tags.get(direction.concat(mode))));
access.putAll(Access.getTransportModes().stream().map(mode -> getCachedDirectionMode(direction, mode))
.filter(tags::containsKey).collect(Collectors.toMap(mode -> mode, tags::get)));
}
return access;
}
private static String getCachedDirectionMode(String direction, String mode) {
if (!DIRECTION_MAP.containsKey(direction)) {
DIRECTION_MAP.put(direction, new HashMap<>(Access.getTransportModes().size()));
}
final var directionMap = DIRECTION_MAP.get(direction);
if (!directionMap.containsKey(mode)) {
directionMap.put(mode, direction.concat(mode));
}
return directionMap.get(mode);
}
private static TagMap getDefaultWaterwayAccessTags(TagMap tags) {
if ("river".equals(tags.get(WATERWAY))) {
tags.putIfAbsent("boat", Access.AccessTags.YES.getKey());

Wyświetl plik

@ -329,6 +329,9 @@ public final class Access {
*/
TRAIN("train", RAIL_TRANSPORT_TYPE);
/** Used to avoid array instantiations */
private static AccessTags[] allValues = values();
private final String key;
private final AccessTags type;
@ -382,7 +385,7 @@ public final class Access {
* @return The AccessTags enum that matches the childrenMode, or null
*/
public static AccessTags get(String childrenMode) {
for (AccessTags value : values()) {
for (AccessTags value : allValues) {
if (value.getKey().equalsIgnoreCase(childrenMode)) {
return value;
}
@ -400,7 +403,7 @@ public final class Access {
* @return A collection of access tags that match the given transport type
*/
public static Collection<AccessTags> getByTransportType(AccessTags type) {
return Arrays.stream(values()).filter(type::parentOf).collect(Collectors.toList());
return Arrays.stream(allValues).filter(type::parentOf).collect(Collectors.toList());
}
}