Skip to content

Commit

Permalink
Merge pull request #64 from WattleScript/opt-memory
Browse files Browse the repository at this point in the history
Optimise memory usage
  • Loading branch information
CallumDev authored Sep 24, 2023
2 parents 34e859c + bbe9fec commit d9b6814
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 46 deletions.
70 changes: 43 additions & 27 deletions src/WattleScript.Interpreter/Debugging/SourceRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,43 @@ namespace WattleScript.Interpreter.Debugging
/// </summary>
public class SourceRef
{
private int Flags = 0;
private const int FLAG_CLRLOCATION = (1 << 0);
private const int FLAG_BREAKPOINT = (1 << 1);
private const int FLAG_CANNOT_BREAKPOINT = (1 << 2);
private const int FLAG_STEPSTOP = (1 << 3);
//Flags accessors


/// <summary>
/// Gets a value indicating whether this location is inside CLR .
/// </summary>
public bool IsClrLocation { get; private set; }
public bool IsClrLocation => (Flags & FLAG_CLRLOCATION) != 0;

/// <summary>
/// Gets a value indicating whether this instance is a stop "step" in source mode
/// </summary>
public bool IsStepStop => (Flags & FLAG_STEPSTOP) != 0;

/// <summary>
/// Gets a value indicating whether this instance is a breakpoint
/// </summary>
public bool Breakpoint
{
get => (Flags & FLAG_BREAKPOINT) != 0;
set
{
if (value)
Flags |= FLAG_BREAKPOINT;
else
Flags &= ~FLAG_BREAKPOINT;
}
}

/// <summary>
/// Gets a value indicating whether this instance cannot be set as a breakpoint
/// </summary>
public bool CannotBreakpoint => (Flags & FLAG_CANNOT_BREAKPOINT) != 0;

/// <summary>
/// Gets the index of the source.
Expand All @@ -33,19 +66,7 @@ public class SourceRef
/// Gets to which line the source code ref ends
/// </summary>
public int ToLine { get; }
/// <summary>
/// Gets a value indicating whether this instance is a stop "step" in source mode
/// </summary>
public bool IsStepStop { get; }

/// <summary>
/// Gets a value indicating whether this instance is a breakpoint
/// </summary>
public bool Breakpoint;
/// <summary>
/// Gets a value indicating whether this instance cannot be set as a breakpoint
/// </summary>
public bool CannotBreakpoint { get; private set; }

/// <summary>
/// Gets character index the source ref starts at
/// </summary>
Expand All @@ -57,7 +78,7 @@ public class SourceRef

internal static SourceRef GetClrLocation()
{
return new SourceRef(0, 0, 0, 0, 0, false, 0, 0) { IsClrLocation = true };
return new SourceRef(0, 0, 0, 0, 0, false, 0, 0) { Flags = FLAG_CLRLOCATION };
}

public SourceRef(SourceRef src, bool isStepStop)
Expand All @@ -67,7 +88,7 @@ public SourceRef(SourceRef src, bool isStepStop)
ToChar = src.ToChar;
FromLine = src.FromLine;
ToLine = src.ToLine;
IsStepStop = isStepStop;
Flags = isStepStop ? FLAG_STEPSTOP : 0;
ToCharIndex = src.ToCharIndex;
FromCharIndex = src.FromCharIndex;
}
Expand All @@ -81,17 +102,14 @@ public override bool Equals(object obj)
return false;
}

protected bool Equals(SourceRef other)
public bool Equals(SourceRef other)
{
return Breakpoint == other.Breakpoint &&
IsClrLocation == other.IsClrLocation &&
return Flags == other.Flags &&
SourceIdx == other.SourceIdx &&
FromChar == other.FromChar &&
ToChar == other.ToChar &&
FromLine == other.FromLine &&
ToLine == other.ToLine &&
IsStepStop == other.IsStepStop &&
CannotBreakpoint == other.CannotBreakpoint &&
ToCharIndex == other.ToCharIndex &&
FromCharIndex == other.FromCharIndex;
}
Expand All @@ -100,17 +118,14 @@ public override int GetHashCode()
{
unchecked
{
var hashCode = Breakpoint.GetHashCode();
hashCode = (hashCode * 397) ^ IsClrLocation.GetHashCode();
var hashCode = Flags.GetHashCode();
hashCode = (hashCode * 397) ^ SourceIdx;
hashCode = (hashCode * 397) ^ FromChar;
hashCode = (hashCode * 397) ^ ToChar;
hashCode = (hashCode * 397) ^ FromLine;
hashCode = (hashCode * 397) ^ ToLine;
hashCode = (hashCode * 397) ^ IsStepStop.GetHashCode();
hashCode = (hashCode * 397) ^ FromCharIndex;
hashCode = (hashCode * 397) ^ ToCharIndex;
hashCode = (hashCode * 397) ^ CannotBreakpoint.GetHashCode();
return hashCode;
}
}
Expand All @@ -128,6 +143,7 @@ public override int GetHashCode()
public SourceRef(int sourceIdx)
{
SourceIdx = sourceIdx;

}

