Skip to content

Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.

1.1 KB
Raw Blame
// Code generated by fast-anything. DO NOT EDIT. Profiles or records one run; removed afterwards.
package main

import (
	"expvar"
	"os"
	"runtime/pprof"
	"strconv"
	"sync"
	"time"
)

var (
	fastAnythingProfFile *os.File
	fastAnythingDoneOnce sync.Once
)

func init() {
	if path := os.Getenv("FAST_ANYTHING_PPROF"); path != "" {
		if f, err := os.Create(path); err == nil && pprof.StartCPUProfile(f) == nil {
			fastAnythingProfFile = f
		}
	}
	// scan --duration: a server or GUI runs until closed, so end it after that long, profile saved
	if ms, err := strconv.Atoi(os.Getenv("FAST_ANYTHING_STOP_AFTER_MS")); err == nil && ms > 0 {
		time.AfterFunc(time.Duration(ms)*time.Millisecond, func() {
			fastAnythingDone()
			os.Exit(0)
		})
	}
}

// Deferred at the top of main: stops the profile and saves recordings when main returns.
func fastAnythingDone() {
	fastAnythingDoneOnce.Do(func() {
		if fastAnythingProfFile != nil {
			pprof.StopCPUProfile()
			fastAnythingProfFile.Close()
			fastAnythingProfFile = nil
		}
		if save, ok := expvar.Get("fast_anything_save").(expvar.Func); ok {
			save()
		}
	})
}