75 lines
1.7 KiB
C#
75 lines
1.7 KiB
C#
|
using System;
|
||
|
using System.Text;
|
||
|
|
||
|
//NOTE: cache for less GC operations
|
||
|
public static class TempBuffer
|
||
|
{
|
||
|
static byte[][] tmp_bufs = new byte[][] { new byte[512], new byte[512], new byte[512] };
|
||
|
static int tmp_buf_counter = 0;
|
||
|
static public int stats_max_buf = 0;
|
||
|
|
||
|
static public byte[] Get(int size)
|
||
|
{
|
||
|
var buf = Get();
|
||
|
if(size > buf.Length)
|
||
|
{
|
||
|
Array.Resize(ref buf, size);
|
||
|
Update(buf);
|
||
|
}
|
||
|
return buf;
|
||
|
}
|
||
|
|
||
|
static byte[] Get()
|
||
|
{
|
||
|
var buf = tmp_bufs[tmp_buf_counter % 3];
|
||
|
++tmp_buf_counter;
|
||
|
return buf;
|
||
|
}
|
||
|
|
||
|
static void Update(byte[] buf)
|
||
|
{
|
||
|
var idx = (tmp_buf_counter-1) % 3;
|
||
|
//for debug
|
||
|
//var curr = tmp_bufs[idx];
|
||
|
//if(curr != buf)
|
||
|
// Log.Debug(curr.Length + " VS " + buf.Length);
|
||
|
tmp_bufs[idx] = buf;
|
||
|
stats_max_buf = stats_max_buf < buf.Length ? buf.Length : stats_max_buf;
|
||
|
}
|
||
|
|
||
|
static public void Cleanup()
|
||
|
{
|
||
|
for(int i = 0; i < tmp_bufs.Length; ++i)
|
||
|
System.Array.Clear(tmp_bufs[i], 0, tmp_bufs[i].Length);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static class TempStringBuffer
|
||
|
{
|
||
|
static public StringBuilder[] tmp_bufs = new StringBuilder[] { new StringBuilder(), new StringBuilder() };
|
||
|
static int tmp_buf_counter = 0;
|
||
|
|
||
|
static public StringBuilder Get()
|
||
|
{
|
||
|
var buf = tmp_bufs[tmp_buf_counter % 2];
|
||
|
buf.Length = 0;
|
||
|
++tmp_buf_counter;
|
||
|
return buf;
|
||
|
}
|
||
|
|
||
|
static public string Concat(string s1, string s2)
|
||
|
{
|
||
|
return Get().Append(s1).Append(s2).ToString();
|
||
|
}
|
||
|
|
||
|
static public string Concat(string s1, string s2, string s3)
|
||
|
{
|
||
|
return Get().Append(s1).Append(s2).Append(s3).ToString();
|
||
|
}
|
||
|
|
||
|
static public string Concat(string s1, string s2, string s3, string s4)
|
||
|
{
|
||
|
return Get().Append(s1).Append(s2).Append(s3).Append(s4).ToString();
|
||
|
}
|
||
|
}
|