Coder Social home page Coder Social logo

Comments (2)

jakeinflab avatar jakeinflab commented on May 28, 2024 1

TL;DR: Declaring SatoriOptions.fonts as a global variable doubled the performance

Upon closer inspection of the code, I discovered a global variable named fontCache that utilizes WeakMap. I added console.log to satori.ts#L57-L62 to check if the cache was indeed being utilized.

satori.ts#L57-L62

  let font: FontLoader
  if (fontCache.has(options.fonts)) {
    font = fontCache.get(options.fonts)
  } else {
    fontCache.set(options.fonts, (font = new FontLoader(options.fonts)))
  }

It turned out, in satori.ts, the cache key was being assigned to the memory address of the SatoriOptions.fonts object.

We had been declaring SatoriOptions.fonts as a local variable, just as the example in README.md suggests.

This made me realize that changing the address of options.fonts with every invocation prevented us from ever utilizing the cache effectively, as it just kept accumulating.

Therefore, I declared SatoriOptions.fonts as a global variable as follows:

AS-IS

export default async function handler(
  request: NextApiRequest,
  response: NextApiResponse
) {
  try {
    const svg = await satori(JSX,
      {
        width: 1200,
        height: 630,
        embedFont: true,
        fonts: [
          // ...
        ]
    );

TO-BE

const options: SatoriOptions = {
  width: 1200,
  height: 630,
  embedFont: true,
  fonts: [
    // ...
  ],
};

export default async function handler(
  request: NextApiRequest,
  response: NextApiResponse
) {
  try {
    const svg = await satori(JSX, options);
    // ...

As a result, the same code was able to handle approximately double the number of requests.

wrk -t10 -c 30 -d30s http://localhost:3000/og/review/41854\?updatedAt\=2024-01-29T14:30:22
Running 30s test @ http://localhost:3000/og/review/41854?updatedAt=2024-01-29T14:30:22
  10 threads and 30 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   992.55ms  273.04ms   1.87s    66.67%
    Req/Sec     4.43      3.97    20.00     70.93%
  891 requests in 30.07s, 318.15MB read
Requests/sec:     29.63
Transfer/sec:     10.58MB
  • AS-IS: 15.95 Requests/sec
  • TO-BE: 29.63 Requests/sec

Performance improvement by roughly 1.86 times

As you can see, this changes no longer make unnecessary calls to addFonts() in the profiling graph.

image

Now, we can see that the sendData() function, used when returning a response, occupies most of the flame graph.

image

Required

  • Modify satori.ts#L57-L62 to use serialized value as key of the map instead of the memory address value of the object
  • ImageResponse provided by @next/og might also have similar issues.

I hope this helps those experiencing performance issues. Thank you.

Regarding this, I'd like to inform @shuding , the author of this code, about the matter. If it's not too much trouble, may I go ahead and make the improvements, then submit them as a Pull Request? @shuding

from satori.

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.