Skip to content

Commit 13bf46b

Browse files
lesnik2umichaelsembwever
authored andcommitted
CNDB-16175: CNDB-15990: Avoid varargs allocations in atomicMoveWithFallback (#2124)
riptano/cndb#15990 - What: Eliminates unnecessary array allocations in PathUtils.atomicMoveWithFallback() by pre-allocating CopyOption[] arrays as static constants. - Why: JFR profiling showed this method was a memory allocation hotspot during RemoteFileCache preloading. Each call to Files.move() with varargs parameters created new arrays, causing excessive GC pressure.
1 parent 4d371dd commit 13bf46b

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/java/org/apache/cassandra/io/util/PathUtils.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,31 @@ public final class PathUtils
9999
private static final Logger logger = LoggerFactory.getLogger(PathUtils.class);
100100
private static final NoSpamLogger nospam1m = NoSpamLogger.getLogger(logger, 1, TimeUnit.MINUTES);
101101

102-
private static Consumer<Path> onDeletion = path -> {};
103102
private static final boolean USE_NIX_RECURSIVE_DELETE = CassandraRelevantProperties.USE_NIX_RECURSIVE_DELETE.getBoolean();
104103

104+
private static volatile boolean DAEMON_SETUP_COMPLETED = false;
105+
106+
private static final CopyOption[] ATOMIC_MOVE_OPTIONS = {
107+
StandardCopyOption.REPLACE_EXISTING,
108+
StandardCopyOption.ATOMIC_MOVE
109+
};
110+
111+
private static final CopyOption[] REPLACE_EXISTING_OPTIONS = {
112+
StandardCopyOption.REPLACE_EXISTING
113+
};
114+
115+
private static Consumer<Path> onDeletion = path -> {
116+
if (DAEMON_SETUP_COMPLETED)
117+
setDeletionListener(ignore -> {});
118+
else if (logger.isTraceEnabled())
119+
logger.trace("Deleting file during startup: {}", path);
120+
};
121+
122+
public static void daemonSetupCompleted()
123+
{
124+
DAEMON_SETUP_COMPLETED = true;
125+
}
126+
105127
public static FileChannel newReadChannel(Path path) throws NoSuchFileException
106128
{
107129
return newFileChannel(path, READ_OPTIONS);
@@ -556,12 +578,12 @@ private static void atomicMoveWithFallback(Path from, Path to) throws IOExceptio
556578
{
557579
try
558580
{
559-
Files.move(from, to, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
581+
Files.move(from, to, ATOMIC_MOVE_OPTIONS);
560582
}
561583
catch (AtomicMoveNotSupportedException e)
562584
{
563585
logger.trace("Could not do an atomic move", e);
564-
Files.move(from, to, StandardCopyOption.REPLACE_EXISTING);
586+
Files.move(from, to, REPLACE_EXISTING_OPTIONS);
565587
}
566588
}
567589

0 commit comments

Comments
 (0)