Skip to content

Commit

Permalink
drop 0.4mb "runtime/pprof" and "runtime/trace" (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 19, 2021
1 parent 187feec commit a0d9dc9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 27 deletions.
36 changes: 9 additions & 27 deletions cmd/esbuild/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"os"
"runtime/debug"
"runtime/pprof"
"runtime/trace"
"strings"
"time"

Expand Down Expand Up @@ -184,50 +182,34 @@ func main() {
// To view a CPU trace, use "go tool trace [file]". Note that the trace
// viewer doesn't work under Windows Subsystem for Linux for some reason.
if traceFile != "" {
f, err := os.Create(traceFile)
if err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create trace file: %s", err.Error()))
if done := createTraceFile(osArgs, traceFile); done == nil {
return
} else {
defer done()
}
defer f.Close()
trace.Start(f)
defer trace.Stop()
}

// To view a heap trace, use "go tool pprof [file]" and type "top". You can
// also drop it into https://speedscope.app and use the "left heavy" or
// "sandwich" view modes.
if heapFile != "" {
f, err := os.Create(heapFile)
if err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create heap file: %s", err.Error()))
if done := createHeapFile(osArgs, heapFile); done == nil {
return
} else {
defer done()
}
defer func() {
if err := pprof.WriteHeapProfile(f); err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to write heap profile: %s", err.Error()))
}
f.Close()
}()
}

// To view a CPU profile, drop the file into https://speedscope.app.
// Note: Running the CPU profiler doesn't work under Windows subsystem for
// Linux. The profiler has to be built for native Windows and run using the
// command prompt instead.
if cpuprofileFile != "" {
f, err := os.Create(cpuprofileFile)
if err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create cpuprofile file: %s", err.Error()))
if done := createCpuprofileFile(osArgs, cpuprofileFile); done == nil {
return
} else {
defer done()
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}

if cpuprofileFile != "" {
Expand Down
56 changes: 56 additions & 0 deletions cmd/esbuild/main_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// +build !js !wasm

package main

import (
"fmt"
"os"
"runtime/pprof"
"runtime/trace"

"github.com/evanw/esbuild/internal/logger"
)

func createTraceFile(osArgs []string, traceFile string) func() {
f, err := os.Create(traceFile)
if err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create trace file: %s", err.Error()))
return nil
}
trace.Start(f)
return func() {
trace.Stop()
f.Close()
}
}

func createHeapFile(osArgs []string, heapFile string) func() {
f, err := os.Create(heapFile)
if err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create heap file: %s", err.Error()))
return nil
}
return func() {
if err := pprof.WriteHeapProfile(f); err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to write heap profile: %s", err.Error()))
}
f.Close()
}
}

func createCpuprofileFile(osArgs []string, cpuprofileFile string) func() {
f, err := os.Create(cpuprofileFile)
if err != nil {
logger.PrintErrorToStderr(osArgs, fmt.Sprintf(
"Failed to create cpuprofile file: %s", err.Error()))
return nil
}
pprof.StartCPUProfile(f)
return func() {
pprof.StopCPUProfile()
f.Close()
}
}
24 changes: 24 additions & 0 deletions cmd/esbuild/main_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// +build js,wasm

package main

import (
"github.com/evanw/esbuild/internal/logger"
)

// Remove this code from the WebAssembly binary to reduce size. This only removes 0.4mb of stuff.

func createTraceFile(osArgs []string, traceFile string) func() {
logger.PrintErrorToStderr(osArgs, "The \"--trace\" flag is not supported when using WebAssembly")
return nil
}

func createHeapFile(osArgs []string, heapFile string) func() {
logger.PrintErrorToStderr(osArgs, "The \"--heap\" flag is not supported when using WebAssembly")
return nil
}

func createCpuprofileFile(osArgs []string, cpuprofileFile string) func() {
logger.PrintErrorToStderr(osArgs, "The \"--cpuprofile\" flag is not supported when using WebAssembly")
return nil
}
2 changes: 2 additions & 0 deletions pkg/api/serve_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package api

import "fmt"

// Remove the serve API in the WebAssembly build. This removes 2.7mb of stuff.

func serveImpl(serveOptions ServeOptions, buildOptions BuildOptions) (ServeResult, error) {
return ServeResult{}, fmt.Errorf("The \"serve\" API is not supported when using WebAssembly")
}

0 comments on commit a0d9dc9

Please sign in to comment.