Skip to content

Conversation

gnodet
Copy link
Contributor

@gnodet gnodet commented Jun 29, 2025

Overview

This PR replaces the buggy SoftIdentityMap with a modern, thread-safe cache implementation and adds model object pooling for better memory efficiency.

Note: This is a simplified version of #2506 targeted for Maven 4.0.0, focusing on the core cache replacement and object pooling without complex configuration and statistics.

Problem

The existing SoftIdentityMap had bugs and needed replacement with a modern cache implementation. Additionally, Maven 4 model objects could benefit from object pooling to reduce memory allocation overhead.

Solution

1. Modern Cache Infrastructure

Replaced SoftIdentityMap with a complete new cache infrastructure:

  • Cache<K,V> interface - Clean, simple API without complex configuration or statistics
  • DefaultCache<K,V> - Thread-safe implementation featuring:
    • Soft references for automatic memory management under pressure
    • Identity-based key comparison using System.identityHashCode() for better performance
    • Concurrent computation ensuring mapping functions called at most once per key
    • Automatic cleanup of garbage-collected entries
  • CacheMapAdapter<K,V> - Backward compatibility adapter for existing Map-based APIs

2. Model Object Pooling

Added object pooling for Maven model objects:

  • ModelObjectProcessor interface for processing model objects
  • DefaultModelObjectPool implementation providing object pooling
  • Integrated into Modello template generation for model classes only
  • Service registration for proper dependency injection

