Coder Social home page Coder Social logo

Comments (14)

cristianoc avatar cristianoc commented on June 16, 2024 5

This is the generated code after DCE:

// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE


var Raw = { };

var MyQuery = {
  Raw: Raw
};

export {
  MyQuery ,
  
}
/* No side effect */

from reanalyze.

jfrolich avatar jfrolich commented on June 16, 2024 1

I will follow this up in the 1.0 branch.

from reanalyze.

cristianoc avatar cristianoc commented on June 16, 2024

@jfrolich here's the code annotated dead by the analysis.
Looks like there are a couple of things not considered dead yet which could (e.g. serializeVariables). Good test, I can look into why.

// generated code by the PPX
module MyQuery = {
  module Raw = {
    type t = {scalarsInput: string};
  };
  [@dead "MyQuery.query"] let query = "query ($arg: VariousScalarsInput!)  {\nscalarsInput(arg: $arg)  \n}\n";
  type t = {scalarsInput: string};
  type t_variables = {arg: t_variables_VariousScalarsInput}
  and t_variables_VariousScalarsInput = {
    nullableString: option(string),
    string,
    nullableInt: option(int),
    int,
    nullableFloat: option(float),
    float,
    nullableBoolean: option(bool),
    boolean: bool,
    nullableID: option(string),
    id: string,
  };
  [@dead "MyQuery.parse"] let parse: Raw.t => t =
    (value) => (
      {

        scalarsInput: {
          let value = (value: Raw.t).scalarsInput;

          value;
        },
      }: t
    );
  [@dead "MyQuery.serialize"] let serialize: t => Raw.t =
    (value) => (
      {
        let scalarsInput = {
          let value = (value: t).scalarsInput;

          value;
        };
        {

          scalarsInput: scalarsInput,
        };
      }: Raw.t
    );
  let rec serializeVariables: t_variables => Js.Json.t =
    inp =>
      [|
        (
          "arg",
          (a => Some(serializeInputObjectVariousScalarsInput(a)))(inp.arg),
        ),
      |]
      |> Js.Array.filter(
           fun
           | (_, None) => false
           | (_, Some(_)) => true,
         )
      |> Js.Array.map(
           fun
           | (k, Some(v)) => (k, v)
           | (k, None) => (k, Js.Json.null),
         )
      |> Js.Dict.fromArray
      |> Js.Json.object_
  and serializeInputObjectVariousScalarsInput:
    t_variables_VariousScalarsInput => Js.Json.t =
    inp =>
      [|
        (
          "nullableString",
          (
            a =>
              switch (a) {
              | None => None
              | Some(b) => (a => Some(Js.Json.string(a)))(b)
              }
          )(
            inp.nullableString,
          ),
        ),
        ("string", (a => Some(Js.Json.string(a)))(inp.string)),
        (
          "nullableInt",
          (
            a =>
              switch (a) {
              | None => None
              | Some(b) => (a => Some(Js.Json.number(float_of_int(a))))(b)
              }
          )(
            inp.nullableInt,
          ),
        ),
        ("int", (a => Some(Js.Json.number(float_of_int(a))))(inp.int)),
        (
          "nullableFloat",
          (
            a =>
              switch (a) {
              | None => None
              | Some(b) => (a => Some(Js.Json.number(a)))(b)
              }
          )(
            inp.nullableFloat,
          ),
        ),
        ("float", (a => Some(Js.Json.number(a)))(inp.float)),
        (
          "nullableBoolean",
          (
            a =>
              switch (a) {
              | None => None
              | Some(b) => (a => Some(Js.Json.boolean(a)))(b)
              }
          )(
            inp.nullableBoolean,
          ),
        ),
        ("boolean", (a => Some(Js.Json.boolean(a)))(inp.boolean)),
        (
          "nullableID",
          (
            a =>
              switch (a) {
              | None => None
              | Some(b) => (a => Some(Js.Json.string(a)))(b)
              }
          )(
            inp.nullableID,
          ),
        ),
        ("id", (a => Some(Js.Json.string(a)))(inp.id)),
      |]
      |> Js.Array.filter(
           fun
           | (_, None) => false
           | (_, Some(_)) => true,
         )
      |> Js.Array.map(
           fun
           | (k, Some(v)) => (k, v)
           | (k, None) => (k, Js.Json.null),
         )
      |> Js.Dict.fromArray
      |> Js.Json.object_;
  let makeVar = (~f, ~arg, ()) =>
    f(
      serializeVariables(
        {

          arg: arg,
        }: t_variables,
      ),
    )
  [@dead "MyQuery.makeInputObjectVariousScalarsInput"] and makeInputObjectVariousScalarsInput =
      (
        ~nullableString=?,
        ~string,
        ~nullableInt=?,
        ~int,
        ~nullableFloat=?,
        ~float,
        ~nullableBoolean=?,
        ~boolean,
        ~nullableID=?,
        ~id,
        (),
      )
      : t_variables_VariousScalarsInput => {

    nullableString,

    string,

    nullableInt,

    int,

    nullableFloat,

    float,

    nullableBoolean,

    boolean,

    nullableID,

    id,
  };
  [@dead "MyQuery.definition"] let definition = (parse, query, makeVar, serialize);
  let makeVariables = makeVar(~f=f => f);
};
// end of generated code by the ppx

