Compare commits

...
Author SHA1 Message Date
Mohammad Arham 8620839692 changes to make system not lag due to number of contracts 2024-05-29 11:17:15 -04:00
4 changed files with 93 additions and 37 deletions

No files matched your search

+23 -16
View File
@@ -20,6 +20,7 @@ using System.Composition;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Diagnostics;
using SecurityKey = System.UInt32;
@@ -112,25 +113,31 @@ namespace bts
}
private void _merge_and_post()
{
Func<SecurityMD, bool> is_active = (sec_md) =>
{
{
// Stopwatch stopwatch = Stopwatch.StartNew();
Func<SecurityMD, bool> is_active = (sec_md) =>
{
var sec = sec_md.security;
return SecurityExpirationHelper.is_unexpired(sec);
};
var to_pub = _server_sec_map.Where(kv => is_active(kv.Value)).ToDictionary(kv => kv.Key, kv => kv.Value);
foreach (var kv in _fake_sec_map)
{
};
var to_pub = _server_sec_map.Where(kv => is_active(kv.Value)).ToDictionary(kv => kv.Key, kv => kv.Value);
foreach (var kv in _fake_sec_map)
{
to_pub[kv.Key] = kv.Value;
}
bts.Log.Gen.info(String.Format("MergedMDDaemon._merge_and_post sz: {0}", to_pub.Count()));
_bus.post(SparkTags.SecuritiesTag, to_pub);
SystemContractData sc_data = new SystemContractData(to_pub);
_bus.post(SparkTags.SystemContractsTag, sc_data);
ExchangeSecurityMap exs_map = new ExchangeSecurityMap(sc_data);
// backwards compat for TC2
_bus.post(SparkTags.TC2Tags.ExchangeSecurityMapTag, exs_map);
}
}
// look here
bts.Log.Gen.info(String.Format("MergedMDDaemon._merge_and_post sz: {0}", to_pub.Count()));
_bus.post(SparkTags.SecuritiesTag, to_pub);
SystemContractData sc_data = new SystemContractData(to_pub);
// _bus.post(SparkTags.SystemContractsTag, sc_data);
ExchangeSecurityMap exs_map = new ExchangeSecurityMap(sc_data);
// backwards compat for TC2
_bus.post(SparkTags.TC2Tags.ExchangeSecurityMapTag, exs_map);
// stopwatch.Stop();
// bts.Log.Gen.info($"MergedMDDaemon._merge_and_post took {stopwatch.ElapsedMilliseconds} ms");
}
public void Dispose() =>
Disposable.dispose(ref _dispose);
@@ -30,7 +30,7 @@ namespace mbts.spark {
using System.Windows;
using System.Windows.Media;
[Export(typeof(IShellModule))]
// [Export(typeof(IShellModule))]
class AnalyticsShellModule : IShellModule, IShellModuleViewFactory {
readonly IBus _bus;
readonly IWindowManager _wm;
@@ -83,12 +83,18 @@ namespace mbts.spark {
ulong _prices_updated;
NativePriceSize[] _bids = new NativePriceSize[NativePriceHelpers.COCONUT_MAX_DEPTH], _asks = new NativePriceSize[NativePriceHelpers.COCONUT_MAX_DEPTH];
bool _price_dirty = false, _piq_dirty = false;
LadderPriceList _price_list = new() {
length = _initial_row_count,
};
LadderPriceList _price_list_backing;
LadderPriceList _price_list=>_price_list_backing ??= new LadderPriceList {
length = _initial_row_count,
security = security
};
LadderPriceList _pending_price_list = null;
IComparer<double> _price_comparer;
double _last_origin_price;
IComparer<double> _price_comparer_backing;
IComparer<double> _price_comparer => _price_comparer_backing
??= Comparer<double>.Create((x, y) => _spec.tick_equals(_normalize_price(x), _normalize_price(y)) ? 0 : y.CompareTo(x));
double _last_origin_price;
readonly MarketBreakLines _mkt_break_lines;
NativePriceSize _last_trade;
Func<NativePriceSize, string> _fmt_mkt_depth_item_impl = MDDisplayHelper.get_formatter_nps(
@@ -296,7 +302,7 @@ namespace mbts.spark {
nameof(TopTick),
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => {
if (o._price_list is null) {
if (o._price_list_backing is null) {
return;
}
o._set_scrollbar_range(high_tick: v);
@@ -591,7 +597,7 @@ namespace mbts.spark {
busy = false;
void ensure_tick_padding() {
if (_price_list is null || _settings is null) {
if (_price_list_backing is null || _settings is null) {
return;
}
var pct = _scroll_viewer.VerticalOffset / _scroll_viewer.ScrollableHeight;
@@ -950,7 +956,6 @@ namespace mbts.spark {
// this serves as a redundancy/fail-safe; it ensures that the vm's security's
// contract spec will be used, even if the settings sequence is empty.
_invalidate_spec();
_price_comparer = Comparer<double>.Create((x, y) => _spec.tick_equals(_normalize_price(x), _normalize_price(y)) ? 0 : y.CompareTo(x));
_price_dirty = true;
_piq_dirty = true;
// **use local vm to protect against re-assignment,
@@ -1047,7 +1052,7 @@ namespace mbts.spark {
#endregion
void _refresh_frozen_mkt_lines() {
if (_price_list is null)
if (_price_list_backing is null)
return;
_mkt_break_lines.refresh_frozen();
_refresh_bid_mkt_break();
@@ -1109,9 +1114,9 @@ namespace mbts.spark {
bool last_trade_changed = _vm.md.get_last_trade(ref unused, out _last_trade, out DateTime _, out ulong __);
bool depth_changed = _vm.md.get_depth(ref _prices_updated, ref _bids, ref _asks, NativePriceHelpers.COCONUT_MAX_DEPTH);
_mkt_break_lines.refresh();
if (_price_list is null) {
if (_price_list_backing is null) {
Log.assert(_price_dirty);
_price_list = new LadderPriceList {
_price_list_backing = new LadderPriceList {
length = _initial_row_count,
security = security
};
@@ -1126,9 +1131,9 @@ namespace mbts.spark {
_price_dirty = true;
}
if (_price_dirty) {
if (_pending_price_list != null && !_pending_price_list.Equals(_price_list)) {
var prev_price_list = _price_list;
_price_list = _pending_price_list;
if (_pending_price_list != null && !_pending_price_list.Equals(_price_list_backing)) {
var prev_price_list = _price_list_backing;
_price_list_backing = _pending_price_list;
_pending_price_list = null;
if (prev_price_list.Count != _price_list.Count) {
_reset_height();
@@ -1359,11 +1364,6 @@ namespace mbts.spark {
void _clear_visuals() {
if (_settings == null)
return;
if (_price_list is null) {
_price_list = new LadderPriceList {
security = security
};
}
_price_list.reset(null, null, _initial_row_count);
Array.Clear(_bids, 0, _bids.Length);
Array.Clear(_asks, 0, _asks.Length);
@@ -1429,8 +1429,11 @@ namespace mbts.spark {
LadderRender.render_single_grid_line(vis, row, _settings.row_height, color, _price_column.x_left, _settings.price_column.width);
#endregion
void _invalidate_spec() =>
void _invalidate_spec(){
_spec_backing = null;
_price_comparer_backing = null;
_price_list_backing = null;
}
void _settings_updated(LadderSettings s) {
double prev_row_height;
+46
View File
@@ -8,6 +8,7 @@ using System.Collections.Generic;
using System.Composition;
using System.Reactive.Subjects;
// using System.Diagnostics;
namespace bts.core {
[Export(typeof(IBus))]
public class SimpleBus : IBus {
@@ -28,10 +29,55 @@ namespace bts.core {
return result;
}
}
// class TimedSubscriptionCallbackWrapper<T> : ISubject<T> {
// readonly ISubject<T> subject;
// public TimedSubscriptionCallbackWrapper(ISubject<T> subject) =>
// this.subject = subject;
// public void OnCompleted() =>
// subject.OnCompleted();
// public void OnError(Exception error) =>
// subject.OnError(error);
// public void OnNext(T value) =>
// subject.OnNext(value);
// public IDisposable Subscribe(IObserver<T> observer) {
// return subject.Subscribe(onNext, observer.OnError, observer.OnCompleted);
// void onNext(T value) {
// var timer = Stopwatch.StartNew();
// observer.OnNext(value);
// timer.Stop();
// bts.Log.Gen.info($"SimpleBus.onNext took {timer.ElapsedMilliseconds} ms {value}");
// }
// }
// }
// private ISubject<T> subject<T>(object tag) {
// var inst = SubjectSingleton<T>.instance;
// lock (inst) {
// if (inst.TryGetValue(tag, out ISubject<T> result)) {
// return result;
// }
// Log.Ui.info($"SimpleBus, publishing '{tag}' for the first time");
// result = new ReplaySubject<T>(1);
// if (tag == mbts.spark.SparkTags.SystemContractsTag) {
// result = new TimedSubscriptionCallbackWrapper<T>(result);
// }
// inst[tag] = result;
// return result;
// }
// }
public void post<T>(ITag<T> tag, T data) {
try {
// Stopwatch stopwatch = Stopwatch.StartNew();
subject<T>(tag).OnNext(data);
// stopwatch.Stop();
// if(stopwatch.ElapsedMilliseconds > 50)
// bts.Log.Gen.info($"SimpleBus.post took {stopwatch.ElapsedMilliseconds} ms {tag} {data}");
// TODO: for debugging purposes, remove it later
if (tag != mbts.spark.SparkTags.SparkTimer) {
var tid = System.Threading.Thread.CurrentThread.ManagedThreadId;