Prompt
Reduce garbage collection pressure and improve application performance by avoiding unnecessary memory allocations. This significantly impacts overall system responsiveness, especially for high-throughput services.
Key practices to follow:
- Use shared buffers and specialized APIs - Prefer operations that leverage pre-allocated memory:
// Instead of this (creates allocations): var binaryData = ModelReaderWriter.Write(model); return RequestContent.Create(binaryData); // Use this (leverages shared buffers): return BinaryContent.Create(model); // Uses UnsafeBufferSequence internally - Work directly with Spans - Avoid copying when operating on memory regions:
// Instead of this (allocates new array): json.WriteString("certificate", Convert.ToBase64String(data.ToArray())); // Use this (avoids allocation): json.WriteString("certificate", Convert.ToBase64String(data.Span)); - Cache infrequently-changing data - Balance freshness with performance:
// Cache agent tools to avoid API calls on every request if (_agentTools is null) { PersistentAgent agent = await _client.Administration.GetAgentAsync(_agentId); _agentTools = agent.Tools; } - Use streaming APIs for large data - Process data incrementally instead of loading everything into memory:
// Instead of loading the whole stream: var allProfile = BinaryData.FromStream(stream).ToObjectFromJson<Dictionary<string, Dictionary<string, object>>>(); // Use streaming deserialization: var allProfile = await JsonSerializer.DeserializeAsync<Dictionary<string, Dictionary<string, JsonElement>>>(stream, options); - Reuse objects for repeated operations rather than creating new instances each time.
These optimizations are particularly important in high-throughput scenarios where allocation pressure can cause frequent garbage collections, leading to performance degradation.