Source:
<code>private Map<String, Map<String, Object>> getSimpleDatasource(String dataSourceName) { Map<String, Object> tiers = new HashMap<>(); Map<String, Object> segments = new HashMap<>(); Map<String, Map<String, Object>> retVal = ImmutableMap.of( "tiers", tiers, "segments", segments ); Set<SegmentId> totalDistinctSegments = new HashSet<>(); Map<String, HashSet<Object>> tierDistinctSegments = new HashMap<>(); long totalSegmentSize = 0; DateTime minTime = DateTimes.MAX; DateTime maxTime = DateTimes.MIN; String tier; for (DruidServer druidServer : serverInventoryView.getInventory()) { DruidDataSource druidDataSource = druidServer.getDataSource(dataSourceName); tier = druidServer.getTier(); if (druidDataSource == null) { continue; } <START> tierDistinctSegments.putIfAbsent(tier, new HashSet<>()); <END> long dataSourceSegmentSize = 0; for (DataSegment dataSegment : druidDataSource.getSegments()) { if (!tierDistinctSegments.get(tier).contains(dataSegment.getId())) { dataSourceSegmentSize += dataSegment.getSize(); tierDistinctSegments.get(tier).add(dataSegment.getId()); } if (totalDistinctSegments.add(dataSegment.getId())) { totalSegmentSize += dataSegment.getSize(); minTime = DateTimes.min(minTime, dataSegment.getInterval().getStart()); maxTime = DateTimes.max(maxTime, dataSegment.getInterval().getEnd()); } } Map<String, Object> tierStats = (Map) tiers.get(tier); if (tierStats == null) { tierStats = new HashMap<>(); tiers.put(druidServer.getTier(), tierStats); } tierStats.put("segmentCount", tierDistinctSegments.get(tier).size()); long segmentSize = MapUtils.getLong(tierStats, "size", 0L); tierStats.put("size", segmentSize + dataSourceSegmentSize); } segments.put("count", totalDistinctSegments.size()); segments.put("size", totalSegmentSize); segments.put("minTime", minTime); segments.put("maxTime", maxTime); return retVal; }</code><technical_language>This is a good candidate for computeIfAbsent, avoid extra allocation. (It's short lived a big deal, but, still.)</technical_language>

Target:
private Map<String, Map<String, Object>> getSimpleDatasource(String dataSourceName) { Map<String, Object> tiers = new HashMap<>(); Map<String, Object> segments = new HashMap<>(); Map<String, Map<String, Object>> retVal = ImmutableMap.of( "tiers", tiers, "segments", segments ); Set<SegmentId> totalDistinctSegments = new HashSet<>(); Map<String, HashSet<Object>> tierDistinctSegments = new HashMap<>(); long totalSegmentSize = 0; DateTime minTime = DateTimes.MAX; DateTime maxTime = DateTimes.MIN; String tier; for (DruidServer druidServer : serverInventoryView.getInventory()) { DruidDataSource druidDataSource = druidServer.getDataSource(dataSourceName); tier = druidServer.getTier(); if (druidDataSource == null) { continue; } tierDistinctSegments.computeIfAbsent(tier, k -> new HashSet<>()); long dataSourceSegmentSize = 0; for (DataSegment dataSegment : druidDataSource.getSegments()) { if (!tierDistinctSegments.get(tier).contains(dataSegment.getId())) { dataSourceSegmentSize += dataSegment.getSize(); tierDistinctSegments.get(tier).add(dataSegment.getId()); } if (totalDistinctSegments.add(dataSegment.getId())) { totalSegmentSize += dataSegment.getSize(); minTime = DateTimes.min(minTime, dataSegment.getInterval().getStart()); maxTime = DateTimes.max(maxTime, dataSegment.getInterval().getEnd()); } } Map<String, Object> tierStats = (Map) tiers.get(tier); if (tierStats == null) { tierStats = new HashMap<>(); tiers.put(druidServer.getTier(), tierStats); } tierStats.put("segmentCount", tierDistinctSegments.get(tier).size()); long segmentSize = MapUtils.getLong(tierStats, "size", 0L); tierStats.put("size", segmentSize + dataSourceSegmentSize); } segments.put("count", totalDistinctSegments.size()); segments.put("size", totalSegmentSize); segments.put("minTime", minTime); segments.put("maxTime", maxTime); return retVal; }