Performance Optimization: Replace Linear Property Search with O(1) HashMap Lookup #172
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces a significant performance optimization to the Olingo OData4 library by replacing linear O(n) property searches with O(1) HashMap lookups in the serialization components. The optimization is particularly beneficial for entities with a large number of properties, where the previous implementation would perform poorly.
Problem
Previously, the serializer classes (
ODataJsonSerializer,ODataXmlSerializer,JsonDeltaSerializer, andJsonDeltaSerializerWithNavigations) used a linear search approach to find properties by name within entity property lists. This resulted in O(n) time complexity for each property lookup, where n is the number of properties in an entity. For entities with many properties, this could lead to significant performance degradation, especially when serializing complex entities or large collections.Solution
This PR introduces a new
PropertyFinderclass that leverages aConcurrentHashMapto index properties by name during initialization. This provides O(1) average time complexity for property lookups while maintaining thread safety through the use ofConcurrentHashMap.The
PropertyFinderclass:find(String propertyName)method for O(1) lookupsPerformance Benefits
Benchmark Results
In entities with 50+ properties, preliminary testing shows:
Backward Compatibility
This change is fully backward compatible. The external API remains unchanged, and existing functionality is preserved while delivering improved performance.
Testing
All existing unit tests pass without modification. Found following numbers with the performance test for property find portion
above result is only to find one property, when we deal with a data with 10000+ rows and 100+ columns the time saved would be enormous with reduced cpu consumption.