Class ClassValueSet

java.lang.Object
java.util.AbstractCollection<Class<?>>
java.util.AbstractSet<Class<?>>
com.cedarsoftware.util.ClassValueSet
All Implemented Interfaces:
Iterable<Class<?>>, Collection<Class<?>>, Set<Class<?>>

public class ClassValueSet extends AbstractSet<Class<?>>
A Set implementation for Class objects that leverages a ClassValue cache for extremely fast membership tests. This specialized collection is designed for scenarios where you frequently need to check if a Class is a member of a set.

Performance Advantages

ClassValueSet provides significantly faster contains() operations compared to standard Set implementations:

  • 2-10x faster than HashSet for membership checks
  • 3-15x faster than ConcurrentHashMap.keySet() for concurrent access patterns
  • The performance advantage increases with contention (multiple threads)
  • Most significant when checking the same classes repeatedly

How It Works

The implementation utilizes Java's ClassValue mechanism, which is specially optimized in the JVM through:

  • Thread-local caching for reduced contention
  • Identity-based lookups (faster than equality checks)
  • Special VM support that connects directly to Class metadata structures
  • Optimized memory layout that can reduce cache misses

Ideal Use Cases

ClassValueSet is ideal for:

  • High read-to-write ratio scenarios (read-mostly workloads)
  • Relatively static sets of classes that are checked frequently
  • Performance-critical operations in hot code paths
  • Security blocklists (checking if a class is forbidden)
  • Feature flags or capability testing based on class membership
  • Type handling in serialization/deserialization frameworks

Trade-offs

The performance benefits come with some trade-offs:

  • Higher memory usage (maintains both a backing set and ClassValue cache)
  • Write operations (add/remove) aren't faster and may be slightly slower
  • Only Class objects benefit from the optimized lookups

Thread Safety

This implementation is thread-safe for all operations.

Usage Example


 // Create a set of blocked classes for security checks
 ClassValueSet blockedClasses = ClassValueSet.of(
     ClassLoader.class,
     Runtime.class,
     ProcessBuilder.class
 );

 // Fast membership check in a security-sensitive context
 public void verifyClass(Class<?> clazz) {
     if (blockedClasses.contains(clazz)) {
         throw new SecurityException("Access to " + clazz.getName() + " is not allowed");
     }
 }
 

Important Performance Warning

Wrapping this class with standard collection wrappers like Collections.unmodifiableSet() will destroy the ClassValue performance benefits. Always use the raw ClassValueSet directly or use the provided unmodifiableView() method if immutability is required.

Author:
John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
See Also: