Skip to content

Commit 7dc064c

Browse files
committed
Performance optimizations
Refactor jar lifecycle listener invocation to reduce redundant plugin lookups: - Fetch listeners once and pass them to beforeJarWrite/afterJarWrite instead of looking them up multiple times - Create context map only when listeners exist (lazy initialization) - Reuse DiffPluginImpl instance field instead of creating new instances - Move timestamp fetch earlier in RebuildTriggerPolicy to simplify file existence check Signed-off-by: Christoph Rueger <chrisrueger@gmail.com>
1 parent e195827 commit 7dc064c

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

biz.aQute.bndlib/src/aQute/bnd/build/Project.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,14 +2105,20 @@ private File saveBuildWithoutClose(Jar jar) throws Exception {
21052105
File outputFile = getOutputFile(jar.getName(), jar.getVersion());
21062106

21072107
// Notify BEFORE listeners
2108-
Map<String, Object> context = new HashMap<String, Object>();
2109-
beforeJarWrite(jar, outputFile, context);
2108+
List<JarLifecycleListener> listeners = getPlugins(JarLifecycleListener.class);
2109+
Map<String, Object> context = null;
2110+
if (!listeners.isEmpty()) {
2111+
context = new HashMap<String, Object>();
2112+
beforeJarWrite(jar, outputFile, context, listeners);
2113+
}
21102114

21112115
reportNewer(outputFile.lastModified(), jar);
21122116
File logicalFile = write(jar::write, outputFile);
21132117

21142118
// Notify AFTER listeners
2115-
afterJarWrite(this, outputFile, jar, context);
2119+
if (!listeners.isEmpty()) {
2120+
afterJarWrite(this, outputFile, jar, context, listeners);
2121+
}
21162122

21172123
logger.debug("{} ({}) {}", jar.getName(), outputFile.getName(), jar.getResources()
21182124
.size());
@@ -2143,8 +2149,8 @@ private File saveBuildWithoutClose(Jar jar) throws Exception {
21432149
return logicalFile;
21442150
}
21452151

2146-
private void beforeJarWrite(Jar jar, File outputFile, Map<String, Object> context) throws Exception {
2147-
List<JarLifecycleListener> listeners = getPlugins(JarLifecycleListener.class);
2152+
private void beforeJarWrite(Jar jar, File outputFile, Map<String, Object> context,
2153+
List<JarLifecycleListener> listeners) throws Exception {
21482154

21492155
for (JarLifecycleListener listener : listeners) {
21502156
try {
@@ -2155,9 +2161,10 @@ private void beforeJarWrite(Jar jar, File outputFile, Map<String, Object> contex
21552161
}
21562162
}
21572163

2158-
private void afterJarWrite(Project project, File outputFile, Jar jar, Map<String, Object> context)
2164+
private void afterJarWrite(Project project, File outputFile, Jar jar, Map<String, Object> context,
2165+
List<JarLifecycleListener> listeners)
21592166
throws Exception {
2160-
List<JarLifecycleListener> listeners = getPlugins(JarLifecycleListener.class);
2167+
21612168
for (JarLifecycleListener listener : listeners) {
21622169
try {
21632170
listener.afterWrite(project, outputFile, jar, context);

bndtools.core/src/bndtools/central/RebuildTriggerPolicy.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public class RebuildTriggerPolicy {
8484

8585
private static final ILogger logger = Logger.getLogger(RebuildTriggerPolicy.class);
8686
private final String rebuildTriggerPolicyKey;
87+
private final DiffPluginImpl differ = new DiffPluginImpl();
8788

8889
/**
8990
* Result of evaluating the build change policy.
@@ -167,11 +168,13 @@ RebuildTriggerPolicyResult doRebuildTriggerPolicy(Jar jar, File outputFile) {
167168
String newContentDigestHex = calcContentDigest(jar);
168169

169170
// Fast path: no existing output means nothing to preserve.
170-
if (!outputFile.isFile()) {
171+
long existingTimestamp = outputFile.lastModified();
172+
173+
if (existingTimestamp == 0) {
174+
// existingTimestamp==0 means file does not exist
171175
return new RebuildTriggerPolicyResult(0, contentDigestFile, newContentDigestHex, null, null);
172176
}
173177

174-
long existingTimestamp = outputFile.lastModified();
175178

176179
// Check 1: byte-identical content -> preserve immediately.
177180
if (digestMatches(contentDigestFile, newContentDigestHex, outputFile, "stored content digest")) {
@@ -241,7 +244,7 @@ private String calcApiDigest(Jar jar) {
241244
if (exportPackage == null || exportPackage.isEmpty()) {
242245
return null;
243246
}
244-
Tree tree = new DiffPluginImpl().tree(jar);
247+
Tree tree = differ.tree(jar);
245248
Tree apiTree = tree.get("<api>");
246249
if (apiTree == null) {
247250
return null;

0 commit comments

Comments
 (0)