Skip to content

Commit

Permalink
speed up decompile by ~5 sec 🤠
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolMineman committed Oct 27, 2021
1 parent df05e6b commit f34d0c8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 100 deletions.
2 changes: 1 addition & 1 deletion brachyura/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<dependency>
<groupId>io.github.coolcrabs</groupId>
<artifactId>cfr</artifactId>
<version>0.3</version>
<version>0.4</version>
</dependency>
<dependency>
<groupId>io.github.coolcrabs</groupId>
Expand Down
2 changes: 1 addition & 1 deletion cfr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.coolcrabs</groupId>
<artifactId>cfr</artifactId>
<version>0.3</version>
<version>0.4</version>

<name>cfr</name>
<description>CFR Java decompiler</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,100 @@

import org.benf.cfr.reader.util.ConfusedCFRException;

import java.io.DataInputStream;

/**
* {@link java.io.DataInputStream}
*/
public abstract class AbstractBackedByteData implements ByteData {
protected final byte[] d;

protected AbstractBackedByteData(byte[] data) {
this.d = data;
}

abstract DataInputStream rawDataAsStream(int offset, int length);
abstract int getRealOffset(int offset);

@Override
public int getS4At(long o) throws ConfusedCFRException {
// Let's find an EFFICIENT way to do this later!
DataInputStream dis = rawDataAsStream((int) o, 4);
int a = getRealOffset((int) o);
try {
return dis.readInt();
} catch (Exception e) {
return (((d[a] & 0xFF) << 24) | ((d[a + 1] & 0xFF) << 16) | ((d[a + 2] & 0xFF) << 8) | (d[a + 3] & 0xFF));
} catch (IndexOutOfBoundsException e) {
throw new ConfusedCFRException(e);
}
}

@Override
public double getDoubleAt(long o) throws ConfusedCFRException {
DataInputStream dis = rawDataAsStream((int) o, 8);
try {
return dis.readDouble();
} catch (Exception e) {
throw new ConfusedCFRException(e);
}
return Double.longBitsToDouble(getLongAt(o));
}

@Override
public float getFloatAt(long o) throws ConfusedCFRException {
DataInputStream dis = rawDataAsStream((int) o, 8);
try {
return dis.readFloat();
} catch (Exception e) {
throw new ConfusedCFRException(e);
}
return Float.intBitsToFloat(getS4At(o));
}

@Override
public long getLongAt(long o) throws ConfusedCFRException {
DataInputStream dis = rawDataAsStream((int) o, 8);
int a = getRealOffset((int) o);
try {
return dis.readLong();
} catch (Exception e) {
return (((long)(d[a + 0] & 255) << 56) +
((long)(d[a + 1] & 255) << 48) +
((long)(d[a + 2] & 255) << 40) +
((long)(d[a + 3] & 255) << 32) +
((long)(d[a + 4] & 255) << 24) +
((d[a + 5] & 255) << 16) +
((d[a + 6] & 255) << 8) +
((d[a + 7] & 255) << 0));
} catch (IndexOutOfBoundsException e) {
throw new ConfusedCFRException(e);
}
}

@Override
public short getS2At(long o) throws ConfusedCFRException {
// Let's find an EFFICIENT way to do this later!
DataInputStream dis = rawDataAsStream((int) o, 2);
int a = getRealOffset((int) o);
try {
return dis.readShort();
} catch (Exception e) {
return (short)(((d[a] & 0xFF) << 8) | (d[a + 1] & 0xFF));
} catch (IndexOutOfBoundsException e) {
throw new ConfusedCFRException(e);
}
}

@Override
public int getU2At(long o) throws ConfusedCFRException {
// Let's find an EFFICIENT way to do this later!
DataInputStream dis = rawDataAsStream((int) o, 2);
int a = getRealOffset((int) o);
try {
return dis.readUnsignedShort();
} catch (Exception e) {
return (((d[a] & 0xFF) << 8) | (d[a + 1] & 0xFF));
} catch (IndexOutOfBoundsException e) {
throw new ConfusedCFRException(e);
}
}

@Override
public short getU1At(long o) throws ConfusedCFRException {
// Let's find an EFFICIENT way to do this later!
DataInputStream dis = rawDataAsStream((int) o, 1);
int a = getRealOffset((int) o);
try {
return (short) dis.readUnsignedByte();
} catch (Exception e) {
return (short)(d[a] & 0xff);
} catch (IndexOutOfBoundsException e) {
throw new ConfusedCFRException(e);
}
}

@Override
public byte getS1At(long o) {
int a = getRealOffset((int) o);
try {
return d[a];
} catch (IndexOutOfBoundsException e) {
throw new ConfusedCFRException(e);
}
}

@Override
public byte[] getBytesAt(int count, long offset) {
int a = getRealOffset((int) offset);
byte[] res = new byte[count];
System.arraycopy(d, a, res, 0, count);
return res;
}
}
28 changes: 6 additions & 22 deletions cfr/src/org/benf/cfr/reader/util/bytestream/BaseByteData.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
package org.benf.cfr.reader.util.bytestream;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;

