Closed vgamed closed 4 years ago
Unfortunately I think this is unavoidable with the Promise pattern. Potentially you could reduce GC allocations by using predefined methods instead of anonymous methods for your promise callbacks, although as the article you linked mentioned, the method references will be allocated on the heap.
Completely avoiding GC allocations was never a design goal of this library. A typical use-case for this would be to do something like download some data from a web request or asynchronously write a file to disk, in which case your bottleneck will pretty much always be IO. For tasks that run every frame, promises probably aren't the best pattern to use.
http://www.what-could-possibly-go-wrong.com/promises-for-game-development/ I found your library because this article mentioned its usage in loading game assets. So I was wondering if it's applicable for loading game assets dynamically. Yes, I agree with you that completely avoiding GC allocation is impossible. Just for some low-level devices, for example, memory below to 1G bytes, the developer has to deal with GC carefully. By the way, I do not quite get the point that every time the calling of Then() or Done(), there will be a new Promise allocated for the return value. Why should not just use the origin Promise object? It's already designed to store the handlers as Lists, right? From what I understand, Then and Done are just elegant ways to register callbacks. But I don't see the reason that we should register them in different Promises. They are all for the first promise in fact, aren't they?
It is necessary to return a new promise every time you call Then()
because it's possible to assign multiple callbacks to the same promise:
// In this example, the text "Callback 1" and "Callback 2" will both be printed as soon as
// the promise returned by Fetch resolves.
var w = Fetch("https://github.com");
w.Then(() => Console.WriteLine("Callback 1"));
w.Then(() => Console.WriteLine("Callback 2"));
// Note that it would be possible to chain different things to the promises returned by the
// two invocations of Then.
In the case of asset loading, I probably wouldn't be too concerned about the garbage allocated by a promise because it will most likely be negligible compared to the memory allocated for the assets themselves, although depending on your use-case your mileage may vary.
I notice there might be garbage collectings caused by closures. For example, the object resultPromise in method Promise.Then() is used in a lambda expression later. Reference: https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity4-1.html, the part about closure and anonymous method.