Code Monkey home page Code Monkey logo

Comments (3)

cjheppell-ravio avatar cjheppell-ravio commented on June 11, 2024

Update: This appears to not be related to applyTransform or loadOne at all. There's a simpler repro here with just each: https://github.com/scottravio/grafast-memory-spike-repro

from crystal.

benjie avatar benjie commented on June 11, 2024

This is due to the each() creating a subroutine (since it's not used as the return result of a plan resolver, but instead as input to another step), and subroutines are currently not hoisted (see #2041); as such we're having to run the each transform with a batch size of 1000 (because you're fetching 1000 users, and this is a field in the User type), and for each of those 1,000 user we're then mapping over the 1000 languages ultimately resulting in 1000*1000 = 1,000,000 values.

Since your step here is not dependent on $user, theoretically we should be able to hoist it (see #2041) but we currently do not. Instead, try and ensure that the steps that you use can all be hoisted (see the plan diagram to see which bucket the steps exist in); for example:

    plans: {
      User: {
        totalLanguages() {
          const $allLangugages = withPgClient(
            executor,
            constant(null),
            async (pgClient) => {
              const { rows } = await pgClient.query({
                text: `select language from languages`,
              });
              return rows;
            },
          );
          $allLangugages.hasSideEffects = false;
          return lambda(
            $allLangugages,
            (allLangugages) => allLangugages.length,
          );
        },
      },
    },

However, it would be better to directly address your need, which in this case seems to be counting the number of languages on a per-user basis, so perhaps something like:

    plans: {
      User: {
        totalLanguages($user) {
          const $agg = user_config.find({ user_id: $user.get("id") }).clone("aggregate");
          return $agg.single().select(sql`count(*)`, TYPES.bigint);
        },
      },
    },

Note that .clone is currently marked as an internal API, but that is resolved in #2039.

from crystal.

benjie avatar benjie commented on June 11, 2024

Closing this issue in favour of the related issues #2039, #2040 and #2041.

from crystal.

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.