Code Monkey home page Code Monkey logo

Comments (2)

leebyron avatar leebyron commented on May 5, 2024

I think there is missing information in order to do this programmatically, but also some bad corner cases to solve.

The primary question here - the missing information - is how long photoLoader should wait to run it's batch. To avoid latency, DataLoader's answer to this question is "until the call stack unwinds", but we could make a different decision if we wanted to. Maybe the right thing to do if batching is really that valuable is always add 100ms of latency because in aggregate the opportunity to batch will make that latency worth it. Waiting 100ms maybe catches the second call to photoLoader after userLoader returns.

That's probably not the best we can do in this scenario though. Adding latency atop an already expensive operation is probably not the best user experience, and maybe userLoader only takes 2ms so 100ms of latency was an awful choice, or maybe it takes 102ms in which case we spent the cost but got no benefit. Also, this mechanism would have us wait another 100ms after the second call to photoLoader just in case there are any other calls, even though we know there won't be based on reading this example, so it's more wasted latency.

Another way to solve this might be explicitly declaring that you want to wait for another call to load() but you just don't have the ids yet. In this example we could maybe declare that the call is coming. DataLoader just cares about what in frame of execution the call to load() occurs. If we can ensure that something is called on the loader instance in that frame, then we have enough info to know to wait to coalesce a batch.

Maybe: waitFor(Promise<any>): void

async function loadUserPhotos(userId) {
  let userPromise = userLoader.load(userId);
  photoLoader.waitFor(userPromise);
  let user = await userPromise;
  let photos = await photoLoader.loadMany(user.photoIds);
  return photos;
}

Or maybe: load(K | Promise<K>): Promise<V>

async function loadUserPhotos(userId) {
  let photos = await photoLoader.loadMany(loadUserPhotoIDs(userId));
  return photos;
}

async loadUserPhotoIDs(userId) {
  let user = await userLoader.load(userId);
  return user.photoIds;
}

Thoughts?

from dataloader.

sophiebits avatar sophiebits commented on May 5, 2024

(Now I don't remember what actual situation I was pondering that prompted me to file this issue.)

I like the waitFor idea, though I wonder if that requires a breach of encapsulation in the general case. The photo loader might live at a relatively low level of the stack, but this sequence of fetches might be coordinated at a higher level.

The load(promise) API feels less intuitive and clunkier at a first glance, but it might have the potential to accommodate this use case if this structure was idiomatic and fetching was always performed bottom-up. Ultimately it seems like this also can't work in every case because your first fetch could determine whether you want to do a second fetch at all.

Both good ideas though; thank you! I suppose there's probably no magic bullet. :)

from dataloader.

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.