ctubbsii commented on code in PR #113:
URL: https://github.com/apache/accumulo-access/pull/113#discussion_r2933431902
##########
modules/core/src/main/java/org/apache/accumulo/access/impl/AccessEvaluatorImpl.java:
##########
@@ -43,7 +45,27 @@ public final class AccessEvaluatorImpl implements
AccessEvaluator {
*/
AccessEvaluatorImpl(Predicate<String> authorizationChecker,
AuthorizationValidator authorizationValidator) {
- this.authorizedPredicate = auth ->
authorizationChecker.test(auth.toString());
+
+ // This map helps avoid allocating string objects on each call to this
predicate
+ Map<CharsWrapper,Boolean> checkCache = new ConcurrentHashMap<>();
+ this.authorizedPredicate = auth -> {
+ if (auth instanceof CharsWrapper wrapped) {
+ // Try to avoid allocating a string object and copying the char array.
+ Boolean cachedResult = checkCache.get(wrapped);
+ if (cachedResult == null) {
+ // Not in cache, so have to allocate and copy
+ String authStr = wrapped.toString();
+ cachedResult = authorizationChecker.test(authStr);
+ // Lets put it in the cache for the next time. Must allocate a new
CharsWrapper as these
+ // are mutable, so can not put the one passed to this lambda in the
map.
+ checkCache.put(new CharsWrapper(authStr.toCharArray()),
cachedResult);
+ }
+ return cachedResult;
+ } else {
+ return authorizationChecker.test(auth.toString());
Review Comment:
Why do we only cache the results for CharsWrapper instances? Wouldn't we
want to cache for all auths? (all instanceof CharSequence)
##########
modules/core/src/main/java/org/apache/accumulo/access/impl/AccessEvaluatorImpl.java:
##########
@@ -43,7 +45,27 @@ public final class AccessEvaluatorImpl implements
AccessEvaluator {
*/
AccessEvaluatorImpl(Predicate<String> authorizationChecker,
AuthorizationValidator authorizationValidator) {
- this.authorizedPredicate = auth ->
authorizationChecker.test(auth.toString());
+
+ // This map helps avoid allocating string objects on each call to this
predicate
+ Map<CharsWrapper,Boolean> checkCache = new ConcurrentHashMap<>();
+ this.authorizedPredicate = auth -> {
+ if (auth instanceof CharsWrapper wrapped) {
+ // Try to avoid allocating a string object and copying the char array.
+ Boolean cachedResult = checkCache.get(wrapped);
Review Comment:
Could this be replaced with `checkCache.computeIfAbsent`?
##########
modules/core/src/main/java/org/apache/accumulo/access/impl/AccessEvaluatorImpl.java:
##########
@@ -43,7 +45,27 @@ public final class AccessEvaluatorImpl implements
AccessEvaluator {
*/
AccessEvaluatorImpl(Predicate<String> authorizationChecker,
AuthorizationValidator authorizationValidator) {
- this.authorizedPredicate = auth ->
authorizationChecker.test(auth.toString());
+
+ // This map helps avoid allocating string objects on each call to this
predicate
+ Map<CharsWrapper,Boolean> checkCache = new ConcurrentHashMap<>();
+ this.authorizedPredicate = auth -> {
+ if (auth instanceof CharsWrapper wrapped) {
+ // Try to avoid allocating a string object and copying the char array.
+ Boolean cachedResult = checkCache.get(wrapped);
+ if (cachedResult == null) {
+ // Not in cache, so have to allocate and copy
+ String authStr = wrapped.toString();
+ cachedResult = authorizationChecker.test(authStr);
Review Comment:
Could we avoid this toString here and below by just making the predicate one
of CharSequence instead of String? Or would that be a problem because mutable
CharSequences could be modified as a side-effect of the predicate?
--
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]