多くの場合、非常に頻繁に実行されるコード内のオブジェクト割り当てを最小限に抑える必要があります。
もちろん、オブジェクトプーリングなどの通常の手法を使用することもできますが、ローカルに含まれているものが必要な場合もあります。
これを達成するために、私は以下を思いついた:
public static class Reusable<T> where T : new()
{
private static T _Internal;
private static Action<T> _ResetAction;
static Reusable()
{
_Internal = Activator.CreateInstance<T>();
}
public static void SetResetAction(Action<T> resetAction)
{
_ResetAction = resetAction;
}
public static T Get()
{
#if DEBUG
if (_ResetAction == null)
{
throw new InvalidOperationException("You must set the reset action first");
}
#endif
_ResetAction(_Internal);
return _Internal;
}
}
現在、使用法は次のようになります。
// In initialisation function somewhere
Reuseable<List<int>>.SetResetAction((l) => l.Clear());
....
// In loop
var list = Reuseable<List<int>>.Get();
// Do stuff with list
私が改善したいのは、すべてが1つの場所に含まれていないという事実です(.SetResetAction
実際に使用されている場所とは別です)。
次のようなコードを取得したいと思います。
// In loop
var list = Reuseable<List<int>>.Get((l) => l.Clear());
// Do stuff with list
Action<T>
これに伴う問題は、ループごとにオブジェクトの割り当てを取得する(作成する)ことです。
オブジェクトを割り当てずに、私が求めている使用法を取得することは可能ですか?
ReuseableList<T>
もちろん、組み込みのを作成することもAction
できますが、アクションが異なる可能性がある他のケースを考慮に入れたいと思います。