Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use a denser coding of strings, also coalesce them
  • Loading branch information
sorear committed May 23, 2011
1 parent 7febed1 commit 481e8ca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
36 changes: 28 additions & 8 deletions lib/CLRBackend.cs
Expand Up @@ -178,6 +178,7 @@ class Unit {
bool depsBound;
public FieldInfo rtunit;
public List<byte> thaw_heap;
public Dictionary<string,int> existing_strings;

public Unit(object[] from, object[] code) {
mainline_ref = Xref.from(from[0]);
Expand All @@ -193,6 +194,7 @@ class Unit {
id_to_tdep = new List<Unit>();
const_pool = new Dictionary<string,CpsOp>();
thaw_heap = new List<byte>();
existing_strings = new Dictionary<string,int>();
for (int i = 0; i < xref.Length; i++) {
if (xref[i] == null) continue;
object[] xr = (object[]) xref[i];
Expand Down Expand Up @@ -348,8 +350,29 @@ class Unit {

public void EmitStr(string s) {
if (s == null) { EmitUShort(0xFFFF); return; }
EmitUShort(s.Length);
foreach(char c in s) EmitUShort((int)c);
int l;
if (existing_strings.TryGetValue(s, out l)) {
if (l >= 0x3FFF) {
EmitInt(l * 4 + 1);
} else {
EmitUShort(l * 4 + 3);
}
} else {
existing_strings[s] = thaw_heap.Count;
bool wide = false;
foreach (char c in s) if (c > 0xFF) wide = true;
l = s.Length * 8;
if (wide) l+= 4;
if (l >= 0xFFF0) {
EmitInt(l);
} else {
EmitUShort(l + 2);
}
foreach(char c in s) {
if (wide) EmitUShort((int)c);
else EmitByte((int)c);
}
}
}

public void EmitIntArray(int[] s) {
Expand Down Expand Up @@ -3178,13 +3201,10 @@ class NamProcessor {
CpsOp.NewIntArray(Tokens.Int32, vec));
};
handlers["rxpushcapture"] = delegate(NamProcessor th, object[] z) {
CpsOp[] strs = new CpsOp[z.Length - 2];
for(int i = 0; i < strs.Length; i++)
strs[i] = CpsOp.StringLiteral(FixStr(z[i+2]));
CpsOp vec = th.Constant(CpsOp.NewArray(Tokens.String, strs));
CpsOp strs = CpsOp.StringArray(false, JScalar.SA(2,z));
return CpsOp.MethodCall(Tokens.RxFrame_PushCapture,
CpsOp.GetField(Tokens.Frame_rx, CpsOp.CallFrame()), vec,
th.Scan(z[1]));
CpsOp.GetField(Tokens.Frame_rx, CpsOp.CallFrame()),
th.Constant(strs), th.Scan(z[1]));
};
handlers["rxbprim"] = delegate(NamProcessor th, object[] z) {
CpsOp[] args = new CpsOp[z.Length - 1];
Expand Down
17 changes: 15 additions & 2 deletions lib/Kernel.cs
Expand Up @@ -259,9 +259,22 @@ public sealed class RuntimeUnit {

public string ReadStr(ref int from) {
int l = ReadShort(ref from);
if ((l & 2) == 0) l |= (ReadShort(ref from) << 16);
if (l == 0xffff) return null;
char[] r = new char[l];
for (int i = 0; i < r.Length; i++) r[i] = (char)ReadShort(ref from);

if ((l & 1) != 0) {
int rf = l >> 2;
return ReadStr(ref rf);
}

char[] r = new char[l >> 3];
if ((l & 4) != 0) {
for (int i = 0; i < r.Length; i++)
r[i] = (char)ReadShort(ref from);
} else {
for (int i = 0; i < r.Length; i++)
r[i] = (char)heap[from++];
}
return new string(r);
}

Expand Down

0 comments on commit 481e8ca

Please sign in to comment.