sodonnel commented on PR #11884:
URL: https://github.com/apache/ozone/pull/11884#issuecomment-6604291502

   I think this PR can be simplified and improved with something like the 
following:
   
   ```
   // Fast path: exact input string → cached instance (no regex on repeated 
calls)
   private static final ConcurrentHashMap<String, ECReplicationConfig> 
BY_STRING =
       new ConcurrentHashMap<>();
   
   // Intern map keyed by value equality (uses existing equals/hashCode on the 
4 fields)
   private static final ConcurrentHashMap<ECReplicationConfig, 
ECReplicationConfig> CACHE =
       new ConcurrentHashMap<>();
   
   public static ECReplicationConfig getInstance(String string) {
       ECReplicationConfig cached = BY_STRING.get(string);
       if (cached != null) return cached;
       ECReplicationConfig config = intern(new ECReplicationConfig(string));
       BY_STRING.put(string, config);  // caches this spelling too 
("RS-6-3-1024K", etc.)
       return config;
   }
   
   public static ECReplicationConfig getInstance(HddsProtos.ECReplicationConfig 
proto) {
       return intern(new ECReplicationConfig(proto));
   }
   
   private static ECReplicationConfig intern(ECReplicationConfig config) {
       return CACHE.computeIfAbsent(config, k -> k);
   }
   ```
   
   This no longer has to pre-compute all the defaults and it handles the case 
where someone is using something that was not part of the defaults and the 
defaults have drifted.
   
   The only negative is a temporary ECReplicationConfig is created for the 
lookup key and discarded on a cache hit. It is a pretty simple object with only 
4 fields, and creating that temporary object is likely much less than the proto 
deserialisation already.
   
   I also thing the negative is greatly out weighted by the much simpler code.


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to