from reanalyze.

cristianoc avatar cristianoc commented on June 16, 2024

So it looks like this is what prevents the final bit of elimination:

Warning Dead Value With Side Effects
  /Users/cristianoc/reasonml/reanalyze/examples/deadcode/src/B.re 194:3-40
  MyQuery.makeVariables is never used and could have side effects

from reanalyze.

cristianoc avatar cristianoc commented on June 16, 2024

This change would allow the full dead code analysis to fire:

  let makeVariables = (~arg) => makeVar( ~f=f => f, arg);

Or, using uncurried application would have forced one into this anyway.

This is one more example of the pitfalls of partial application. In this case, it prevents some analysis optimizaion, and surely a bunch of invisible compiler optimizations are prevented by this.

from reanalyze.

cristianoc avatar cristianoc commented on June 16, 2024

@jfrolich do you have the bandwidth to try and suggest this change upstream in the PPX and follow it through?

This example is on pause at the moment. I'd like to investigate what happens when a subset of the generated code is used. But first I'd like to make sure that if none is used, then everything is eliminated.

from reanalyze.

jfrolich avatar jfrolich commented on June 16, 2024

@jfrolich do you have the bandwidth to try and suggest this change upstream in the PPX and follow it through?

This example is on pause at the moment. I'd like to investigate what happens when a subset of the generated code is used. But first I'd like to make sure that if none is used, then everything is eliminated.

Yes. I think we might be able to eliminate makeVar it was mostly a proof of concept to enable a fancy API surface, but it had other downsides with currying, and to my knowledge it didn't end up implemented in a client. This is exactly the kind of API changes we can make before releasing 1.0 officially.

from reanalyze.

cristianoc avatar cristianoc commented on June 16, 2024

@jfrolich any other changes to report for gql ppx 1.0?

from reanalyze.

jfrolich avatar jfrolich commented on June 16, 2024

Yes there will be quite a few changes. I am looking to make the definition a record instead of a tuple (not sure yet about that though). And there is quite a list of things to be done. I made a list here: teamwalnut/graphql-ppx#86, but it is not exhaustive. Anything particular that you are interested about?

from reanalyze.

cristianoc avatar cristianoc commented on June 16, 2024

Build speed comes to mind! We ran some benchmarks lately, and the ppx can really make a difference.

from reanalyze.

jfrolich avatar jfrolich commented on June 16, 2024

Yes. I think there is much room to improve indeed! I yet have to look at performance, still some features to complete, but after that I will dive a bit deeper to improve the performance!

from reanalyze.

cristianoc avatar cristianoc commented on June 16, 2024

@jfrolich I was curious about progress and took a look at teamwalnut/graphql-ppx#86 . Wow lots of activity there!

from reanalyze.

jfrolich avatar jfrolich commented on June 16, 2024

Yes working towards a 1.0 release. We want it to be great!

from reanalyze.

cristianoc avatar cristianoc commented on June 16, 2024

Looks like this is going to work in the next release.
Please reopen if there are any new issues.

from reanalyze.

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.