Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiled serializer is huged because of serializerState #45

Open
2 tasks done
remidewitte opened this issue Mar 18, 2024 · 4 comments
Open
2 tasks done

Compiled serializer is huged because of serializerState #45

remidewitte opened this issue Mar 18, 2024 · 4 comments

Comments

@remidewitte
Copy link

remidewitte commented Mar 18, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the regression has not already been reported

Last working version

4.20

Stopped working in version

4.3.0

Node.js version

20.x

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

💥 Regression Report

Every compiled schema contains a stringified version of the schema. Issue is when the schema is huged.
Today it almost kills the advantage of compilation.

It comes from the bump to v5.7.0 of fast-json-stringify.
And to be specific if comes from : fastify/fast-json-stringify#617.

It seems that this getState() of the serializer is never used.

Steps to Reproduce

async function main() {
  const app = fastify({
    exposeHeadRoutes: false,
    jsonShorthand: false,
    schemaController: {
      compilersFactory: {
        buildSerializer: writeSerializerFactory,
      },
    },
    logger: {
      level: 'debug'
    }
  });

  addAllSchemas(app);

  await app.register(async api => {
    api.register(... all handlers ...)
  });

  await app.ready().then(() => {
    app.log.info('Ready, all serializer compiled');
    app.close();
  });
}


export const writeSerializerFactory = StandaloneSerializer({
  readMode: false,
  storeFunction(routeOpts, schemaSerializationCode) {
    if (routeOpts.method === 'HEAD') {
      return;
    }
    writeFileSync(generateSerializerFilePath(routeOps), schemaSerializationCode);
  },
});

Expected Behavior

Why have this serializerState ? Is does not seem to be used at all ?

@ocadoret
Copy link

Since only rounding seems to be used in the state, a workaround for me was to make a patch on fast-json-stringify using yarn patch, making the following change in serializer.js :

diff --git a/lib/serializer.js b/lib/serializer.js
index 922be35793cd51c173bde24f992b2d98a172a8ee..fd32a6fea53c6b3c1de49b1216d0c540061acba2 100644
--- a/lib/serializer.js
+++ b/lib/serializer.js
@@ -20,7 +20,7 @@ module.exports = class Serializer {
         this.parseInteger = Math.trunc
         break
     }
-    this._options = options
+    this._options = {rounding: options.rounding}
   }
 
   asInteger (i) {

@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@remidewitte
Copy link
Author

OK, will do.

I guess the PR will be on fast-json-stringify rather than fast-json-stringify-compiler.

@remidewitte
Copy link
Author

Here it is : fastify/fast-json-stringify#704

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants