Update AbstractBaseGraph.java#7641
Conversation
Upon reviewing the AbstractBaseGraph class in the Guava library, I have identified an opportunity to enhance its performance by reducing redundant object creation. Specifically, in methods that return collections, such as edges(), new AbstractSet instances are created each time the method is called. This can be optimized by caching these collections, assuming the graph's structure remains unchanged. Proposed Improvement: Introduce caching for the edges() method to avoid unnecessary object creation. This involves storing the result in a private transient field and returning the cached collection on subsequent calls, provided the graph hasn't been modified. P,S: Do let me know if you want me to change something from it...
|
@Abubakar-Sattar thanks for your suggestion, and apologies for the late response. The idea of caching the result (and invalidating that cache if any changes are made) has merit. However:
Regarding (1), I noticed that
So I'm pretty sure this doesn't currently compile, nor work as intended. Regarding (2), if you know that the graph will not change, you can use It's possible I've missed something, but I think I'd need more convincing that this change would be an overall benefit before it would be worth fixing the issues noted under (1). |
Upon reviewing the AbstractBaseGraph class in the Guava library, I have identified an opportunity to enhance its performance by reducing redundant object creation. Specifically, in methods that return collections, such as edges(), new AbstractSet instances are created each time the method is called. This can be optimized by caching these collections, assuming the graph's structure remains unchanged.
Proposed Improvement:
Introduce caching for the edges() method to avoid unnecessary object creation. This involves storing the result in a private transient field and returning the cached collection on subsequent calls, provided the graph hasn't been modified.
PS: @cpovirk Do let me know if you want me to change something from it...