public SourceRef(int sourceIdx, int from, int to, int fromline, int toline, bool isStepStop, int charIndexFrom, int charIndexTo)
Expand All @@ -137,7 +153,7 @@ public SourceRef(int sourceIdx, int from, int to, int fromline, int toline, bool
ToChar = to;
FromLine = fromline;
ToLine = toline;
IsStepStop = isStepStop;
Flags = isStepStop ? FLAG_STEPSTOP : 0;
ToCharIndex = charIndexTo;
FromCharIndex = charIndexFrom;
}
Expand Down Expand Up @@ -228,7 +244,7 @@ public bool IncludesLocation(int sourceIdx, int line, int col)
/// <returns></returns>
public SourceRef SetNoBreakPoint()
{
CannotBreakpoint = true;
Flags |= FLAG_CANNOT_BREAKPOINT;
return this;
}

Expand Down
8 changes: 4 additions & 4 deletions src/WattleScript.Interpreter/Execution/DynamicExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ internal DynamicExpression(Script S, string strExpr, DynValue constant)
/// </summary>
/// <param name="context">The context.</param>
/// <returns></returns>
public DynValue Evaluate(ScriptExecutionContext context = null)
public DynValue Evaluate(ScriptExecutionContext? context = null)
{
context = context ?? OwnerScript.CreateDynamicExecutionContext();
var ctx = context ?? OwnerScript.CreateDynamicExecutionContext();

this.CheckScriptOwnership(context.GetScript());
this.CheckScriptOwnership(ctx.GetScript());

if (!m_Constant.IsNil())
return m_Constant;

return m_Exp.Eval(context);
return m_Exp.Eval(ctx);
}

/// <summary>
Expand Down
37 changes: 22 additions & 15 deletions src/WattleScript.Interpreter/Execution/ScriptExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,47 @@ namespace WattleScript.Interpreter
/// <summary>
/// Class giving access to details of the environment where the script is executing
/// </summary>
public class ScriptExecutionContext : IScriptPrivateResource
public struct ScriptExecutionContext : IScriptPrivateResource
{
Processor m_Processor;
CallbackFunction m_Callback;

internal bool CanAwait { get; set; }
private SourceRef m_CallingLocation;
private int m_Flags;

private const int FLAG_ISDYNAMIC = (1 << 0);
private const int FLAG_CANAWAIT = (1 << 1);

internal bool CanAwait
{
get => (m_Flags & FLAG_CANAWAIT) != 0;
set
{
if (value)
m_Flags |= FLAG_CANAWAIT;
else
m_Flags &= ~FLAG_CANAWAIT;
}
}

internal ScriptExecutionContext(Processor p, CallbackFunction callBackFunction, SourceRef sourceRef, bool isDynamic = false)
{
IsDynamicExecution = isDynamic;
m_Flags = isDynamic ? FLAG_ISDYNAMIC : 0;
m_Processor = p;
m_Callback = callBackFunction;
CallingLocation = sourceRef;
m_CallingLocation = sourceRef;
}

/// <summary>
/// Gets a value indicating whether this instance is running a dynamic execution.
/// Under a dynamic execution, most methods of ScriptExecutionContext are not reliable as the
/// processing engine of the script is not "really" running or is not available.
/// </summary>
public bool IsDynamicExecution
{
get;
private set;
}
public bool IsDynamicExecution => (m_Flags & FLAG_ISDYNAMIC) != 0;

/// <summary>
/// Gets the location of the code calling back
/// </summary>
public SourceRef CallingLocation
{
get;
private set;
}
public SourceRef CallingLocation => m_CallingLocation;

/// <summary>
/// Gets or sets the additional data associated to this CLR function call.
Expand Down

0 comments on commit d9b6814

Please sign in to comment.