Skip to content

Commit

Permalink
Use CodeTimer.using() instead of creating new CodeTimers everywhere
Browse files Browse the repository at this point in the history
All affected components followed the same basic pattern, with some variations:
1. Create the timer.
2. Enable / disable the timer, usually based on AppState.
3. Set a minimum threshold.
4. At the end, report to the profiling frame.

Now (1), (2) and (4) are done automatically and consistently in `CodeTimer.using()`, with (2) and (3) also doable by the
caller to customize the timer as needed.
  • Loading branch information
kwvanderlinde committed Dec 3, 2023
1 parent ca9753e commit 07f5866
Show file tree
Hide file tree
Showing 9 changed files with 770 additions and 780 deletions.
222 changes: 105 additions & 117 deletions src/main/java/net/rptools/lib/io/PackedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
import net.rptools.lib.CodeTimer;
import net.rptools.lib.FileUtil;
import net.rptools.lib.ModelVersionManager;
import net.rptools.maptool.client.AppState;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.model.Asset;
import net.rptools.maptool.model.AssetManager;
import net.rptools.maptool.model.GUID;
Expand Down Expand Up @@ -286,128 +284,118 @@ public boolean isDirty() {
}

public void save() throws IOException {
CodeTimer saveTimer;

if (!dirty) {
return;
}
saveTimer = new CodeTimer("PackedFile.save");
saveTimer.setEnabled(AppState.isCollectProfilingData());

// Create the new file
File newFile = new File(tmpDir, new GUID() + ".pak");
ZipOutputStream zout =
new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(newFile)));
zout.setLevel(Deflater.BEST_COMPRESSION); // fast compression
try {
saveTimer.start(CONTENT_FILE);
if (hasFile(CONTENT_FILE)) {
saveEntry(zout, CONTENT_FILE);
}
saveTimer.stop(CONTENT_FILE);

saveTimer.start(PROPERTY_FILE);
if (getPropertyMap().isEmpty()) {
removeFile(PROPERTY_FILE);
} else {
zout.putNextEntry(new ZipEntry(PROPERTY_FILE));
xstream.toXML(getPropertyMap(), zout);
zout.closeEntry();
}
saveTimer.stop(PROPERTY_FILE);

// Now put each file
saveTimer.start("addFiles");
addedFileSet.remove(CONTENT_FILE);
for (String path : addedFileSet) {
saveEntry(zout, path);
}
saveTimer.stop("addFiles");

// Copy the rest of the zip entries over
saveTimer.start("copyFiles");
if (file.exists()) {
Enumeration<? extends ZipEntry> entries = zFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()
&& !addedFileSet.contains(entry.getName())
&& !removedFileSet.contains(entry.getName())
&& !CONTENT_FILE.equals(entry.getName())
&& !PROPERTY_FILE.equals(entry.getName())) {
// if (entry.getName().endsWith(".png") ||
// entry.getName().endsWith(".gif") ||
// entry.getName().endsWith(".jpeg"))
// zout.setLevel(Deflater.NO_COMPRESSION); // none needed for images as they are already
// compressed
// else
// zout.setLevel(Deflater.BEST_COMPRESSION); // fast compression
zout.putNextEntry(entry);
try (InputStream is = getFileAsInputStream(entry.getName())) {
// When copying, always use an InputStream
IOUtils.copy(is, zout);
CodeTimer.using(
"PackedFile.save",
saveTimer -> {
// Create the new file
File newFile = new File(tmpDir, new GUID() + ".pak");
ZipOutputStream zout =
new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(newFile)));
zout.setLevel(Deflater.BEST_COMPRESSION); // fast compression

try {
saveTimer.start(CONTENT_FILE);
if (hasFile(CONTENT_FILE)) {
saveEntry(zout, CONTENT_FILE);
}
zout.closeEntry();
} else if (entry.isDirectory()) {
zout.putNextEntry(entry);
zout.closeEntry();
}
}
}
try {
if (zFile != null) zFile.close();
} catch (IOException e) {
// ignore close exception
}
zFile = null;
saveTimer.stop("copyFiles");

saveTimer.start("close");
IOUtils.closeQuietly(zout);
zout = null;
saveTimer.stop("close");

// Backup the original
saveTimer.start("backup");
File backupFile = new File(tmpDir, new GUID() + ".mv");
if (file.exists()) {
backupFile.delete(); // Always delete the old backup file first; renameTo() is very
// platform-dependent
if (!file.renameTo(backupFile)) {
saveTimer.start("backup file");
FileUtil.copyFile(file, backupFile);
file.delete();
saveTimer.stop("backup file");
}
}
saveTimer.stop("backup");

saveTimer.start("finalize");
// Finalize
if (!newFile.renameTo(file)) {
saveTimer.start("backup newFile");
FileUtil.copyFile(newFile, file);
saveTimer.stop("backup newFile");
}
if (backupFile.exists()) backupFile.delete();
saveTimer.stop("finalize");
saveTimer.stop(CONTENT_FILE);

dirty = false;
} finally {
saveTimer.start("cleanup");
try {
if (zFile != null) zFile.close();
} catch (IOException e) {
// ignore close exception
}
if (newFile.exists()) newFile.delete();
IOUtils.closeQuietly(zout);
saveTimer.stop("cleanup");
saveTimer.start(PROPERTY_FILE);
if (getPropertyMap().isEmpty()) {
removeFile(PROPERTY_FILE);
} else {
zout.putNextEntry(new ZipEntry(PROPERTY_FILE));
xstream.toXML(getPropertyMap(), zout);
zout.closeEntry();
}
saveTimer.stop(PROPERTY_FILE);

if (saveTimer.isEnabled()) {
MapTool.getProfilingNoteFrame().addText(saveTimer.toString());
}
}
// Now put each file
saveTimer.start("addFiles");
addedFileSet.remove(CONTENT_FILE);
for (String path : addedFileSet) {
saveEntry(zout, path);
}
saveTimer.stop("addFiles");

