public void Test1() { StringBuffer buf = new StringBuffer(100); Assert.AreEqual(0, buf.Length); Assert.AreEqual("", buf.ToString()); buf.Append('a'); buf.Append('b'); buf.Append('c'); Assert.AreEqual(3, buf.Length); Assert.AreEqual("abc", buf.ToString()); buf.Clear(); Assert.AreEqual(0, buf.Length); Assert.AreEqual("", buf.ToString()); }
/// <inheritdoc /> public override void OnReceivedBytes(bool isNewConnection, ArraySegment <byte> receivedBytes) { receivedBytes.MustNotBeDefault(nameof(receivedBytes)); // Clear receive buffer on new connections if (isNewConnection) { _receiveStringBuffer.Clear(); _decoder.Reset(); } // Parse characters if (receivedBytes.Count == 0) { return; } var addedChars = _receiveStringBuffer.Append(receivedBytes, _decoder); if (addedChars == 0) { return; } while (_receiveStringBuffer.Count > 0) { // Check for start symbol if (_receiveStringBuffer[0] != SYMBOL_START) { throw new MessageRecognitionException($"Error during message recognition. Expected '{SYMBOL_START}' at the start of a message, got '{_receiveStringBuffer[0]}'!"); } // Search delimiter var delimiterIndex = -1; for (var loop = 1; loop < _receiveStringBuffer.Count; loop++) { if (_receiveStringBuffer[loop] == SYMBOL_DELIMITER) { delimiterIndex = loop; break; } if (!TcpCommunicatorUtil.IsNumeric(_receiveStringBuffer[loop])) { throw new MessageRecognitionException($"Error during message recognition. Symbol in length field is not numeric (current index: {loop})!"); } if (loop > 11) { throw new MessageRecognitionException($"Error during message recognition. Length field too long (current index: {loop})!"); } } if (delimiterIndex == -1) { break; } // Parse message count int rawMessageLength; try { rawMessageLength = TcpCommunicatorUtil.ParseInt32FromStringPart( _receiveStringBuffer, 1, delimiterIndex - 1); } catch (Exception ex) { throw new MessageRecognitionException($"Unable to parse message length: {ex.Message}"); } // Look whether we've received the full message var fullMessageLength = delimiterIndex + rawMessageLength + 2; if (_receiveStringBuffer.Count < fullMessageLength) { break; } // Check endsymbol if (_receiveStringBuffer[fullMessageLength - 1] != SYMBOL_END) { throw new MessageRecognitionException($"Error during message recognition. Expected '{SYMBOL_END}' at the end of a message, got '{_receiveStringBuffer[fullMessageLength - 1]}'!"); } // Raise found message base.NotifyRecognizedMessage(_receiveStringBuffer.GetPartReadOnly( delimiterIndex + 1, rawMessageLength)); // Remove the message with endsymbols from receive buffer _receiveStringBuffer.RemoveFromStart(fullMessageLength); } }
public void NoAllocation() { _sb2.AppendFormat(formatTest, v1, v2); _sb2.CopyTo(DestNative, 0, _sb2.Count); _sb2.Clear(); }
public void SerializationPerformanceThreaded() { const int kCount = 100000; // Create kCount entities and assign some arbitrary component data for (var i = 0; i < kCount; ++i) { var entity = m_Manager.CreateEntity(typeof(TestComponent), typeof(TestComponent2), typeof(MathComponent), typeof(BlitComponent)); var comp = m_Manager.GetComponentData <BlitComponent>(entity); comp.blit.x = 123f; comp.blit.y = 456.789; comp.blit.z = -12; comp.flt = 0.01f; m_Manager.SetComponentData(entity, comp); } using (var entities = m_Manager.GetAllEntities()) { // Since we are testing raw serialization performance we rre warm the property type bag // This builds a property tree for each type // This is done on demand for newly discovered types // @NOTE This json string will also be used to debug the size for a single entity var json = JsonSerializer.SerializeStruct(new EntityContainer(m_Manager, entities[0])); var totalTimer = new Stopwatch(); totalTimer.Start(); var numThreads = Math.Max(1, Environment.ProcessorCount - 1); var threadCount = numThreads; var countPerThread = entities.Length / threadCount + 1; var threads = new Thread[threadCount]; // Split the workload 'evenly' across numThreads (IJobParallelForBatch) for (int begin = 0, index = 0; begin < entities.Length; begin += countPerThread, index++) { var context = new WorkerThreadContext { Entities = entities, StartIndex = begin, EndIndex = Mathf.Min(begin + countPerThread, entities.Length) }; var thread = new Thread(obj => { var buffer = new StringBuffer(4096); var visitor = new JsonVisitor { StringBuffer = buffer }; var c = (WorkerThreadContext)obj; for (int p = c.StartIndex, end = c.EndIndex; p < end; p++) { var entity = c.Entities[p]; JsonSerializer.SerializeStruct(new EntityContainer(m_Manager, entity), visitor); // @NOTE at this point we can call Write(buffer.Buffer, 0, buffer.Length) buffer.Clear(); } }) { IsBackground = true }; thread.Start(context); threads[index] = thread; } foreach (var thread in threads) { thread.Join(); } totalTimer.Stop(); Debug.Log($"Serialized {kCount} entities in {totalTimer.Elapsed}. Size per entity = {json.Length} bytes, total size = {json.Length * kCount} bytes"); } }
private static void FormatLogMessage(StringBuffer stringBuffer, IInternalLogEvent logEvent) { stringBuffer.Clear(); logEvent.WriteToStringBuffer(stringBuffer); }
void UpdateBlockInfoString(BlockID block) { if (selIndex < 27 && blocksTable[selIndex] != Block.Bedrock) { int index = 0; buffer.Clear(); buffer.Append(ref index, "&f"); string value = game.BlockInfo.Name[block]; if (blocksTable[selIndex] != Block.Air) { buffer.Append(ref index, value); } else { if (!Mode.survivalinventory.iscrafting && !Mode.survivalinventory.ischest) { buffer.Append(ref index, "Inventory"); } else if (Mode.survivalinventory.iscrafting && !Mode.survivalinventory.ischest) { buffer.Append(ref index, "Crafting"); } else if (!Mode.survivalinventory.iscrafting && Mode.survivalinventory.ischest) { buffer.Append(ref index, "Chest"); } } if (blocksTable[selIndex] != Block.Air) { if (!Mode.survivalinventory.iscrafting && !Mode.survivalinventory.ischest) { buffer.Append(ref index, " (Quantity: "); buffer.Append(ref index, Mode.survivalinventory.inv2Count[selIndex + 1] + ")"); buffer.Append(ref index, " (Durability: "); buffer.Append(ref index, Mode.survivalinventory.inv2durability[selIndex + 1] + ")"); } else if (!Mode.survivalinventory.iscrafting && Mode.survivalinventory.ischest) { buffer.Append(ref index, " (Quantity: "); buffer.Append(ref index, Mode.survivalinventory.chestCount[selIndex + 1] + ")"); buffer.Append(ref index, " (Durability: "); buffer.Append(ref index, Mode.survivalinventory.chestdurability[selIndex + 1] + ")"); } else if (Mode.survivalinventory.iscrafting && !Mode.survivalinventory.ischest) { int craftgrid3; int craftgrid4; int craftgrid5; int craftgrid6; int craftgrid7; int craftgrid8; int ix; if (game.UseCPE) { ix = Mode.survivalinventory.blocknb - Mode.survivalinventory.lessblocknbclassic; craftgrid3 = 9; craftgrid4 = 10; craftgrid5 = 11; craftgrid6 = 18; craftgrid7 = 19; craftgrid8 = 20; } else { ix = Mode.survivalinventory.blocknb - Mode.survivalinventory.lessblocknbclassic; craftgrid3 = 9; craftgrid4 = 10; craftgrid5 = 11; craftgrid6 = 18; craftgrid7 = 19; craftgrid8 = 20; } if (selIndex == 0) { selIndex2 = ix + 1; } if (selIndex == 1) { selIndex2 = ix + 2; } if (selIndex == 2) { selIndex2 = ix + 3; } if (selIndex == craftgrid3) { selIndex2 = ix + 4; } if (selIndex == craftgrid4) { selIndex2 = ix + 5; } if (selIndex == craftgrid5) { selIndex2 = ix + 6; } if (selIndex == craftgrid6) { selIndex2 = ix + 7; } if (selIndex == craftgrid7) { selIndex2 = ix + 8; } if (selIndex == craftgrid8) { selIndex2 = ix + 9; } if (selIndex == craftgrid5 + 2) { selIndex2 = ix + 10; } buffer.Append(ref index, " (Quantity: "); buffer.Append(ref index, Mode.survivalinventory.inv2Count[selIndex2] + ")"); buffer.Append(ref index, " (Durability: "); buffer.Append(ref index, Mode.survivalinventory.inv2durability[selIndex2] + ")"); } } } }