Columnar Data Structure in Java
What if we need traditional object-oriented API and extra high performance for field-wise operations in one place? I designed my CPS exactly having this in mind. The article is for describing the idea behind the library. Imagine you have 10 million product objects, which have the following structure: class Product { String name ; int quantity ; int price ; } You need to calculate the grand total…
When working with large datasets in Java, traditional object-oriented APIs can fall short when it comes to high performance for field-wise operations. To address this, a columnar data structure was designed, demonstrating its idea behind a library.
Consider a scenario where you have 10 million product objects, each with a name, quantity, and price. The most straightforward approach is to create an ArrayList of Product objects, iterate over them, and calculate the grand total value by summing quantity * price for each product. However, this method involves "pointer chasing," as Java arrays store references to objects, not the data itself. This leads to inefficiencies, especially when accessing fields sequentially.
An alternative approach is to use a columnar data structure, where each column (such as names, quantities, and prices) is stored in a separate array. This layout enables efficient CPU cache utilization during sequential access. As a result, there's no need for pointer chasing in loops, as primitive columns can be loaded directly from the arrays.
However, creating this columnar structure isn't always convenient. It requires filling the structure first and handling bookkeeping for offsets during iteration. Moreover, the resulting data layout doesn't contain the actual product objects. This is where the Columnar Projection System (CPS) comes in handy. By designing a projection based on the Product class, CPS handles the heavy lifting, allowing for efficient field-wise operations and SIMD (Single Instruction, Multiple Data) instructions.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.