私は HFT 取引ソフトウェアを書いています。私はすべてのマイクロ秒を気にします。今は C# で書いていますが、すぐに C++ に移行します。
そのようなコードを考えてみましょう
// Original
class Foo {
....
// method is called from one thread only so no need to be thread-safe
public void FrequentlyCalledMethod() {
var actions = new List<Action>();
for (int i = 0; i < 10; i++) {
actions.Add(new Action(....));
}
// use actions, synchronous
executor.Execute(actions);
// now actions can be deleted
}
超低遅延ソフトウェアは「new」キーワードをあまり使用すべきではないと思うのでactions
、次のフィールドに移動しました。
// Version 1
class Foo {
....
private List<Action> actions = new List<Action>();
// method is called from one thread only so no need to be thread-safe
public void FrequentlyCalledMethod() {
actions.Clear()
for (int i = 0; i < 10; i++) {
actions.Add(new Action { type = ActionType.AddOrder; price = 100 + i; });
}
// use actions, synchronous
executor.Execute(actions);
// now actions can be deleted
}
おそらく、「新しい」キーワードをまったく避けるようにする必要がありますか?事前に割り当てられたオブジェクトの「プール」を使用できます。
// Version 2
class Foo {
....
private List<Action> actions = new List<Action>();
private Action[] actionPool = new Action[10];
// method is called from one thread only so no need to be thread-safe
public void FrequentlyCalledMethod() {
actions.Clear()
for (int i = 0; i < 10; i++) {
var action = actionsPool[i];
action.type = ActionType.AddOrder;
action.price = 100 + i;
actions.Add(action);
}
// use actions, synchronous
executor.Execute(actions);
// now actions can be deleted
}
- どこまで行けばいいですか?
- 避けることがどれほど重要
new
ですか? - 構成のみが必要な事前割り当てオブジェクトを使用している間、何かを獲得できますか? (上記の例ではタイプと価格を設定)
これは超低遅延なので、可読性保守性などよりもパフォーマンスが優先されると仮定してください。