nikolaobradovic-db opened a new pull request, #12483:
URL: https://github.com/apache/arrow-rs/pull/12483

   # Which issue does this PR close?
   
   - Closes #12482
   
   # Rationale for this change
   
   `DictEncoder` uses a dedup table to find the dictionary key of each value. 
Today, `Interner::new`
   allocates this table for 4096 keys when the column writer is created. Each 
table uses 73,744 bytes
   with hashbrown 0.17.
   
   `ArrowWriter` creates the writers for all columns of a row group at the same 
time. Thus, a wide
   schema pays 72 KiB for each dictionary column before it writes a value. A 
column that gets no value
   in the row group never uses its table.
   
   #2273 added the 4096-key size to prevent resizes on dense columns. This PR 
keeps that size. It only
   moves the allocation to the first value.
   
   # What changes are included in this PR?
   
   - `Interner::new` creates an empty table. An empty table does not allocate 
memory.
   - The new crate-private method `Interner::reserve_if_empty` reserves 4096 
keys if the table has no
     capacity.
   - `DictEncoder::put` calls `reserve_if_empty` once for each non-empty batch, 
before its loop. Thus,
     a column that gets values starts with the same table as before.
   - The first commit adds the `wide_sparse_int64` case to the 
`arrow_writer_peak_memory` bench. The
     case writes 1,024 rows of 1,000 nullable Int64 columns. One column in 100 
has values, and the
     other columns are all null.
   
   This PR does not change the Arrow byte-array encoder 
(`arrow_writer/byte_array.rs`). That encoder
   does not use `Interner`, and its table already starts empty.
   
   The output does not change. Dictionary keys follow the first-seen order of 
the values. The table
   capacity does not change this order.
   
   I also tried a simpler version: an empty table that grows, with no reserve. 
It saves the same
   memory and has the same speed on the timing benches below. But these benches 
write one large
   column chunk for each column, so they do not show the cost of resizes on 
small chunks. I kept the
   reserve, so a column with values gets the same table as before.
   
   # Are these changes tested?
   
   Yes.
   
   - 
`dict_encoder::tests::test_estimated_memory_size_includes_interner_dedup_table` 
(changed): A new
     encoder reports 0 bytes. After the first value, the size includes the 
4096-key table. A later
     batch does not resize the table.
   - `dict_encoder::tests::test_dictionary_and_indices_follow_first_seen_order` 
(new): The test writes
     40,000 values with 10,000 distinct values, so the table resizes. The 
indices and the dictionary
     page agree with an independent first-seen calculation.
   - `util::interner::tests::keys_do_not_depend_on_dedup_capacity` (new): A 
table that grows from empty
     and a reserved table give the same keys for 20,000 values.
   
   The existing tests pass with no changes.
   
   **Memory:** `cargo bench -p parquet --bench arrow_writer_peak_memory -- 
wide_sparse_int64`
   
   | Writer properties | `peak_memory` main | `peak_memory` this PR | 
`allocated_bytes` main | `allocated_bytes` this PR |
   | --- | --- | --- | --- | --- |
   | `default` | 73.62 MiB | 5.06 MiB (-93.1%) | 80.89 MiB | 11.27 MiB (-86.1%) 
|
   | `parquet_2` | 75.44 MiB | 5.82 MiB (-92.3%) | 82.72 MiB | 13.09 MiB 
(-84.2%) |
   | `cdc` | 73.68 MiB | 5.11 MiB (-93.1%) | 81.33 MiB | 11.71 MiB (-85.6%) |
   
   `allocated_bytes` decreases by 69.62 MiB. This is 990 × 73,744 bytes: one 
table for each all-null
   column. The 10 columns with values still reserve their tables.
   
   **Speed:** `cargo bench -p parquet --bench arrow_writer -- 
'^primitive(_non_null)?/default$'` and
   `'^ndv/(int32|fsb16)_low_cardinality/'`
   
   #2272 added the 4096-key size for the dense `primitive` cases. I ran these 
cases on one idle x86_64
   Linux host. Each round ran main, this PR and the no-reserve version. The 
table shows the change
   against main in each round.
   
   | Case | This PR | No reserve |
   | --- | --- | --- |
   | `primitive/default` | +1.3%, +3.1%, +1.2% | +1.3%, -0.6%, +2.7% |
   | `primitive_non_null/default` | -1.0%, +0.8%, -0.2% | +2.1%, -1.7%, +1.3% |
   | `ndv/int32_low_cardinality/ndv_on` | +0.3%, +0.7% | -1.6%, -0.1% |
   | `ndv/fsb16_low_cardinality/ndv_on` | -1.3%, -1.6% | -0.2%, -0.3% |
   
   # Are there any significant user-facing changes?
   
   No. The output does not change. The writer uses less memory for columns that 
have no values yet,
   so `ArrowWriter::memory_size` reports a lower value for them.
   
   # AI usage disclosure
   
   An AI coding assistant helped to write the change, the tests, the bench case 
and this description.
   I reviewed all of it and ran it.
   


-- 
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]

Reply via email to