public class BaseByteData extends AbstractBackedByteData {
private final byte[] data;

public BaseByteData(byte[] data) {
this.data = data;
}

@Override
public DataInputStream rawDataAsStream(int start, int len) {
return new DataInputStream(new ByteArrayInputStream(data, start, len));
super(data);
}

@Override
public ByteData getOffsetData(long offset) {
return new OffsetBackedByteData(data, offset);
return new OffsetBackedByteData(d, offset);
}

@Override
public OffsettingByteData getOffsettingOffsetData(long offset) {
return new OffsettingBackedByteData(data, offset);
}

@Override
public byte[] getBytesAt(int count, long offset) {
byte[] res = new byte[count];
System.arraycopy(data, (int) offset, res, 0, count);
return res;
return new OffsettingBackedByteData(d, offset);
}

@Override
public byte getS1At(long o) {
return data[(int) o];
int getRealOffset(int offset) {
return offset;
}

}
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
package org.benf.cfr.reader.util.bytestream;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;

public class OffsetBackedByteData extends AbstractBackedByteData {
private final int offset;
private final byte[] data;

OffsetBackedByteData(byte[] data, long offset) {
super(data);
this.offset = (int) offset;
this.data = data;
}

@Override
public DataInputStream rawDataAsStream(int start, int len) {
return new DataInputStream(new ByteArrayInputStream(data, start + offset, len));
}

@Override
public ByteData getOffsetData(long offset) {
return new OffsetBackedByteData(data, this.offset + offset);
return new OffsetBackedByteData(d, this.offset + offset);
}

@Override
public OffsettingByteData getOffsettingOffsetData(long offset) {
return new OffsettingBackedByteData(data, this.offset + offset);
}

@Override
public byte getS1At(long o) {
return data[(int) (offset + o)];
return new OffsettingBackedByteData(d, this.offset + offset);
}

@Override
public byte[] getBytesAt(int count, long offset) {
byte[] res = new byte[count];
System.arraycopy(data, (int) (this.offset + offset), res, 0, count);
return res;
int getRealOffset(int o) {
return o + offset;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package org.benf.cfr.reader.util.bytestream;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;

public class OffsettingBackedByteData extends AbstractBackedByteData implements OffsettingByteData {
private final byte[] data;
private final int originalOffset;
private int mutableOffset;

OffsettingBackedByteData(byte[] data, long offset) {
this.data = data;
super(data);
this.originalOffset = (int) offset;
this.mutableOffset = 0;
}
Expand All @@ -24,30 +20,18 @@ public long getOffset() {
return mutableOffset;
}

@Override
public DataInputStream rawDataAsStream(int start, int len) {
return new DataInputStream(new ByteArrayInputStream(data, start + originalOffset + mutableOffset, len));
}

@Override
public ByteData getOffsetData(long offset) {
return new OffsetBackedByteData(data, originalOffset + mutableOffset + offset);
return new OffsetBackedByteData(d, originalOffset + mutableOffset + offset);
}

@Override
public OffsettingByteData getOffsettingOffsetData(long offset) {
return new OffsettingBackedByteData(data, originalOffset + mutableOffset + offset);
}

@Override
public byte getS1At(long o) {
return data[(int) (originalOffset + mutableOffset + o)];
return new OffsettingBackedByteData(d, originalOffset + mutableOffset + offset);
}

@Override
public byte[] getBytesAt(int count, long offset) {
byte[] res = new byte[count];
System.arraycopy(data, (int) (this.originalOffset + this.mutableOffset + offset), res, 0, count);
return res;
int getRealOffset(int offset) {
return originalOffset + mutableOffset + offset;
}
}

0 comments on commit f34d0c8

Please sign in to comment.