// Copy the rest of the zip entries over
saveTimer.start("copyFiles");
if (file.exists()) {
Enumeration<? extends ZipEntry> entries = zFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()
&& !addedFileSet.contains(entry.getName())
&& !removedFileSet.contains(entry.getName())
&& !CONTENT_FILE.equals(entry.getName())
&& !PROPERTY_FILE.equals(entry.getName())) {
zout.putNextEntry(entry);
try (InputStream is = getFileAsInputStream(entry.getName())) {
// When copying, always use an InputStream
IOUtils.copy(is, zout);
}
zout.closeEntry();
} else if (entry.isDirectory()) {
zout.putNextEntry(entry);
zout.closeEntry();
}
}
}
try {
if (zFile != null) zFile.close();
} catch (IOException e) {
// ignore close exception
}
zFile = null;
saveTimer.stop("copyFiles");

saveTimer.start("close");
IOUtils.closeQuietly(zout);
zout = null;
saveTimer.stop("close");

// Backup the original
saveTimer.start("backup");
File backupFile = new File(tmpDir, new GUID() + ".mv");
if (file.exists()) {
backupFile.delete(); // Always delete the old backup file first; renameTo() is very
// platform-dependent
if (!file.renameTo(backupFile)) {
saveTimer.start("backup file");
FileUtil.copyFile(file, backupFile);
file.delete();
saveTimer.stop("backup file");
}
}
saveTimer.stop("backup");

saveTimer.start("finalize");
// Finalize
if (!newFile.renameTo(file)) {
saveTimer.start("backup newFile");
FileUtil.copyFile(newFile, file);
saveTimer.stop("backup newFile");
}
if (backupFile.exists()) backupFile.delete();
saveTimer.stop("finalize");

dirty = false;
} finally {
saveTimer.start("cleanup");
try {
if (zFile != null) zFile.close();
} catch (IOException e) {
// ignore close exception
}
if (newFile.exists()) newFile.delete();
IOUtils.closeQuietly(zout);
saveTimer.stop("cleanup");
}
});
}

private void saveEntry(ZipOutputStream zout, String path) throws IOException {
Expand Down
Loading

0 comments on commit 07f5866

Please sign in to comment.