Skip to content
Merged
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
38 changes: 34 additions & 4 deletions Jint/Native/JsProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,24 +669,54 @@ internal override bool SetPrototypeOf(JsValue value)
return callable;
}

// trap argument arrays are rented and returned after the call completes, following the
// interpreter call-site pattern (no finally: an exception lets the array fall to the GC,
// and JsArguments.Materialize protects arrays captured by the callee)

private JsValue CallTrap(ICallable trap, JsValue arg0)
{
return trap.Call(_handler!, [arg0]);
var pool = _engine._jsValueArrayPool;
var args = pool.RentArray(1);
args[0] = arg0;
var result = trap.Call(_handler!, args);
pool.ReturnArray(args);
return result;
}

private JsValue CallTrap(ICallable trap, JsValue arg0, JsValue arg1)
{
return trap.Call(_handler!, [arg0, arg1]);
var pool = _engine._jsValueArrayPool;
var args = pool.RentArray(2);
args[0] = arg0;
args[1] = arg1;
var result = trap.Call(_handler!, args);
pool.ReturnArray(args);
return result;
}

private JsValue CallTrap(ICallable trap, JsValue arg0, JsValue arg1, JsValue arg2)
{
return trap.Call(_handler!, [arg0, arg1, arg2]);
var pool = _engine._jsValueArrayPool;
var args = pool.RentArray(3);
args[0] = arg0;
args[1] = arg1;
args[2] = arg2;
var result = trap.Call(_handler!, args);
pool.ReturnArray(args);
return result;
}

private JsValue CallTrap(ICallable trap, JsValue arg0, JsValue arg1, JsValue arg2, JsValue arg3)
{
return trap.Call(_handler!, [arg0, arg1, arg2, arg3]);
var pool = _engine._jsValueArrayPool;
var args = pool.RentArray(4);
args[0] = arg0;
args[1] = arg1;
args[2] = arg2;
args[3] = arg3;
var result = trap.Call(_handler!, args);
pool.ReturnArray(args);
return result;
}

private void AssertNotRevoked(JsValue key)
Expand Down
19 changes: 19 additions & 0 deletions Jint/Pooling/JsValueArrayPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ internal sealed class JsValueArrayPool
private readonly ObjectPool<JsValue[]> _poolArray1;
private readonly ObjectPool<JsValue[]> _poolArray2;
private readonly ObjectPool<JsValue[]> _poolArray3;
private readonly ObjectPool<JsValue[]> _poolArray4;

public JsValueArrayPool()
{
_poolArray1 = new ObjectPool<JsValue[]>(Factory1, PoolSize);
_poolArray2 = new ObjectPool<JsValue[]>(Factory2, PoolSize);
_poolArray3 = new ObjectPool<JsValue[]>(Factory3, PoolSize);
_poolArray4 = new ObjectPool<JsValue[]>(Factory4, PoolSize);
}

private static JsValue[] Factory1()
Expand All @@ -35,6 +37,11 @@ private static JsValue[] Factory3()
return new JsValue[3];
}

private static JsValue[] Factory4()
{
return new JsValue[4];
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public JsValue[] RentArray(int size)
{
Expand All @@ -54,6 +61,10 @@ public JsValue[] RentArray(int size)
{
return _poolArray3.Allocate();
}
if (size == 4)
{
return _poolArray4.Allocate();
}

return new JsValue[size];
}
Expand Down Expand Up @@ -83,5 +94,13 @@ public void ReturnArray(JsValue[] array)
array[2] = null!;
_poolArray3.Free(array);
}
else if (array.Length == 4)
{
array[0] = null!;
array[1] = null!;
array[2] = null!;
array[3] = null!;
_poolArray4.Free(array);
}
}
}
Loading