Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.ksm file optimizations #1733

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/kOS.Safe/Compilation/CompiledObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ public static ulong DecodeNumberFromBytes(byte[] encodedForm)
/// </summary>
private static Dictionary<object,int> argumentPackFinder;

private static string previousLabel = "######"; // bogus value that is ensured to differ from any real value the first time through.
private const string LABEL_INIT = "######"; // bogus value that is ensured to differ from any real value the first time through.

private static string previousLabel = LABEL_INIT;

/// <summary>
/// Returns the compiled program's opcodes packed into a tight form, that is a direct
Expand All @@ -185,7 +187,7 @@ public static byte[] Pack(List<CodePart> program)
argumentPackLogicalLength = 0; // nothing in the argumentPack yet.
argumentPackFinder = new Dictionary<object,int>();
lineMap = new DebugLineMap();
previousLabel = "######"; // bogus value that is ensured to differ from any real value the first time through.
previousLabel = LABEL_INIT;

for (int index = 0 ; index < program.Count ; ++index) // --. This can be replaced with a
{ // |--- foreach. I do it this way so I
Expand All @@ -198,7 +200,7 @@ public static byte[] Pack(List<CodePart> program)
// Now that we've seen every argument, we know how many bytes are needed
// to store the argumentPack, and thus the largest possible index into it.
// This will be how many bytes our indeces will be in this packed ML file.
int numArgIndexBytes = FewestBytesToHold(argumentPackLogicalLength);
int numArgIndexBytes = FewestBytesToHold(argumentPackFinder.Count);
headBuff.Add((byte)'%');
headBuff.Add((byte)'A');
headBuff.Add(((byte)numArgIndexBytes));
Expand All @@ -208,6 +210,7 @@ public static byte[] Pack(List<CodePart> program)

headBuff.AddRange(truncatedArgumentPack);

previousLabel = LABEL_INIT;
for (int index = 0 ; index < program.Count ; ++index) // --. This can be replaced with a
{ // |--- foreach. I do it this way so I
CodePart codePart = program[index]; // --' can print the index in debugging.
Expand Down Expand Up @@ -343,8 +346,6 @@ private static byte[] PackCode(List<Opcode> fragment, int argIndexSize, int star
/// <returns>byte index of where it starts in the argument pack.</returns>
private static int PackedArgumentLocation(object argument)
{
const int LABEL_OFFSET = 3; // Account for the %An at the front of the argument pack.

object arg = argument ?? new PseudoNull();

int returnValue; // bogus starting value before it's calculated.
Expand All @@ -357,8 +358,8 @@ private static int PackedArgumentLocation(object argument)

// When it gets added, it's going to be tacked on right at the end.
// We already know that, se let's get that populated now:
argumentPackFinder.Add(arg, LABEL_OFFSET + argumentPackLogicalLength);
returnValue = LABEL_OFFSET + argumentPackLogicalLength;
returnValue = argumentPackFinder.Count;
argumentPackFinder.Add(arg, returnValue);

// Borrow C#'s Binary IO writer to pack the object into the byte form,
// rather than writing our own for each type:
Expand Down Expand Up @@ -487,8 +488,8 @@ public static List<CodePart> UnPack(GlobalPath path, string prefix, byte[] conte
int argIndexSize;
Dictionary<int,object> arguments = ReadArgumentPack(reader, out argIndexSize);
lineMap = new DebugLineMap();
previousLabel = "######"; // bogus value that is ensured to differ from any real value the first time through.

previousLabel = LABEL_INIT;

int codeStart = 0;

Expand Down Expand Up @@ -555,14 +556,13 @@ private static Dictionary<int,object> ReadArgumentPack(BinaryReader reader, out
bool sectionEnded = false;
while (reader.BaseStream.Position < reader.BaseStream.Length && !(sectionEnded))
{
int offsetLocation = (int)(reader.BaseStream.Position) - startPos;

byte argTypeId = reader.ReadByte();
Type argCSharpType;
if (typeFromId.TryGetValue(argTypeId, out argCSharpType))
{
object arg = ReadSomeBinaryPrimitive(reader, argCSharpType);
returnArgs.Add(offsetLocation,arg);
returnArgs.Add(returnArgs.Count,arg);
}
else
{
Expand Down
7 changes: 7 additions & 0 deletions src/kOS.Safe/Execution/KOSArgMarkerType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ namespace kOS.Safe.Execution
/// </summary>
public class KOSArgMarkerType
{
// all instances of KOSArgMarkerType should be considered identical:
public override bool Equals(object o)
{
return o is KOSArgMarkerType;
}

public override int GetHashCode() { return 0; }
public override string ToString()
{
return "_KOSArgMarker_";
Expand Down