ijuma commented on a change in pull request #11311: URL: https://github.com/apache/kafka/pull/11311#discussion_r712520803
########## File path: metadata/src/main/java/org/apache/kafka/image/TopicsImage.java ########## @@ -92,6 +98,148 @@ public int hashCode() { return Objects.hash(topicsById, topicsByName); } + /** + * Expose a view of this TopicsImage as a map from topic names to IDs. + * + * Like TopicsImage itself, this map is immutable. + */ + public Map<String, Uuid> topicNameToIdView() { + return new TopicNameToIdMap(); + } + + class TopicNameToIdMap extends AbstractMap<String, Uuid> { + private final TopicNameToIdMapEntrySet set = new TopicNameToIdMapEntrySet(); + + @Override + public boolean containsKey(Object key) { + return topicsByName.containsKey(key); + } + + @Override + public Uuid get(Object key) { + TopicImage image = topicsByName.get(key); + if (image == null) return null; + return image.id(); + } + + @Override + public Set<Entry<String, Uuid>> entrySet() { + return set; + } + } + + class TopicNameToIdMapEntrySet extends AbstractSet<Entry<String, Uuid>> { + @Override + public Iterator<Entry<String, Uuid>> iterator() { + return new TopicNameToIdMapEntrySetIterator(topicsByName.entrySet().iterator()); + } + + @SuppressWarnings("rawtypes") + @Override + public boolean contains(Object o) { + if (!(o instanceof Entry)) return false; + Entry other = (Entry) o; + TopicImage image = topicsByName.get(other.getKey()); + if (image == null) return false; + return image.id().equals(other.getValue()); + } + + @Override + public int size() { + return topicsByName.size(); + } + } + + static class TopicNameToIdMapEntrySetIterator implements Iterator<Entry<String, Uuid>> { + private final Iterator<Entry<String, TopicImage>> iterator; + + TopicNameToIdMapEntrySetIterator(Iterator<Entry<String, TopicImage>> iterator) { + this.iterator = iterator; + } + + @Override + public boolean hasNext() { + return this.iterator.hasNext(); + } + + @Override + public Entry<String, Uuid> next() { + Entry<String, TopicImage> entry = iterator.next(); + return new SimpleImmutableEntry<>(entry.getKey(), entry.getValue().id()); Review comment: We don't know if the allocations are short-lived since it depends on what the caller does with them. In the best case, they `Entry` is immediately discarded and the JVM elides the allocation. But escape analysis is a bit hit and miss. If the allocation does happen, then short-lived allocations are cheaper (the cost of the collection is proportional to the live objects remaining typically). If you say that we generally don't iterate, then it's not an issue, but how can we make it data driven? Do we have an existing benchmark where we can show reduced allocation? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org