Skip to content

Commit 19c6bcf

Browse files
authored
HADOOP-19875. Further improve CrcComposer performance. (#8497)
1 parent 62729b0 commit 19c6bcf

4 files changed

Lines changed: 19 additions & 19 deletions

File tree

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/CrcComposer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.io.ByteArrayOutputStream;
2727
import java.io.DataInputStream;
2828
import java.io.IOException;
29-
import java.util.function.ToIntFunction;
29+
import java.util.function.LongToIntFunction;
3030

3131
/**
3232
* Encapsulates logic for composing multiple CRCs into one or more combined CRCs
@@ -40,7 +40,7 @@ public final class CrcComposer {
4040
private static final int CRC_SIZE_BYTES = 4;
4141
private static final Logger LOG = LoggerFactory.getLogger(CrcComposer.class);
4242

43-
private final ToIntFunction<Long> mod;
43+
private final LongToIntFunction mod;
4444
private final int precomputedMonomialForHint;
4545
private final long bytesPerCrcHint;
4646
private final long stripeLength;

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/CrcUtil.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.apache.hadoop.classification.InterfaceStability;
2323

2424
import java.util.Arrays;
25-
import java.util.function.ToIntFunction;
25+
import java.util.function.LongToIntFunction;
2626

2727
/**
2828
* This class provides utilities for working with CRCs.
@@ -39,7 +39,7 @@ public final class CrcUtil {
3939
* @return a * b (mod p),
4040
* where mod p is computed by the given mod function.
4141
*/
42-
static int multiplyMod(int a, int b, ToIntFunction<Long> mod) {
42+
static int multiplyMod(int a, int b, LongToIntFunction mod) {
4343
final long left = ((long)a) << 32;
4444
final long right = ((long)b) << 32;
4545

@@ -98,7 +98,7 @@ private CrcUtil() {
9898
* @param mod mod.
9999
* @return monomial.
100100
*/
101-
public static int getMonomial(long lengthBytes, ToIntFunction<Long> mod) {
101+
public static int getMonomial(long lengthBytes, LongToIntFunction mod) {
102102
if (lengthBytes == 0) {
103103
return MULTIPLICATIVE_IDENTITY;
104104
} else if (lengthBytes < 0) {
@@ -135,7 +135,7 @@ public static int getMonomial(long lengthBytes, ToIntFunction<Long> mod) {
135135
* @return compose with monomial.
136136
*/
137137
public static int composeWithMonomial(
138-
int crcA, int crcB, int monomial, ToIntFunction<Long> mod) {
138+
int crcA, int crcB, int monomial, LongToIntFunction mod) {
139139
return multiplyMod(crcA, monomial, mod) ^ crcB;
140140
}
141141

@@ -148,7 +148,7 @@ public static int composeWithMonomial(
148148
* @param mod mod.
149149
* @return compose result.
150150
*/
151-
public static int compose(int crcA, int crcB, long lengthB, ToIntFunction<Long> mod) {
151+
public static int compose(int crcA, int crcB, long lengthB, LongToIntFunction mod) {
152152
int monomial = getMonomial(lengthB, mod);
153153
return composeWithMonomial(crcA, crcB, monomial, mod);
154154
}

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DataChecksum.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.io.DataOutputStream;
2323
import java.io.IOException;
2424
import java.nio.ByteBuffer;
25-
import java.util.function.ToIntFunction;
25+
import java.util.function.LongToIntFunction;
2626
import java.util.zip.CRC32;
2727
import java.util.zip.CRC32C;
2828
import java.util.zip.Checksum;
@@ -97,7 +97,7 @@ static Checksum newCrc32C() {
9797
* @return the int representation of the polynomial associated with the
9898
* CRC {@code type}, suitable for use with further CRC arithmetic.
9999
*/
100-
static ToIntFunction<Long> getModFunction(Type type) {
100+
static LongToIntFunction getModFunction(Type type) {
101101
switch (type) {
102102
case CRC32:
103103
return PureJavaCrc32::mod;

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestCrcUtil.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import java.util.Objects;
2121
import java.util.Random;
22-
import java.util.function.ToIntFunction;
22+
import java.util.function.LongToIntFunction;
2323
import org.apache.hadoop.test.LambdaTestUtils;
2424
import org.junit.jupiter.api.Test;
2525
import org.junit.jupiter.api.Timeout;
@@ -91,7 +91,7 @@ public void testComposeCrc32CZeroLength() {
9191
*/
9292
private static void doTestComposeCrc(
9393
byte[] data, DataChecksum.Type type, int chunkSize, boolean useMonomial) {
94-
final ToIntFunction<Long> mod = DataChecksum.getModFunction(type);
94+
final LongToIntFunction mod = DataChecksum.getModFunction(type);
9595

9696
// Get full end-to-end CRC in a single shot first.
9797
DataChecksum checksum = DataChecksum.newDataChecksum(
@@ -142,7 +142,7 @@ private static void doTestComposeCrcZerolength(DataChecksum.Type type) {
142142
// Without loss of generality, we can pick any integer as our fake crcA
143143
// even if we don't happen to know the preimage.
144144
int crcA = 0xCAFEBEEF;
145-
final ToIntFunction<Long> mod = DataChecksum.getModFunction(type);
145+
final LongToIntFunction mod = DataChecksum.getModFunction(type);
146146
DataChecksum checksum = DataChecksum.newDataChecksum(
147147
type, Integer.MAX_VALUE);
148148
Objects.requireNonNull(checksum, "checksum");
@@ -230,7 +230,7 @@ public void testMultiplyMod() {
230230
private static long[] runTestMultiplyMod(int n, DataChecksum.Type type) {
231231
System.out.printf("Run %s with %d computations%n", type, n);
232232
final int polynomial = getCrcPolynomialForType(type);
233-
final ToIntFunction<Long> mod = DataChecksum.getModFunction(type);
233+
final LongToIntFunction mod = DataChecksum.getModFunction(type);
234234

235235
final int[] p = new int[n];
236236
final int[] q = new int[n];
@@ -256,8 +256,8 @@ private static long[] runTestMultiplyMod(int n, DataChecksum.Type type) {
256256
}
257257
times[1] = System.currentTimeMillis() - t1;
258258
final double ops1 = n * 1000.0 / times[1];
259-
System.out.printf("multiplyCrc32 : %.3fs (%.2f ops)%n", times[1] / 1000.0, ops1);
260-
System.out.printf("multiplyCrc32 is %.2f%% faster%n", (ops1 - ops0) * 100.0 / ops0);
259+
System.out.printf("tableMultiply : %.3fs (%.2f ops)%n", times[1] / 1000.0, ops1);
260+
System.out.printf("tableMultiply ops is %.2f%% faster%n", (ops1 - ops0) * 100.0 / ops0);
261261

262262
for (int i = 0; i < n; i++) {
263263
if (expected[i] != computed[i]) {
@@ -354,11 +354,11 @@ public static void main(String[] args) throws Exception {
354354
}
355355

356356
System.out.printf("%nResult) %d x %d computations:%n", m, n);
357-
final double ops0 = n * 1000.0 / times[0];
357+
final double ops0 = m * n * 1000.0 / times[0];
358358
System.out.printf("galoisFieldMultiply: %.3fs (%.2f ops)%n", times[0] / 1000.0, ops0);
359-
final double ops1 = n * 1000.0 / times[1];
360-
System.out.printf("multiplyCrc32 : %.3fs (%.2f ops)%n", times[1] / 1000.0, ops1);
361-
System.out.printf("multiplyCrc32 is %.2f%% faster%n", (ops1 - ops0) * 100.0 / ops0);
359+
final double ops1 = m * n * 1000.0 / times[1];
360+
System.out.printf("tableMultiply : %.3fs (%.2f ops)%n", times[1] / 1000.0, ops1);
361+
System.out.printf("tableMultiply ops is %.2f%% faster%n", (ops1 - ops0) * 100.0 / ops0);
362362
}
363363
}
364364
}

0 commit comments

Comments
 (0)