Key Features

  • Thread-safe: Uses ConcurrentHashMap and proper synchronization
  • Memory efficient: Soft references allow GC under memory pressure
  • Identity-based: Better performance with identity comparison
  • Simple: No statistics or complex configuration (simplified from Improve Maven cache architecture for better memory efficiency and performance #2506)
  • Concurrent-safe: Prevents duplicate computation under concurrent access
  • Object pooling: Reduces memory allocation for model objects

Files Changed

Added

  • api/maven-api-model/src/main/java/org/apache/maven/api/model/ModelObjectProcessor.java - Object processor interface
  • impl/maven-impl/src/main/java/org/apache/maven/impl/cache/Cache.java - New cache interface
  • impl/maven-impl/src/main/java/org/apache/maven/impl/cache/DefaultCache.java - Thread-safe cache implementation
  • impl/maven-impl/src/main/java/org/apache/maven/impl/cache/CacheMapAdapter.java - Backward compatibility adapter
  • impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelObjectPool.java - Object pool implementation
  • impl/maven-impl/src/main/resources/META-INF/services/org.apache.maven.api.model.ModelObjectProcessor - Service registration
  • impl/maven-impl/src/test/java/org/apache/maven/impl/cache/CacheTest.java - Comprehensive test suite

Modified

  • src/mdo/model.vm - Integrated object pooling for model classes
  • impl/maven-impl/src/main/java/org/apache/maven/impl/cache/DefaultRequestCache.java - Use new cache implementation
  • Generated model classes - Updated with object pooling integration

Removed

  • impl/maven-impl/src/main/java/org/apache/maven/impl/cache/SoftIdentityMap.java - Buggy old implementation
  • impl/maven-impl/src/test/java/org/apache/maven/impl/cache/SoftIdentityMapTest.java - Old tests

Testing

Comprehensive Test Coverage

9 new cache tests covering:

  • Thread-safe concurrent computation
  • Identity-based key comparison
  • Soft reference garbage collection
  • Exception handling
  • Basic CRUD operations
  • Null value handling
  • Clear functionality

Verification

  • ✅ All 12 cache-related tests pass
  • ✅ Full Maven build succeeds (mvn install -DskipTests)
  • ✅ Generated model classes correctly use object pooling
  • ✅ Non-model modules unaffected
  • ✅ No compilation errors in any module

Performance Impact

Before

  • ❌ Buggy cache implementation with potential memory leaks
  • ❌ Thread safety issues in caching
  • ❌ No object pooling for model objects

After

  • ✅ Modern, thread-safe cache implementation
  • ✅ Better memory management with soft references
  • ✅ Improved performance with identity-based keys
  • ✅ Reduced memory allocation through object pooling
  • ✅ Comprehensive test coverage

Backward Compatibility

This change is fully backward compatible:

  • Existing cache usage patterns continue to work via CacheMapAdapter
  • No API changes to public interfaces
  • Service registration maintains dependency injection compatibility
  • Object pooling is transparent to existing code

Relationship to #2506

This PR implements the core functionality from #2506 but simplified for Maven 4.0.0:

  • ✅ Replaces SoftIdentityMap with modern Cache
  • ✅ Adds object pooling for model objects
  • ❌ Removes complex configuration options
  • ❌ Removes statistics and monitoring
  • ❌ Focuses on default behavior without customization

Ready for review and merge 🚀

@gnodet gnodet marked this pull request as draft June 29, 2025 21:30
@gnodet gnodet added this to the 4.0.0 milestone Jun 29, 2025
@gnodet gnodet changed the title Fix ModelObjectProcessor integration and replace SoftIdentityMap with modern Cache Replace SoftIdentityMap with modern Cache implementation and add model object pooling Jun 29, 2025
@gnodet gnodet force-pushed the fix-model-object-processor-and-cache-replacement branch from 4dab0af to 92c8571 Compare June 29, 2025 22:30
@gnodet gnodet force-pushed the fix-model-object-processor-and-cache-replacement branch from ceae74f to 1b4bf00 Compare July 15, 2025 19:55
gnodet added 2 commits July 16, 2025 07:04
…formance

This comprehensive enhancement to Maven's caching system addresses memory issues
and significantly improves performance through several key improvements:

## Key Features:
- Enhanced DefaultRequestCache with configurable reference types and CSS-like selectors
- Pluggable ModelObjectPool service architecture with configurable object types
- Comprehensive cache statistics with eviction tracking
- Improved InputLocation and InputSource with ImmutableCollections
- Custom equality strategy for Dependency pooling
- Enhanced parent request matching with interface checking
- Configurable cache statistics display

## Performance Results:
- Maven 3: Requires -Xmx1536m, runs in 45 seconds
- Maven 4.0.0-rc-4: Runs with -Xmx1024m in 2'32" (cannot run with -Xmx512m)
- Maven 4.0.0-rc-4 with -Xmx1536m: 2'5"
- Maven 4.0.0-rc-4 + maven3Personality with -Xmx1536m: 1'14"
- Maven 4 + this PR: Runs with -Xmx512m in 3'42" (more memory does not help)
- Maven 4 + this PR + maven3Personality: Runs with -Xmx512m in 1'0"

## Memory Improvements:
- Reduced minimum memory requirement from 1024m to 512m
- Eliminated memory scaling issues - additional memory beyond 512m provides no benefit
- Significant reduction in memory pressure through improved caching strategies

This PR definitively solves memory problems while maintaining or improving performance.
This commit removes the following components from the comprehensive PR apache#2506:
- CacheConfigurationResolver and related configuration classes
- CacheStatistics and related statistics tracking
- Cache configuration parsing and system property handling
- Factory methods from InputSource and InputLocation (merge methods)
- Complex object pooling configuration (keeping only Dependency pooling)

Remaining work:
- Fix compilation errors for missing interfaces
- Adapt to current master branch API structure
- Ensure all tests pass

This trimmed version keeps the core cache improvements while removing
the configuration complexity that's not needed for the 4.0.x branch.
@gnodet gnodet force-pushed the fix-model-object-processor-and-cache-replacement branch from 7f7c0d8 to 104e4f4 Compare July 16, 2025 05:05
@gnodet gnodet changed the base branch from master to maven-4.0.x July 16, 2025 05:05
@gnodet gnodet marked this pull request as ready for review July 17, 2025 06:14
@gnodet gnodet closed this Jul 17, 2025
@github-actions github-actions bot removed this from the 4.0.0 milestone Jul 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant