-
Notifications
You must be signed in to change notification settings - Fork 864
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancements in CRC32 and CRC32C Checksum implementations
- Loading branch information
Showing
23 changed files
with
1,002 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "feature", | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "", | ||
"description": "The SDK now defaults to Java built-in CRC32 and CRC32C(if it's Java 9+) implementations, resulting in improved performance." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
core/checksums/src/main/java/software/amazon/awssdk/checksums/internal/ConstructorCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.checksums.internal; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.lang.reflect.Constructor; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.WeakHashMap; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.utils.ClassLoaderHelper; | ||
import software.amazon.awssdk.utils.Logger; | ||
|
||
/** | ||
* A cache that stores classes and their constructors by class name and class loader. | ||
* <p> | ||
* This cache uses weak references to both class loaders and classes, allowing them to be garbage collected | ||
* when no longer needed. It provides methods to retrieve the zero-argument constructor for a class, | ||
* based on the current thread's context class loader or the system class loader. | ||
* <p> | ||
* If a class or its zero-argument constructor cannot be found, an empty result is returned. | ||
*/ | ||
@SdkInternalApi | ||
public final class ConstructorCache { | ||
private static final Logger log = Logger.loggerFor(ConstructorCache.class); | ||
|
||
/** | ||
* Cache storing classes by class name and class loader. | ||
* Uses weak references to allow garbage collection when not needed. | ||
*/ | ||
private final Map<String, Map<ClassLoader, Optional<WeakReference<Class<?>>>>> classesByClassName = | ||
new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* Retrieve the class for the given class name from the context or system class loader. | ||
* Returns an empty result if the class is not found. | ||
*/ | ||
private Optional<Class<?>> getClass(String className) { | ||
Map<ClassLoader, Optional<WeakReference<Class<?>>>> classesByClassLoader = | ||
classesByClassName.computeIfAbsent(className, k -> Collections.synchronizedMap(new WeakHashMap<>())); | ||
|
||
ClassLoader classLoader = ClassLoaderHelper.contextClassLoader(); | ||
Optional<WeakReference<Class<?>>> classRef = classesByClassLoader.computeIfAbsent(classLoader, k -> { | ||
try { | ||
Class<?> clazz = classLoader.loadClass(className); | ||
return Optional.of(new WeakReference<>(clazz)); | ||
} catch (ClassNotFoundException e) { | ||
return Optional.empty(); | ||
} | ||
}); | ||
return classRef.map(WeakReference::get); | ||
} | ||
|
||
/** | ||
* Retrieve the zero-argument constructor for the given class name. | ||
* Returns an empty result if no such constructor is found. | ||
*/ | ||
public Optional<Constructor<?>> getConstructor(String className) { | ||
return getClass(className).flatMap(clazz -> { | ||
try { | ||
return Optional.of(clazz.getConstructor()); | ||
} catch (NoSuchMethodException e) { | ||
log.debug(() -> "Classloader contains " + className + ", but without a zero-arg constructor.", e); | ||
return Optional.empty(); | ||
} | ||
}); | ||
} | ||
} |
78 changes: 0 additions & 78 deletions
78
core/checksums/src/main/java/software/amazon/awssdk/checksums/internal/Crc32CChecksum.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
core/checksums/src/main/java/software/amazon/awssdk/checksums/internal/Crc32cProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.checksums.internal; | ||
|
||
import java.util.zip.Checksum; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.checksums.SdkChecksum; | ||
import software.amazon.awssdk.crt.checksums.CRC32C; | ||
|
||
/** | ||
* Utility class to provide different implementations of CRC32C checksum. This class supports the use of: 1. Java-based CRC32C | ||
* (Java 9+ when available) 2. CRT-based CRC32C (when available) 3. SDK-based CRC32C (as fallback) | ||
*/ | ||
@SdkInternalApi | ||
public final class Crc32cProvider { | ||
|
||
|
||
// Class paths for different CRC32C implementations | ||
private static final String CRT_CRC32C_CLASS_PATH = "software.amazon.awssdk.crt.checksums.CRC32C"; | ||
private static final String JAVA_CRC32C_CLASS_PATH = "java.util.zip.CRC32C"; | ||
private static final ConstructorCache CONSTRUCTOR_CACHE = new ConstructorCache(); | ||
|
||
// Private constructor to prevent instantiation | ||
private Crc32cProvider() { | ||
} | ||
|
||
/** | ||
* Creates an instance of the SDK-based CRC32C checksum as a fallback. | ||
* | ||
* @return An SdkChecksum instance. | ||
*/ | ||
static SdkChecksum createSdkBasedCrc32C() { | ||
SdkCrc32CChecksum sdkChecksum = SdkCrc32CChecksum.create(); | ||
return new CrcCloneOnMarkChecksum(sdkChecksum, checksumToClone -> ((SdkCrc32CChecksum) checksumToClone).clone()); | ||
} | ||
|
||
/** | ||
* Tries to create a Java 9-based CRC32C checksum. | ||
* If it's not available, it tries to create a CRT-based checksum. | ||
* If both are not available, it falls back to an SDK-based CRC32C checksum. | ||
* | ||
* @return An instance of {@link SdkChecksum}, based on the first available option. | ||
*/ | ||
public static SdkChecksum create() { | ||
SdkChecksum checksum = createJavaCrc32C(); | ||
if (checksum == null) { | ||
checksum = createCrtCrc32C(); | ||
} | ||
return checksum != null ? checksum : createSdkBasedCrc32C(); | ||
} | ||
|
||
static SdkChecksum createCrtCrc32C() { | ||
return CONSTRUCTOR_CACHE.getConstructor(CRT_CRC32C_CLASS_PATH).map(constructor -> { | ||
try { | ||
return new CrcCloneOnMarkChecksum((Checksum) constructor.newInstance(), checksumToClone -> | ||
(Checksum) ((CRC32C) checksumToClone).clone()); | ||
} catch (ClassCastException | ReflectiveOperationException e) { | ||
throw new IllegalStateException("Failed to instantiate " + JAVA_CRC32C_CLASS_PATH, e); | ||
} | ||
}).orElse(null); | ||
} | ||
|
||
static SdkChecksum createJavaCrc32C() { | ||
return CONSTRUCTOR_CACHE.getConstructor(JAVA_CRC32C_CLASS_PATH).map(constructor -> { | ||
try { | ||
return new CrcCombineOnMarkChecksum((Checksum) constructor.newInstance(), SdkCrc32CChecksum::combine); | ||
} catch (ClassCastException | ReflectiveOperationException e) { | ||
throw new IllegalStateException("Failed to instantiate " + JAVA_CRC32C_CLASS_PATH, e); | ||
} | ||
}).orElse(null); | ||
} | ||
|
||
} |
Oops, something went wrong.