rdblue commented on code in PR #16747: URL: https://github.com/apache/iceberg/pull/16747#discussion_r4010600018
########## core/src/main/java/org/apache/iceberg/mumbling/BitPacking.java: ########## @@ -0,0 +1,769 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ +package org.apache.iceberg.mumbling; + +import java.nio.ByteBuffer; +import org.apache.iceberg.util.ByteBuffers; + +/** + * Bit packing and unpacking for values of 1–7 bits. + * + * <p>The least-significant bits of each value are packed with the first value occupying the most + * significant bits of the output. Output is padded to the nearest byte with 0s. For example, + * packing values [0b11, 0b10, 0b01] with width 2 produces 0b11100100. + */ +class BitPacking { + + private BitPacking() {} + + /** + * Packs {@code width} least-significant bits of {@code count} values into a data buffer. + * + * <p>Output is padded to the nearest byte with 0s. + * + * <p>Values are written to the buffer's underlying storage, but the buffer's position and limit + * are not modified. + * + * @param width number of bits of each value to pack + * @param values array containing source values to pack + * @param valueOffset starting index of values to pack + * @param data an output {@link ByteBuffer} + * @param dataOffset starting index for output in the data buffer + * @param count the number of values to pack + * @return the number of bytes written to the data buffer + */ + static int packBits( Review Comment: I did it this way because I think it's easier to read and verify, and we only need to do that once. For example, we can break down the 3-bit pack method, starting with `packWord3`: ```java private static int packWord3(int a, int b, int c, int d, int e, int f, int g, int h) { return ((a & 0b111) << 21) | ((b & 0b111) << 18) | ((c & 0b111) << 15) | ((d & 0b111) << 12) | ((e & 0b111) << 9) | ((f & 0b111) << 6) | ((g & 0b111) << 3) | (h & 0b111); } ``` This method packs the least-significant 3 bits of 8 values into 3 bytes. It's easy to see the order of values and the offset each one uses. I think that's easier to read than a loop that keeps adding 3 and uses variables like `mask = (0b1 << width) - 1`. Then for the array pack method: ```java int fullGroups = count / 8; // . . . int remaining = count % 8; ``` This is common to each pack method and breaks the values into groups of 8 for use with the `packWord` method. ```java for (int group = 0; group < fullGroups; group += 1) { int groupOffset = valueOffset + 8 * group; int outputOffset = dataOffset + 3 * group; int word = packWord3(values[groupOffset], ..., values[groupOffset + 7]); ByteBuffers.writeByte(data, word >>> 16, outputOffset); ByteBuffers.writeByte(data, word >>> 8, outputOffset + 1); ByteBuffers.writeByte(data, word, outputOffset + 2); } ``` This packs all of the full groups of 8 values. The hardest thing is the offset tracking, which skips by 8 values for each group and 3 bytes for each group's output in the data buffer. The values are all passed to `packWord3` that produces an `int` with the 3 packed bytes. This then writes the packed bytes into the data buffer. I think this makes it easy to see the bits that are used from the int. ```java if (remaining > 0) { int groupOffset = valueOffset + 8 * fullGroups; int outputOffset = dataOffset + 3 * fullGroups; int word = packWord3( values[groupOffset], remaining > 1 ? values[groupOffset + 1] : 0, remaining > 2 ? values[groupOffset + 2] : 0, remaining > 3 ? values[groupOffset + 3] : 0, remaining > 4 ? values[groupOffset + 4] : 0, remaining > 5 ? values[groupOffset + 5] : 0, remaining > 6 ? values[groupOffset + 6] : 0, 0); int byteCount = byteWidth(3 * remaining); for (int k = 0; k < byteCount; k += 1) { ByteBuffers.writeByte(data, word >>> (16 - 8 * k), outputOffset + k); } } ``` This handles the remaining values of the last partial group. Each value is replaced with 0 when packing to a word, if it is not present in the data. After packing, the bytes of the value are written to the data buffer. This is a little tricky because it uses a loop to write the first 1 or 2 bytes if the third byte isn't used. However, since it mirrors the `writeByte` calls above, I think it's more understandable. `(16 - 8 * k)` is the shift; first 16, then 8, then 0 just like the first section. ```java return byteWidth(3 * count); ``` Finally, this returns the actual bytes written. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
