Skip to content

Commit

Permalink
Tiny speed improvements, Windows native compilation support
Browse files Browse the repository at this point in the history
  • Loading branch information
FeepingCreature committed Aug 14, 2013
1 parent b6c7542 commit e8f8f4d
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 80 deletions.
8 changes: 5 additions & 3 deletions ast/aliasing.d
Expand Up @@ -75,14 +75,16 @@ class CValueAlias : ExprAlias, CValue {
override CValueAlias dup() { return fastalloc!(CValueAlias)(base.dup, name); }
}

class LValueAlias : CValueAlias, LValue {
final class LValueAlias : CValueAlias, LValue {
static const isFinal = true;
mixin MyThis!("super(base, name)"[]);
override LValueAlias dup() { return fastalloc!(LValueAlias)(base.dup, name); }
}

class MValueAlias : ExprAlias, MValue {
final class MValueAlias : ExprAlias, MValue {
static const isFinal = true;
mixin MyThis!("super(base, name)"[]);
override void emitAssignment(LLVMFile lf) { (fastcast!(MValue)~ base).emitAssignment(lf); }
void emitAssignment(LLVMFile lf) { (fastcast!(MValue)~ base).emitAssignment(lf); }
override MValueAlias dup() { return fastalloc!(MValueAlias)(base.dup, name); }
}

Expand Down
1 change: 1 addition & 0 deletions ast/arrays.d
Expand Up @@ -84,6 +84,7 @@ class Array_ : Type, RelNamespace, Dwarf2Encodable, ReferenceType {
}

final class Array : Array_ {
static const isFinal = true;
this() { super(); }
this(IType it) { super(it); }
}
Expand Down
35 changes: 18 additions & 17 deletions ast/base.d
Expand Up @@ -662,9 +662,20 @@ interface Dependency {
}

extern(C) int atoi(char*);
int my_atoi(string s) {
auto mew = qformat(s, "\x00"[]);
return atoi(mew.ptr);
bool my_atoi(string s, out int i) {
s = s.mystripl();
if (!s.length) return false;
if (s == "0") return true;
bool neg;
if (s[0] == '-') { s = s[1..$]; neg = true; }
if (!s.length) return false;
foreach (ch; s) {
if (ch < '0' || ch > '9') return false;
byte b = ch - '0';
i = i * 10 + b;
}
if (neg) i = -i;
return true;
}

class NamedNull : NoOp, Named, SelfAdding {
Expand Down Expand Up @@ -740,11 +751,7 @@ interface ModifiesName {

bool readIndexShorthand(string name, ref int i) {
auto idxstr = name.startsWith("_"[]);
if (!idxstr) return false;
auto idx = my_atoi(idxstr);
if (idxstr != Format(idx))
return false;
i = idx;
if (!idxstr || !my_atoi(idxstr, i)) return false;
return true;
}

Expand Down Expand Up @@ -896,14 +903,7 @@ Expr llvmval(T...)(T t) {
return llvmvalstr(qformat(t));
}

bool isnum(string s, out int i) {
auto var = s.my_atoi();
if (qformat(var) == s) {
i = var;
return true;
}
return false;
}
bool isnum(string s, out int i) { return my_atoi(s, i); }

string lladd(string a, string b) {
int k, l;
Expand All @@ -927,7 +927,8 @@ string lldiv(string a, string b) {
}
bool llmax_decompose_first(string s, out int i, out string k) {
if (auto rest = s.startsWith("select(i1 icmp sgt(i32 ")) {
auto num = rest.slice(" ").my_atoi();
int num;
my_atoi(rest.slice(" "), num);
auto reconstruct_a = qformat(num);
auto start = qformat("select(i1 icmp sgt(i32 ", reconstruct_a, ", i32 ");
if (auto rest2 = s.startsWith(start)) {
Expand Down
2 changes: 1 addition & 1 deletion ast/math.d
Expand Up @@ -73,7 +73,7 @@ static this() {
return null;
};
foldopt ~= delegate Itr(Itr it) {
if (auto fc = fastcast!(FunCall) (it)) {
if (auto fc = fastcast_direct!(FunCall)(it)) {
if (fc.fun.extern_c && fc.fun.name == "sqrtf"[]) {
assert(fc.params.length == 1);
auto fe = fc.params[0];
Expand Down
30 changes: 18 additions & 12 deletions ast/modules.d
Expand Up @@ -39,17 +39,21 @@ class Module : NamespaceImporter, IModule, Tree, Named, StoresDebugState, Emitti
scope(exit) current_module.set(backup);
current_module.set(this);

if (sourcefile) if (auto cache = read_cache("module imports", sourcefile)) {
Module[] res;
bool failed;
while (cache) {
auto entry = slice(cache, ",");
try res ~= lookupMod(entry);
catch (Exception ex) { failed = true; break; } // don't use cache if it leads to errors
}
if (!failed) {
imports_cache = res;
return res;
if (sourcefile) {
string relevant_file = sourcefile;
if (sourcefile == "sys.nt") relevant_file = "fcc.exe";
if (auto cache = read_cache("module imports", relevant_file)) {
Module[] res;
bool failed;
while (cache) {
auto entry = slice(cache, ",");
try res ~= lookupMod(entry);
catch (Exception ex) { failed = true; break; } // don't use cache if it leads to errors
}
if (!failed) {
imports_cache = res;
return res;
}
}
}

Expand All @@ -72,7 +76,9 @@ class Module : NamespaceImporter, IModule, Tree, Named, StoresDebugState, Emitti
if (sourcefile) {
string[] names;
foreach (entry; res) names ~= entry.name;
write_cache("module imports", sourcefile, names.join(","));
string relevant_file = sourcefile;
if (sourcefile == "sys.nt") relevant_file = "fcc.exe";
write_cache("module imports", relevant_file, names.join(","));
}

imports_cache = res;
Expand Down
7 changes: 6 additions & 1 deletion ast/pointer.d
Expand Up @@ -85,7 +85,7 @@ final class RefExpr : RefExpr_ {
}

// *foo
class DerefExpr : LValue, HasInfo {
class DerefExpr_ : LValue, HasInfo {
Expr src;
int count;
static int de_count;
Expand Down Expand Up @@ -117,6 +117,11 @@ class DerefExpr : LValue, HasInfo {
string toString() { return Format("*"[], src); }
}

final class DerefExpr : DerefExpr_ {
static const isFinal = true;
this(Expr ex) { super(ex); }
}

bool isVoidP(IType it) {
if (!it) return false;
auto p = fastcast!(Pointer)~ it;
Expand Down
11 changes: 10 additions & 1 deletion cache.d
Expand Up @@ -41,7 +41,12 @@ long atol(string s) {

string[string] findfile_cache;
string findfile(string file) {
if (auto p = file in findfile_cache) return *p;
if (auto p = file in findfile_cache) {
if (!*p) {
fail(qformat("lookup for ", file, " returned in null from cache"));
}
return *p;
}
string res;
if (file.exists()) res = file;
else {
Expand All @@ -50,8 +55,12 @@ string findfile(string file) {
res = entry.qsub(file);
break;
}
if (!res) {
fail(qformat("File not found: ", file, ", in path ", include_path));
}
}
findfile_cache[file] = res;
if (!res) fail(qformat("lookup for ", file, " returned in uncached null"));
return res;
}

Expand Down
51 changes: 32 additions & 19 deletions casts.d
Expand Up @@ -101,7 +101,7 @@ const string[] quicklist = [

Stuple!(void*, int)[] idtable;

int xor;
int xor, idmask;
const uint knuthMagic = 2654435761;

const cachesize = 1; // tested and best .. 0 is way slower, 2 is slightly slower.
Expand Down Expand Up @@ -139,7 +139,7 @@ void initCastTable() {
}
ci ~= cl;
}
int bestXOR, bestXORSize = int.max;
int bestXOR, bestXORSize = int.max, bestMask;
auto rng = fastalloc!(Mersenne)(23);
auto backing_pretable = new bool[1024];
bool[] pretable;
Expand All @@ -151,36 +151,35 @@ void initCastTable() {
for (int i = 0; i < 512; ++i) {
// lol
xor = rng();
auto cursize = quicklist.length;
// auto cursize = quicklist.length;
int cursize = 2, curmask = 1;
outer:
resize_pretable(cursize);
resetHash();
foreach (int id, entry; ci) {
auto pos = hash(cast(void*) entry) % pretable.length;
if (pretable[pos]) {
cursize ++;
// cursize ++;
cursize *= 2; curmask = (curmask << 1) | 1;
if (cursize >= bestXORSize) break;
goto outer;
}
pretable[pos] = true;
}
if (cursize < bestXORSize) {
bestXORSize = cursize;
bestXOR = xor;
bestXORSize = cursize;
bestMask = curmask;
}
}
xor = bestXOR;
idtable.length = bestXORSize;
if (PredictedTableLength != -1 && idtable.length != PredictedTableLength) {
logln("please update pred const to "[], idtable.length);
asm { int 3; }
}
idmask = bestMask;

memset(idtable.ptr, 0, idtable.length * typeof(idtable[0]).sizeof);
resetHash();
foreach (int i, entry; ci) {
static if (PredictedTableLength == -1) int len = idtable.length;
else const len = PredictedTableLength;
idtable[hash(cast(void*) entry) % len] = stuple(cast(void*) entry, i);
idtable[hash(cast(void*) entry) & idmask] = stuple(cast(void*) entry, i);
}
}

Expand All @@ -195,9 +194,7 @@ int getId(ClassInfo ci) {
if (getIdCache[idx]._0 == cp)
return getIdCache[idx]._1;
}
static if (PredictedTableLength == -1) int len = idtable.length;
else const len = PredictedTableLength;
auto entry = idtable.ptr[hash(cp) % len];
auto entry = idtable.ptr[hash(cp) & idmask];
int res = (entry._0 == cp)?entry._1:-1;
static if (getIdCacheSize) {
auto p = &getIdCache[getIdLoopPtr];
Expand All @@ -210,10 +207,26 @@ int getId(ClassInfo ci) {
return res;
}

const PredictedTableLength = -1;

extern(C) void fastcast_marker() { }

template fastcast_direct(T) {
T fastcast_direct(U)(U u) {
Object obj = void;
// stolen from fastcast
static if (!is(U: Object)) { // interface
auto ptr = **cast(Interface***) u;
void* vp = cast(void*) u - ptr.offset;
obj = *cast(Object*) &vp;
} else {
void* vp = cast(void*) u;
obj = *cast(Object*) &vp;
}

if (obj.classinfo !is typeid(T)) return null;
return *cast(T*) &obj; // prevent a redundant D cast
}
}

struct _fastcast(T) {
const ptrdiff_t INVALID = 0xffff; // magic numbah
static ptrdiff_t[quicklist.length] offsets;
Expand All @@ -229,10 +242,10 @@ struct _fastcast(T) {
const string fillObj = `
{static if (!is(U: Object)) { // interface
auto ptr = **cast(Interface***) u;
void* vp = *cast(void**) &u - ptr.offset;
void* vp = cast(void*) u - ptr.offset;
obj = *cast(Object*) &vp;
} else {
void* vp = *cast(void**) &u;
void* vp = cast(void*) u;
obj = *cast(Object*) &vp; // prevent a redundant D cast
}}`;
static if (is(typeof(T.isFinal)) && T.isFinal) {
Expand Down

0 comments on commit e8f8f4d

Please sign in to comment.