Giter VIP home page Giter VIP logo

Comments (3)

RoryDungan avatar RoryDungan commented on June 2, 2024

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.

from c-sharp-promise.

vgamed avatar vgamed commented on June 2, 2024

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?

from c-sharp-promise.

RoryDungan avatar RoryDungan commented on June 2, 2024

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.

from c-sharp-promise.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.