Skip to content

Commit

Permalink
Storing all constants as an array in compiled module not as a List fo…
Browse files Browse the repository at this point in the history
…r less access overhead
  • Loading branch information
pachanga committed Dec 30, 2024
1 parent 67fa53d commit 16b57d8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/compile/compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public Module Compile_Finish()
init_func_idx,
imports,
interim.gvar_index.Count,
constants,
constants.ToArray(),
type_refs,
init_bytes,
code_bytes,
Expand Down
19 changes: 10 additions & 9 deletions src/vm/compiled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class CompiledModule
public int total_gvars_num;
public byte[] initcode;
public byte[] bytecode;
public List<Const> constants;
public Const[] constants;
public TypeRefIndex type_refs;
IType[] _type_refs_resolved;
public IType[] type_refs_resolved
Expand All @@ -76,7 +76,7 @@ public CompiledModule(
int init_func_idx,
List<string> imports,
int total_gvars_num,
List<Const> constants,
Const[] constants,
TypeRefIndex type_refs,
byte[] initcode,
byte[] bytecode,
Expand All @@ -97,7 +97,7 @@ public CompiledModule()
-1,
new List<string>(),
0,
new List<Const>(),
Array.Empty<Const>(),
new TypeRefIndex(),
new byte[0],
new byte[0],
Expand Down Expand Up @@ -128,7 +128,6 @@ static public Module FromStream(
int total_gvars_num = 0;
int local_gvars_num = 0;
byte[] constant_bytes = null;
var constants = new List<Const>();
var type_refs = new TypeRefIndex();
List<int> type_refs_offsets = new List<int>();
byte[] type_refs_bytes = null;
Expand Down Expand Up @@ -201,8 +200,10 @@ static public Module FromStream(
module.ns.Link(types.ns);
module.local_gvars_mark = local_gvars_num;

var constants = new Const[constants_len];

if(constants_len > 0)
ReadConstants(symb_factory, constant_bytes, constants);
ReadConstants(constant_bytes, constants);

var compiled = new CompiledModule(
init_func_idx,
Expand All @@ -220,7 +221,7 @@ static public Module FromStream(
return module;
}

static void ReadConstants(SymbolFactory symb_factory, byte[] constant_bytes, List<Const> constants)
static void ReadConstants(byte[] constant_bytes, Const[] constants)
{
var src = new MemoryStream(constant_bytes);
using(BinaryReader r = new BinaryReader(src, System.Text.Encoding.UTF8))
Expand All @@ -242,7 +243,7 @@ static void ReadConstants(SymbolFactory symb_factory, byte[] constant_bytes, Lis
else
throw new Exception("Unknown type: " + cn_type);

constants.Add(cn);
constants[i] = cn;
}
}
}
Expand Down Expand Up @@ -313,12 +314,12 @@ static public void ToStream(Module module, Stream dst, bool leave_open = false)
}
}

static byte[] WriteConstants(List<Const> constants)
static byte[] WriteConstants(Const[] constants)
{
var dst = new MemoryStream();
using(BinaryWriter w = new BinaryWriter(dst, System.Text.Encoding.UTF8))
{
w.Write(constants.Count);
w.Write(constants.Length);
foreach(var cn in constants)
{
w.Write((byte)cn.type);
Expand Down
4 changes: 2 additions & 2 deletions src/vm/vm.frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Frame : IDeferSupport
public Module module;

public byte[] bytecode;
public List<Const> constants;
public Const[] constants;
public IType[] type_refs;
public ValStack locals = new ValStack(MAX_LOCALS);
public ValStack stack = new ValStack(MAX_STACK);
Expand Down Expand Up @@ -104,7 +104,7 @@ internal void Init(
Fiber fb,
ValStack return_stack,
Module module,
List<Const> constants,
Const[] constants,
IType[] type_refs,
byte[] bytecode,
int start_ip)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ public static int NewTestFile(string path, string text, ref List<string> files,

public static int ConstIdx(bhl.Module module, string str)
{
for(int i=0;i<module.compiled.constants.Count;++i)
for(int i=0;i<module.compiled.constants.Length;++i)
{
var cn = module.compiled.constants[i];
if(cn.type == ConstType.STR && cn.str == str)
Expand All @@ -715,7 +715,7 @@ public static int ConstIdx(bhl.Module module, string str)

public static int ConstIdx(bhl.Module module, int num)
{
for(int i=0;i<module.compiled.constants.Count;++i)
for(int i=0;i<module.compiled.constants.Length;++i)
{
var cn = module.compiled.constants[i];
if(cn.type == ConstType.INT && cn.num == num)
Expand All @@ -726,7 +726,7 @@ public static int ConstIdx(bhl.Module module, int num)

public static int ConstIdx(bhl.Module module, double num)
{
for(int i=0;i<module.compiled.constants.Count;++i)
for(int i=0;i<module.compiled.constants.Length;++i)
{
var cn = module.compiled.constants[i];
if(cn.type == ConstType.FLT && cn.num == num)
Expand All @@ -737,7 +737,7 @@ public static int ConstIdx(bhl.Module module, double num)

public static int ConstIdx(bhl.Module module, bool v)
{
for(int i=0;i<module.compiled.constants.Count;++i)
for(int i=0;i<module.compiled.constants.Length;++i)
{
var cn = module.compiled.constants[i];
if(cn.type == ConstType.BOOL && cn.num == (v ? 1 : 0))
Expand All @@ -753,7 +753,7 @@ public static int TypeIdx(bhl.Module module, ProxyType v)

public static int ConstNullIdx(bhl.Module module)
{
for(int i=0;i<module.compiled.constants.Count;++i)
for(int i=0;i<module.compiled.constants.Length;++i)
{
var cn = module.compiled.constants[i];
if(cn.type == ConstType.NIL)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_type_casts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func string test()
;
AssertEqual(c, expected);

Assert.Equal(2, c.compiled.constants.Count);
Assert.Equal(2, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -137,7 +137,7 @@ func string test()
;
AssertEqual(c, expected);

Assert.Equal(2, c.compiled.constants.Count);
Assert.Equal(2, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down
34 changes: 17 additions & 17 deletions tests/test_vm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ func bool test()
;
AssertEqual(c, expected);

Assert.Equal(2, c.compiled.constants.Count);
Assert.Equal(2, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -1402,7 +1402,7 @@ func bool test()
;
AssertEqual(c, expected);

Assert.Equal(2, c.compiled.constants.Count);
Assert.Equal(2, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -1453,7 +1453,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(2, c.compiled.constants.Count);
Assert.Equal(2, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -1486,7 +1486,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(2, c.compiled.constants.Count);
Assert.Equal(2, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -1519,7 +1519,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(2, c.compiled.constants.Count);
Assert.Equal(2, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -1552,7 +1552,7 @@ func float test()
;
AssertEqual(c, expected);

Assert.Equal(2, c.compiled.constants.Count);
Assert.Equal(2, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -2025,7 +2025,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(2, c.compiled.constants.Count);
Assert.Equal(2, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -2058,7 +2058,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(2, c.compiled.constants.Count);
Assert.Equal(2, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -2350,7 +2350,7 @@ func int test()

var c = Compile(bhl);

Assert.Equal(260, c.compiled.constants.Count);
Assert.Equal(260, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -2628,7 +2628,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(3, c.compiled.constants.Count);
Assert.Equal(3, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -3308,7 +3308,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(4, c.compiled.constants.Count);
Assert.Equal(4, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -3383,7 +3383,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(5, c.compiled.constants.Count);
Assert.Equal(5, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -3459,7 +3459,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(7, c.compiled.constants.Count);
Assert.Equal(7, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -3924,7 +3924,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(2, c.compiled.constants.Count);
Assert.Equal(2, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -4136,7 +4136,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(2, c.compiled.constants.Count);
Assert.Equal(2, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -4458,7 +4458,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(4, c.compiled.constants.Count);
Assert.Equal(4, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down Expand Up @@ -5447,7 +5447,7 @@ func int test()
;
AssertEqual(c, expected);

Assert.Equal(3, c.compiled.constants.Count);
Assert.Equal(3, c.compiled.constants.Length);

var vm = MakeVM(c);
var fb = vm.Start("test");
Expand Down

0 comments on commit 16b57d8

Please sign in to comment.