Browse Source

Optimize Hibernate native footprint by making ByteBuddy unreachable

As a workaround before a proper solution the Spring team is trying to
find with the Hibernate team in a future version of Hibernate, this
commit introduces 2 GraalVM substitutions that should allow to remove
ByteBuddy reachability with Hibernate 6.3.

Closes gh-29549
pull/31158/head
Sébastien Deleuze 1 year ago
parent
commit
63a20be502
  1. 41
      spring-orm/src/main/java/org/springframework/orm/jpa/vendor/Target_BytecodeProvider.java
  2. 49
      spring-orm/src/main/java/org/springframework/orm/jpa/vendor/Target_BytecodeProviderInitiator.java
  3. 1
      src/checkstyle/checkstyle-suppressions.xml

41
spring-orm/src/main/java/org/springframework/orm/jpa/vendor/Target_BytecodeProvider.java vendored

@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.orm.jpa.vendor;
import java.util.Map;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import org.hibernate.bytecode.spi.ReflectionOptimizer;
import org.hibernate.property.access.spi.PropertyAccess;
/**
* Hibernate 6.3+ substitution designed to leniently return {@code null}, as authorized by the API, to avoid throwing an
* {@code HibernateException}.
* TODO Ask Hibernate team to fix this as it looks like a bug
*
* @author Sebastien Deleuze
* @since 6.1
*/
@TargetClass(className = "org.hibernate.bytecode.internal.none.BytecodeProviderImpl")
final class Target_BytecodeProvider {
@Substitute
public ReflectionOptimizer getReflectionOptimizer(Class<?> clazz, Map<String, PropertyAccess> propertyAccessMap) {
return null;
}
}

49
spring-orm/src/main/java/org/springframework/orm/jpa/vendor/Target_BytecodeProviderInitiator.java vendored

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.orm.jpa.vendor;
import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import org.hibernate.bytecode.spi.BytecodeProvider;
import static com.oracle.svm.core.annotate.RecomputeFieldValue.Kind;
/**
* Hibernate substitution designed to prevent ByteBuddy reachability on native, and to enforce the
* usage of {@code org.hibernate.bytecode.internal.none.BytecodeProviderImpl} with Hibernate 6.3+.
* TODO Collaborate with Hibernate team on a substitution-less alternative that does not require a custom list of StandardServiceInitiator
*
* @author Sebastien Deleuze
* @since 6.1
*/
@TargetClass(className = "org.hibernate.bytecode.internal.BytecodeProviderInitiator")
final class Target_BytecodeProviderInitiator {
@Alias
public static String BYTECODE_PROVIDER_NAME_NONE;
@Alias
@RecomputeFieldValue(kind = Kind.FromAlias)
public static String BYTECODE_PROVIDER_NAME_DEFAULT = BYTECODE_PROVIDER_NAME_NONE;
@Substitute
public static BytecodeProvider buildBytecodeProvider(String providerName) {
return new org.hibernate.bytecode.internal.none.BytecodeProviderImpl();
}
}

1
src/checkstyle/checkstyle-suppressions.xml

@ -84,6 +84,7 @@ @@ -84,6 +84,7 @@
<!-- spring-orm -->
<suppress files="jpa[\\/]vendor[\\/]Database" checks="JavadocVariable|JavadocStyle"/>
<suppress files="Target_BytecodeProviderInitiator" checks="HideUtilityClassConstructorCheck"/>
<!-- spring-tx -->
<suppress files="TransactionSystemException" checks="MutableException"/>

Loading…
Cancel
Save