Show HN: Switchboard – Dependency Injection for Unity, Log Files, Zero GC https://ift.tt/sKAnSLd
Show HN: Switchboard – Dependency Injection for Unity, Log Files, Zero GC I've been a professional game engineer for over 10 years, working with Unity for most of that time. Around when the pandemic started, I was at a point where I could start a new version of an existing project, rebuilding everything from scratch. So, I investigated this "Dependency Injection" concept I had heard about, and how it could be better than using singletons. If you've worked with Unity you know that singletons are prolific in that environment. You mostly create scripts derived from MonoBehaviours that get instantiated for you, so there is no opportunity for constructor injection. Other dependency injection frameworks for Unity mostly mirror the way it's done in other environments, and end up using reflection to get the job done in Unity. To make a long story short, I ended up creating a novel design pattern (correct me if I'm wrong) that I call the Injector Locator Pattern. It accomplishes inversion of control in Unity without any use of reflection. The first service I thought to inject was a logger. Unity, especially when I started, did not have good log files. The performance is poor, and it generates a lot of garbage memory allocations. To make yet another long story short, I ended up creating a replacement for StringBuilder that does not allocate garbage memory when appending numerical values. (StringBuilder still does that in Unity 2021/2022.) Also, I realized that .NET literally cannot render the exact value of certain floating-point numbers. So, I made sure that you can render any float or double to any degree of precision, formatted however you like. Even the values displayed in Visual Studio's debugger are wrong! Anyway, you can essentially use the standard .NET ILogger interface, in Unity, loosely coupled and injected, and write thousands of log entries in a fraction of a second that get written out to UTF-8 log files, with zero GC allocations at any point along the way. Please let me know what you think of my Injector Locator Pattern, my replacement for StringBuilder, and anything else. Thanks! https://ift.tt/DThMt2b October 24, 2023 at 09:53PM
Comments
Post a Comment