Optimize GC for status info.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#define BUILD
|
||||
|
||||
using Cryville.Common;
|
||||
using Cryville.Common.Buffers;
|
||||
using Cryville.Crtr.Config;
|
||||
using Cryville.Crtr.Event;
|
||||
using Newtonsoft.Json;
|
||||
@@ -8,7 +9,9 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Formatting;
|
||||
using System.Threading;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.SceneManagement;
|
||||
@@ -45,7 +48,9 @@ namespace Cryville.Crtr {
|
||||
|
||||
static bool initialized;
|
||||
static Text logs;
|
||||
Text status;
|
||||
TextMeshProUGUI status;
|
||||
readonly TargetString statusstr = new TargetString();
|
||||
readonly StringBuffer statusbuf = new StringBuffer();
|
||||
|
||||
static Vector2 screenSize;
|
||||
public static Rect hitRect;
|
||||
@@ -81,7 +86,7 @@ namespace Cryville.Crtr {
|
||||
}
|
||||
OnSettingsUpdate();
|
||||
|
||||
status = GameObject.Find("Status").GetComponent<Text>();
|
||||
status = GameObject.Find("Status").GetComponent<TextMeshProUGUI>();
|
||||
|
||||
texHandler = new DownloadHandlerTexture();
|
||||
#if BUILD
|
||||
@@ -107,7 +112,6 @@ namespace Cryville.Crtr {
|
||||
if (texLoader != null) texLoader.Dispose();
|
||||
if (inputProxy != null) inputProxy.Dispose();
|
||||
if (texs != null) foreach (var t in texs) Texture.Destroy(t.Value);
|
||||
Camera.onPostRender -= OnCameraPostRender;
|
||||
GC.Collect();
|
||||
}
|
||||
|
||||
@@ -216,7 +220,6 @@ namespace Cryville.Crtr {
|
||||
}
|
||||
}
|
||||
}
|
||||
string timetext = string.Empty;
|
||||
void LogUpdate() {
|
||||
string _logs = logs.text;
|
||||
Game.MainLogger.Enumerate((level, module, msg) => {
|
||||
@@ -236,7 +239,8 @@ namespace Cryville.Crtr {
|
||||
);
|
||||
});
|
||||
logs.text = _logs.Substring(Mathf.Max(0, _logs.IndexOf('\n', Mathf.Max(0, _logs.Length - 4096))));
|
||||
var sttext = string.Format(
|
||||
statusbuf.Clear();
|
||||
statusbuf.AppendFormat(
|
||||
"FPS: i{0:0} / s{1:0}\nSMem: {2:N0} / {3:N0}\nIMem: {4:N0} / {5:N0}",
|
||||
1 / Time.deltaTime,
|
||||
1 / Time.smoothDeltaTime,
|
||||
@@ -253,25 +257,27 @@ namespace Cryville.Crtr {
|
||||
#endif
|
||||
);
|
||||
if (started) {
|
||||
sttext += string.Format(
|
||||
statusbuf.AppendFormat(
|
||||
"\nStates: c{0} / b{1}",
|
||||
cbus.ActiveStateCount, bbus.ActiveStateCount
|
||||
);
|
||||
sttext += timetext;
|
||||
if (judge != null) sttext += "\n== Scores ==\n" + judge.GetFullFormattedScoreString();
|
||||
statusbuf.AppendFormat(
|
||||
"\nSTime: {0:G17}s {3}\ndATime: {1:+0.0ms;-0.0ms;0} {3}\ndITime: {2:+0.0ms;-0.0ms;0} {3}",
|
||||
cbus.Time,
|
||||
(Game.AudioClient.Position - atime0 - cbus.Time) * 1e3,
|
||||
(inputProxy.GetTimestampAverage() - cbus.Time) * 1e3,
|
||||
forceSyncFrames != 0 ? "(force sync)" : ""
|
||||
);
|
||||
if (judge != null) {
|
||||
statusbuf.Append("\n== Scores ==\n");
|
||||
var fullScoreStr = judge.GetFullFormattedScoreString();
|
||||
statusbuf.Append(fullScoreStr.TrustedAsArray(), 0, fullScoreStr.Length);
|
||||
}
|
||||
}
|
||||
status.text = sttext;
|
||||
}
|
||||
void OnCameraPostRender(Camera cam) {
|
||||
if (!started) return;
|
||||
if (!logEnabled) return;
|
||||
timetext = string.Format(
|
||||
"\nSTime: {0:R}s {3}\ndATime: {1:+0.0ms;-0.0ms;0} {3}\ndITime: {2:+0.0ms;-0.0ms;0} {3}",
|
||||
cbus.Time,
|
||||
(Game.AudioClient.Position - atime0 - cbus.Time) * 1e3,
|
||||
(inputProxy.GetTimestampAverage() - cbus.Time) * 1e3,
|
||||
forceSyncFrames != 0 ? "(force sync)" : ""
|
||||
);
|
||||
statusstr.Length = statusbuf.Count;
|
||||
var arr = statusstr.TrustedAsArray();
|
||||
statusbuf.CopyTo(0, arr, 0, statusbuf.Count);
|
||||
status.SetText(arr, 0, statusbuf.Count);
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -302,7 +308,7 @@ namespace Cryville.Crtr {
|
||||
bool logEnabled = true;
|
||||
public void ToggleLogs() {
|
||||
logs.text = "";
|
||||
status.text = "";
|
||||
status.SetText("");
|
||||
logEnabled = !logEnabled;
|
||||
}
|
||||
|
||||
@@ -330,8 +336,6 @@ namespace Cryville.Crtr {
|
||||
Game.NetworkTaskWorker.SuspendBackgroundTasks();
|
||||
Game.AudioSession = Game.AudioSequencer.NewSession();
|
||||
|
||||
Camera.onPostRender += OnCameraPostRender;
|
||||
|
||||
var hitPlane = new Plane(Vector3.forward, Vector3.zero);
|
||||
var r0 = Camera.main.ViewportPointToRay(new Vector3(0, 0, 1));
|
||||
float dist;
|
||||
|
@@ -205,6 +205,9 @@ namespace Cryville.Crtr {
|
||||
readonly Dictionary<int, string> scoreStringCache = new Dictionary<int, string>();
|
||||
readonly Dictionary<int, PropSrc> scoreStringSrcs = new Dictionary<int, PropSrc>();
|
||||
readonly ArrayPool<byte> scoreStringPool = new ArrayPool<byte>();
|
||||
readonly Dictionary<int, string> scoreFormatCache = new Dictionary<int, string>();
|
||||
readonly TargetString scoreFullStr = new TargetString();
|
||||
readonly StringBuffer scoreFullBuf = new StringBuffer();
|
||||
void InitScores() {
|
||||
foreach (var s in _rs.scores) {
|
||||
var key = s.Key.Key;
|
||||
@@ -217,6 +220,7 @@ namespace Cryville.Crtr {
|
||||
scores.Add(key, s.Value.init);
|
||||
scoreStringCache.Add(scoreStringKeys[key], null);
|
||||
scoreStringSrcs.Add(scoreStringKeys[key], new ScoreStringSrc(scoreStringPool, () => scores[key], scoreDefs[key].format));
|
||||
scoreFormatCache[key] = string.Format("{{0:{0}}}", s.Value.format);
|
||||
}
|
||||
}
|
||||
void InvalidateScore(int key) {
|
||||
@@ -224,28 +228,24 @@ namespace Cryville.Crtr {
|
||||
scoreStringCache[scoreStringKeys[key]] = null;
|
||||
scoreStringSrcs[scoreStringKeys[key]].Invalidate();
|
||||
}
|
||||
string GetScoreString(int key) {
|
||||
var result = scoreStringCache[key];
|
||||
if (result == null) {
|
||||
var rkey = scoreStringKeysRev[key];
|
||||
return scoreStringCache[key] = scores[rkey].ToString(scoreDefs[rkey].format, CultureInfo.InvariantCulture);
|
||||
}
|
||||
else return result;
|
||||
}
|
||||
public bool TryGetScoreSrc(int key, out PropSrc value) {
|
||||
return scoreSrcs.TryGetValue(key, out value);
|
||||
}
|
||||
public bool TryGetScoreStringSrc(int key, out PropSrc value) {
|
||||
return scoreStringSrcs.TryGetValue(key, out value);
|
||||
}
|
||||
public string GetFullFormattedScoreString() {
|
||||
public TargetString GetFullFormattedScoreString() {
|
||||
bool flag = false;
|
||||
string result = "";
|
||||
scoreFullBuf.Clear();
|
||||
foreach (var s in scores.Keys) {
|
||||
result += string.Format(flag ? "\n{0}: {1}" : "{0}: {1}", IdentifierManager.SharedInstance.Retrieve(s), GetScoreString(scoreStringKeys[s]));
|
||||
scoreFullBuf.AppendFormat(flag ? "\n{0}: " : "{0}: ", (string)IdentifierManager.SharedInstance.Retrieve(s));
|
||||
scoreFullBuf.AppendFormat(scoreFormatCache[s], scores[s]);
|
||||
flag = true;
|
||||
}
|
||||
return result;
|
||||
scoreFullStr.Length = scoreFullBuf.Count;
|
||||
var arr = scoreFullStr.TrustedAsArray();
|
||||
scoreFullBuf.CopyTo(0, arr, 0, scoreFullBuf.Count);
|
||||
return scoreFullStr;
|
||||
}
|
||||
class ScoreStringSrc : PropSrc {
|
||||
readonly Func<float> _cb;
|
||||
|
Reference in New Issue
Block a user