Skip to content

Commit 65c0d6b

Browse files
committed
Revert "Optimize ByteCodeTranslator IO with caching and buffering (#4295)"
This reverts commit 3c3a5fe.
1 parent 3c3a5fe commit 65c0d6b

File tree

3 files changed

+46
-178
lines changed

3 files changed

+46
-178
lines changed

vm/ByteCodeTranslator/src/com/codename1/tools/translator/ByteCodeTranslator.java

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
package com.codename1.tools.translator;
2525

26-
import java.io.BufferedInputStream;
27-
import java.io.BufferedOutputStream;
2826
import java.io.DataInputStream;
2927
import java.io.File;
3028
import java.io.FileFilter;
@@ -35,9 +33,6 @@
3533
import java.io.IOException;
3634
import java.io.InputStream;
3735
import java.io.OutputStream;
38-
import java.nio.file.Files;
39-
import java.nio.file.StandardCopyOption;
40-
import java.nio.file.StandardOpenOption;
4136
import java.util.ArrayList;
4237
import java.util.Arrays;
4338
import java.util.HashSet;
@@ -241,14 +236,14 @@ private static void handleCleanOutput(ByteCodeTranslator b, File[] sources, File
241236
b.execute(sources, srcRoot);
242237

243238
File cn1Globals = new File(srcRoot, "cn1_globals.h");
244-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/cn1_globals.h"), cn1Globals.toPath(), StandardCopyOption.REPLACE_EXISTING);
239+
copy(ByteCodeTranslator.class.getResourceAsStream("/cn1_globals.h"), new FileOutputStream(cn1Globals));
245240
if (System.getProperty("INCLUDE_NPE_CHECKS", "false").equals("true")) {
246241
replaceInFile(cn1Globals, "//#define CN1_INCLUDE_NPE_CHECKS", "#define CN1_INCLUDE_NPE_CHECKS");
247242
}
248243
File xmlvm = new File(srcRoot, "xmlvm.h");
249-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/xmlvm.h"), xmlvm.toPath(), StandardCopyOption.REPLACE_EXISTING);
244+
copy(ByteCodeTranslator.class.getResourceAsStream("/xmlvm.h"), new FileOutputStream(xmlvm));
250245
File nativeMethods = new File(srcRoot, "nativeMethods.m");
251-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/nativeMethods.m"), nativeMethods.toPath(), StandardCopyOption.REPLACE_EXISTING);
246+
copy(ByteCodeTranslator.class.getResourceAsStream("/nativeMethods.m"), new FileOutputStream(nativeMethods));
252247

253248
Parser.writeOutput(srcRoot);
254249

@@ -273,13 +268,13 @@ private static void handleIosOutput(ByteCodeTranslator b, File[] sources, File d
273268
launchImageLaunchimage.mkdirs();
274269
//cleanDir(launchImageLaunchimage);
275270

276-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/LaunchImages.json"), new File(launchImageLaunchimage, "Contents.json").toPath(), StandardCopyOption.REPLACE_EXISTING);
271+
copy(ByteCodeTranslator.class.getResourceAsStream("/LaunchImages.json"), new FileOutputStream(new File(launchImageLaunchimage, "Contents.json")));
277272

278273
File appIconAppiconset = new File(imagesXcassets, "AppIcon.appiconset");
279274
appIconAppiconset.mkdirs();
280275
//cleanDir(appIconAppiconset);
281276

282-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/Icons.json"), new File(appIconAppiconset, "Contents.json").toPath(), StandardCopyOption.REPLACE_EXISTING);
277+
copy(ByteCodeTranslator.class.getResourceAsStream("/Icons.json"), new FileOutputStream(new File(appIconAppiconset, "Contents.json")));
283278

284279

285280
File xcproj = new File(root, appName + ".xcodeproj");
@@ -296,37 +291,37 @@ private static void handleIosOutput(ByteCodeTranslator b, File[] sources, File d
296291
b.execute(sources, srcRoot);
297292

298293
File cn1Globals = new File(srcRoot, "cn1_globals.h");
299-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/cn1_globals.h"), cn1Globals.toPath(), StandardCopyOption.REPLACE_EXISTING);
294+
copy(ByteCodeTranslator.class.getResourceAsStream("/cn1_globals.h"), new FileOutputStream(cn1Globals));
300295
if (System.getProperty("INCLUDE_NPE_CHECKS", "false").equals("true")) {
301296
replaceInFile(cn1Globals, "//#define CN1_INCLUDE_NPE_CHECKS", "#define CN1_INCLUDE_NPE_CHECKS");
302297
}
303298
File cn1GlobalsM = new File(srcRoot, "cn1_globals.m");
304-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/cn1_globals.m"), cn1GlobalsM.toPath(), StandardCopyOption.REPLACE_EXISTING);
299+
copy(ByteCodeTranslator.class.getResourceAsStream("/cn1_globals.m"), new FileOutputStream(cn1GlobalsM));
305300
File nativeMethods = new File(srcRoot, "nativeMethods.m");
306-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/nativeMethods.m"), nativeMethods.toPath(), StandardCopyOption.REPLACE_EXISTING);
301+
copy(ByteCodeTranslator.class.getResourceAsStream("/nativeMethods.m"), new FileOutputStream(nativeMethods));
307302

308303
if (System.getProperty("USE_RPMALLOC", "false").equals("true")) {
309304
File malloc = new File(srcRoot, "malloc.c");
310-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/malloc.c"), malloc.toPath(), StandardCopyOption.REPLACE_EXISTING);
305+
copy(ByteCodeTranslator.class.getResourceAsStream("/malloc.c"), new FileOutputStream(malloc));
311306
File rpmalloc = new File(srcRoot, "rpmalloc.c");
312-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/rpmalloc.c"), rpmalloc.toPath(), StandardCopyOption.REPLACE_EXISTING);
307+
copy(ByteCodeTranslator.class.getResourceAsStream("/rpmalloc.c"), new FileOutputStream(rpmalloc));
313308
File rpmalloch = new File(srcRoot, "rpmalloc.h");
314-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/rpmalloc.h"), rpmalloch.toPath(), StandardCopyOption.REPLACE_EXISTING);
309+
copy(ByteCodeTranslator.class.getResourceAsStream("/rpmalloc.h"), new FileOutputStream(rpmalloch));
315310
}
316311

317312
Parser.writeOutput(srcRoot);
318313

319314
File templateInfoPlist = new File(srcRoot, appName + "-Info.plist");
320-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/template/template/template-Info.plist"), templateInfoPlist.toPath(), StandardCopyOption.REPLACE_EXISTING);
315+
copy(ByteCodeTranslator.class.getResourceAsStream("/template/template/template-Info.plist"), new FileOutputStream(templateInfoPlist));
321316

322317
File templatePch = new File(srcRoot, appName + "-Prefix.pch");
323-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/template/template/template-Prefix.pch"), templatePch.toPath(), StandardCopyOption.REPLACE_EXISTING);
318+
copy(ByteCodeTranslator.class.getResourceAsStream("/template/template/template-Prefix.pch"), new FileOutputStream(templatePch));
324319

325320
File xmlvm = new File(srcRoot, "xmlvm.h");
326-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/xmlvm.h"), xmlvm.toPath(), StandardCopyOption.REPLACE_EXISTING);
321+
copy(ByteCodeTranslator.class.getResourceAsStream("/xmlvm.h"), new FileOutputStream(xmlvm));
327322

328323
File projectWorkspaceData = new File(projectXCworkspace, "contents.xcworkspacedata");
329-
Files.copy(ByteCodeTranslator.class.getResourceAsStream("/template/template.xcodeproj/project.xcworkspace/contents.xcworkspacedata"), projectWorkspaceData.toPath(), StandardCopyOption.REPLACE_EXISTING);
324+
copy(ByteCodeTranslator.class.getResourceAsStream("/template/template.xcodeproj/project.xcworkspace/contents.xcworkspacedata"), new FileOutputStream(projectWorkspaceData));
330325
replaceInFile(projectWorkspaceData, "KitchenSink", appName);
331326

332327

@@ -625,8 +620,11 @@ private static String getFileType(String s) {
625620
//
626621
private static StringBuilder readFileAsStringBuilder(File sourceFile) throws IOException
627622
{
628-
byte[] data = Files.readAllBytes(sourceFile.toPath());
629-
StringBuilder b = new StringBuilder(new String(data, "UTF-8"));
623+
DataInputStream dis = new DataInputStream(new FileInputStream(sourceFile));
624+
byte[] data = new byte[(int)sourceFile.length()];
625+
dis.readFully(data);
626+
dis.close();
627+
StringBuilder b = new StringBuilder(new String(data));
630628
return b;
631629
}
632630
//
@@ -656,7 +654,9 @@ private static void replaceInFile(File sourceFile, String... values) throws IOEx
656654
// don't start the output file until all the processing is done
657655
//
658656
System.out.println("Rewrite " + sourceFile + " with " + totchanges + " changes");
659-
Files.write(sourceFile.toPath(), str.toString().getBytes("UTF-8"), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
657+
FileWriter fios = new FileWriter(sourceFile);
658+
fios.write(str.toString());
659+
fios.close();
660660
}
661661

662662

@@ -683,25 +683,12 @@ public static void copy(InputStream i, OutputStream o) throws IOException {
683683
*/
684684
public static void copy(InputStream i, OutputStream o, int bufferSize) throws IOException {
685685
try {
686-
if (i instanceof FileInputStream && o instanceof FileOutputStream) {
687-
// This case should be handled by caller using Files.copy usually, but if streams are passed we can't cast to Path
688-
// We can't use Files.copy with streams easily without copying to temp.
689-
// So we stick to stream copy but ensure buffering.
690-
}
691-
692-
if(!(i instanceof BufferedInputStream)) {
693-
i = new BufferedInputStream(i);
694-
}
695-
if(!(o instanceof BufferedOutputStream)) {
696-
o = new BufferedOutputStream(o);
697-
}
698686
byte[] buffer = new byte[bufferSize];
699687
int size = i.read(buffer);
700688
while(size > -1) {
701689
o.write(buffer, 0, size);
702690
size = i.read(buffer);
703691
}
704-
o.flush();
705692
} finally {
706693
cleanup(o);
707694
cleanup(i);

vm/ByteCodeTranslator/src/com/codename1/tools/translator/Cache.java

Lines changed: 0 additions & 113 deletions
This file was deleted.

vm/ByteCodeTranslator/src/com/codename1/tools/translator/Parser.java

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
package com.codename1.tools.translator;
2525

2626
import java.io.*;
27-
import java.nio.file.Files;
28-
import java.nio.file.StandardOpenOption;
2927
import java.util.*;
3028

3129
import org.objectweb.asm.AnnotationVisitor;
@@ -129,7 +127,7 @@ public static int addToConstantPool(String s) {
129127

130128

131129

132-
private static void generateClassAndMethodIndexHeader(File outputDirectory, Cache cache) throws Exception {
130+
private static void generateClassAndMethodIndexHeader(File outputDirectory) throws Exception {
133131
int classOffset = 0;
134132
int methodOffset = 0;
135133
ArrayList<BytecodeMethod> methods = new ArrayList<BytecodeMethod>();
@@ -327,8 +325,12 @@ private static void generateClassAndMethodIndexHeader(File outputDirectory, Cach
327325

328326
bld.append("\n\n#endif // __CN1_CLASS_METHOD_INDEX_H__\n");
329327

330-
writeIfChanged(new File(outputDirectory, "cn1_class_method_index.h"), bld.toString(), cache);
331-
writeIfChanged(new File(outputDirectory, "cn1_class_method_index.m"), bldM.toString(), cache);
328+
FileOutputStream fos = new FileOutputStream(new File(outputDirectory, "cn1_class_method_index.h"));
329+
fos.write(bld.toString().getBytes("UTF-8"));
330+
fos.close();
331+
fos = new FileOutputStream(new File(outputDirectory, "cn1_class_method_index.m"));
332+
fos.write(bldM.toString().getBytes("UTF-8"));
333+
fos.close();
332334
}
333335

334336
private static String encodeString(String con) {
@@ -438,18 +440,16 @@ public static void writeOutput(File outputDirectory) throws Exception {
438440
System.out.println("unusued Method cull removed "+neliminated+" methods in "+(dif/1000)+" seconds");
439441
}
440442

441-
Cache cache = new Cache(outputDirectory);
442-
generateClassAndMethodIndexHeader(outputDirectory, cache);
443+
generateClassAndMethodIndexHeader(outputDirectory);
443444

444445
boolean concatenate = "true".equals(System.getProperty("concatenateFiles", "false"));
445446
ConcatenatingFileOutputStream cos = concatenate ? new ConcatenatingFileOutputStream(outputDirectory) : null;
446447

447448
for(ByteCodeClass bc : classes) {
448449
file = bc.getClsName();
449-
writeFile(bc, outputDirectory, cos, cache);
450+
writeFile(bc, outputDirectory, cos);
450451
}
451452
if (cos != null) cos.realClose();
452-
cache.save();
453453

454454
} catch(Throwable t) {
455455
System.out.println("Error while working with the class: " + file);
@@ -626,33 +626,27 @@ private static boolean isMethodUsed(BytecodeMethod m, ByteCodeClass cls) {
626626
return false;
627627
}
628628

629-
private static void writeFile(ByteCodeClass cls, File outputDir, ConcatenatingFileOutputStream writeBufferInstead, Cache cache) throws Exception {
630-
if (writeBufferInstead != null && ByteCodeTranslator.output == ByteCodeTranslator.OutputType.OUTPUT_TYPE_IOS) {
631-
writeBufferInstead.beginNextFile(cls.getClsName());
632-
writeBufferInstead.write(cls.generateCCode(classes).getBytes("UTF-8"));
633-
writeBufferInstead.close();
629+
private static void writeFile(ByteCodeClass cls, File outputDir, ConcatenatingFileOutputStream writeBufferInstead) throws Exception {
630+
OutputStream outMain =
631+
writeBufferInstead != null && ByteCodeTranslator.output == ByteCodeTranslator.OutputType.OUTPUT_TYPE_IOS ?
632+
writeBufferInstead :
633+
new FileOutputStream(new File(outputDir, cls.getClsName() + "." + ByteCodeTranslator.output.extension()));
634634

635-
// we also need to write the header file for C outputs
636-
String headerName = cls.getClsName() + ".h";
637-
writeIfChanged(new File(outputDir, headerName), cls.generateCHeader(), cache);
638-
return;
635+
if (outMain instanceof ConcatenatingFileOutputStream) {
636+
((ConcatenatingFileOutputStream)outMain).beginNextFile(cls.getClsName());
639637
}
640-
641638
if(ByteCodeTranslator.output == ByteCodeTranslator.OutputType.OUTPUT_TYPE_CSHARP) {
642-
writeIfChanged(new File(outputDir, cls.getClsName() + "." + ByteCodeTranslator.output.extension()), cls.generateCSharpCode(), cache);
639+
outMain.write(cls.generateCSharpCode().getBytes());
640+
outMain.close();
643641
} else {
644-
writeIfChanged(new File(outputDir, cls.getClsName() + "." + ByteCodeTranslator.output.extension()), cls.generateCCode(classes), cache);
642+
outMain.write(cls.generateCCode(classes).getBytes());
643+
outMain.close();
645644

646645
// we also need to write the header file for C outputs
647646
String headerName = cls.getClsName() + ".h";
648-
writeIfChanged(new File(outputDir, headerName), cls.generateCHeader(), cache);
649-
}
650-
}
651-
652-
private static void writeIfChanged(File dest, String content, Cache cache) throws IOException {
653-
byte[] data = content.getBytes("UTF-8");
654-
if(cache.shouldWrite(dest, data)) {
655-
Files.write(dest.toPath(), data, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
647+
FileOutputStream outHeader = new FileOutputStream(new File(outputDir, headerName));
648+
outHeader.write(cls.generateCHeader().getBytes());
649+
outHeader.close();
656650
}
657651
}
658652

0 commit comments

Comments
 (0)