evidence-dev / evidence

Business intelligence as code: build fast, interactive data visualizations in pure SQL and markdown
https://evidence.dev
MIT License
3.68k stars 181 forks source link

Faster Dev Server Boot #1564

Open mcrascal opened 5 months ago

mcrascal commented 5 months ago

npm run dev takes a lot longer to get to a live app than a bare sveltekit app does. In addition to #922, improving this would do a lot for the initial experience of trying out evidence, and for my own day to day experience of using it.

csjh commented 4 months ago

Some profiling suggests a major cause is large files in dependencies taking up a lot of time in a vite function called ssrTransformScript (~4.1s). Main perpetrators:

image image image

There's a few other fat time sinks like svelte file compilation (~1.7s) - 170 files are compiled when the dev server is started, so we should figure out if there's a way to avoid that.

archiewood commented 4 months ago

Can't actually see these images, Non-Image content-type returned but I assume they are inadvertent additions?

csjh commented 4 months ago

Whoops, updated

hughess commented 4 months ago

I'm looking through vite config for something else and stumbled upon this warmup option - sharing in case it's useful here: https://vitejs.dev/config/server-options.html#server-warmup

csjh commented 4 months ago

Yeah seems like that helps move the heavy bits to the start of the dev server instead of later in, but the end result is the same

archiewood commented 4 months ago

@csjh what was your rough profiling setup to get these measurements?

csjh commented 4 months ago

@archiewood I've been modifying node_modules/vite/bin/vite.js because their --profile flag wasn't working 100% correctly for me

Basically just go in that file and replace

  session.post('Profiler.enable', () => {
    session.post('Profiler.start', start)
  })

(located near the bottom) with

  session.post('Profiler.enable', () => {
    session.post('Profiler.start', start)
    setTimeout(() => {
        session.post('Profiler.stop', async (err, { profile }) => {
            if (err) {
                console.error(err)
                process.exit(1)
            }
            const fs = await import('node:fs')
            const path = await import('node:path')
            const filename = path.resolve(process.cwd(), 'v8-profile.cpuprofile')
            fs.writeFileSync(filename, JSON.stringify(profile))
            console.log(`Profile written to ${filename}`)
            process.exit(0)
        })
    }, 20000);
  })

and run vite with the --profile flag after the .cpuprofile can be loaded into chrome