UNPKG

bc-code-intelligence-mcp

Version:

BC Code Intelligence MCP Server - Complete Specialist Bundle with AI-driven expert consultation, seamless handoffs, and context-preserving workflows

56 lines (33 loc) 4.48 kB
--- title: "SingleInstance Subscriber Performance Impact" domain: "dean-debug" difficulty: "intermediate" bc_versions: "14+" tags: ["events", "singleinstance", "memory", "performance"] prerequisites: ["event-subscriber-basics", "memory-management"] samples: "samples/singleinstance-subscribers.md" --- # SingleInstance Subscriber Performance Impact ## Overview SingleInstance subscriber codeunits create one instance per session that persists throughout the session lifecycle. This architectural pattern significantly impacts memory usage and performance characteristics compared to non-SingleInstance subscribers. The SingleInstance pattern affects three critical performance areas: memory allocation patterns, initialization overhead, and cross-event state management. Understanding these impacts is essential for designing high-performance event-driven BC solutions. ## Memory Allocation Patterns SingleInstance subscribers consume memory differently than standard subscribers. The codeunit instance remains in memory for the entire session, including all global variables and their allocated memory. This persistence can lead to memory accumulation if not properly managed. Memory usage grows incrementally with each event processed, particularly when global variables store event data or temporary objects. Large global arrays, complex data structures, or cached references compound memory consumption over long-running sessions. The session-scoped lifetime means memory is only released when the user session ends, not after individual event processing completes. This pattern requires careful consideration of global variable usage and explicit cleanup strategies. ## Initialization Performance Impact SingleInstance subscribers incur initialization costs only once per session, during the first event trigger. This front-loaded initialization can improve performance for frequently triggered events but creates unpredictable response times for the initial event. Heavy initialization logic in SingleInstance subscribers delays the first event response while benefiting subsequent events. The initialization timing becomes critical for user-facing operations where response time consistency matters. Complex initialization sequences, external service connections, or large data loading operations amplify the first-event delay. Performance testing must account for both cold-start and warm-running scenarios. ## Cross-Event State Management SingleInstance subscribers can maintain state between event invocations, enabling sophisticated event processing patterns. However, state accumulation introduces performance considerations and potential side effects. Global variables persisting event data across invocations can create unexpected dependencies between unrelated business operations. State accumulation requires explicit cleanup strategies to prevent memory leaks and logical inconsistencies. Cross-event state enables powerful optimization patterns like caching frequently accessed data or maintaining connection pools. These optimizations must be balanced against memory usage and state consistency requirements. ## Performance Optimization Strategies Minimize global variable usage in SingleInstance subscribers to control memory consumption. Use local variables for event-specific data and reserve global variables for truly session-scoped information. Implement explicit cleanup methods for global variables when appropriate. Consider memory usage patterns and implement cleanup at logical business process boundaries rather than after each event. Design initialization sequences to be as lightweight as possible while still providing necessary functionality. Defer heavy initialization until actually needed rather than performing all setup during first event processing. ## Common Performance Issues Memory accumulation from uncleaned global variables leads to session memory growth and potential performance degradation over long sessions. This issue is particularly problematic in high-frequency event scenarios. Excessive initialization overhead creates inconsistent response times, with the first event significantly slower than subsequent events. This pattern can impact user experience in interactive scenarios. State management complexity increases debugging difficulty and can introduce performance bottlenecks when cross-event dependencies create processing delays or conflicts.