Skip to content

Commit

Permalink
perf: make debug a constant set using a build flag (gnolang#2072)
Browse files Browse the repository at this point in the history
Changing the `debug` flag from a variable to a constant results in
increased performance anywhere in the code where the `if debug {`
conditional exists. The setting of the debug flag is now facilitated by
a `debug` build tag.

Here are a few benchmarks showing the performance difference for
TypedValue Set and Get operations. The benchmarks suffixed with 'New'
are those run with debugging disabled after introducing the build tag.
The other benchmarks were run with debugging disabled before introducing
the build tag.
```
go test -cpu 1,2,4,8 -benchmem -bench '^BenchmarkTV' -run=^$           
goos: darwin
goarch: amd64
pkg: github.com/gnolang/gno/gnovm/pkg/gnolang
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
BenchmarkTVGetBool              616270255                1.919 ns/op           0 B/op          0 allocs/op
BenchmarkTVGetBool-2            623206738                1.859 ns/op           0 B/op          0 allocs/op
BenchmarkTVGetBool-4            645019149                1.825 ns/op           0 B/op          0 allocs/op
BenchmarkTVGetBool-8            628096153                1.897 ns/op           0 B/op          0 allocs/op
BenchmarkTVGetBoolNew           1000000000               0.3015 ns/op          0 B/op          0 allocs/op
BenchmarkTVGetBoolNew-2         1000000000               0.2997 ns/op          0 B/op          0 allocs/op
BenchmarkTVGetBoolNew-4         1000000000               0.3026 ns/op          0 B/op          0 allocs/op
BenchmarkTVGetBoolNew-8         1000000000               0.3009 ns/op          0 B/op          0 allocs/op
BenchmarkTVSetBool              636429537                1.895 ns/op           0 B/op          0 allocs/op
BenchmarkTVSetBool-2            612271684                1.983 ns/op           0 B/op          0 allocs/op
BenchmarkTVSetBool-4            583097722                1.923 ns/op           0 B/op          0 allocs/op
BenchmarkTVSetBool-8            608042474                1.949 ns/op           0 B/op          0 allocs/op
BenchmarkTVSetBoolNew           1000000000               0.3109 ns/op          0 B/op          0 allocs/op
BenchmarkTVSetBoolNew-2         1000000000               0.3116 ns/op          0 B/op          0 allocs/op
BenchmarkTVSetBoolNew-4         1000000000               0.3101 ns/op          0 B/op          0 allocs/op
BenchmarkTVSetBoolNew-8         1000000000               0.3080 ns/op          0 B/op          0 allocs/op
BenchmarkTVGetInt               596222908                1.979 ns/op           0 B/op          0 allocs/op
BenchmarkTVGetInt-2             565257805                2.024 ns/op           0 B/op          0 allocs/op
BenchmarkTVGetInt-4             603462258                1.913 ns/op           0 B/op          0 allocs/op
BenchmarkTVGetInt-8             623668717                1.896 ns/op           0 B/op          0 allocs/op
BenchmarkTVGetIntNew            1000000000               0.3049 ns/op          0 B/op          0 allocs/op
BenchmarkTVGetIntNew-2          1000000000               0.3035 ns/op          0 B/op          0 allocs/op
BenchmarkTVGetIntNew-4          1000000000               0.3139 ns/op          0 B/op          0 allocs/op
BenchmarkTVGetIntNew-8          1000000000               0.3006 ns/op          0 B/op          0 allocs/op
BenchmarkTVSetInt               646375224                1.819 ns/op           0 B/op          0 allocs/op
BenchmarkTVSetInt-2             639805758                1.817 ns/op           0 B/op          0 allocs/op
BenchmarkTVSetInt-4             641337288                1.840 ns/op           0 B/op          0 allocs/op
BenchmarkTVSetInt-8             636959119                1.836 ns/op           0 B/op          0 allocs/op
BenchmarkTVSetIntNew            1000000000               0.3011 ns/op          0 B/op          0 allocs/op
BenchmarkTVSetIntNew-2          1000000000               0.3017 ns/op          0 B/op          0 allocs/op
BenchmarkTVSetIntNew-4          1000000000               0.2984 ns/op          0 B/op          0 allocs/op
BenchmarkTVSetIntNew-8          1000000000               0.2909 ns/op          0 B/op          0 allocs/op
```

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [x] Provided any useful hints for running manual tests
- [x] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
  • Loading branch information
deelawn authored May 29, 2024
1 parent e8b26ec commit 98bd124
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
15 changes: 6 additions & 9 deletions gnovm/pkg/gnolang/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gnolang
import (
"fmt"
"net/http"
"os"
"strings"
"time"

Expand All @@ -23,10 +22,8 @@ type debugging bool

// using a const is probably faster.
// const debug debugging = true // or flip
var debug debugging = false

func init() {
debug = os.Getenv("DEBUG") == "1"
if debug {
go func() {
// e.g.
Expand All @@ -48,16 +45,16 @@ func init() {

var enabled bool = true

func (d debugging) Println(args ...interface{}) {
if d {
func (debugging) Println(args ...interface{}) {
if debug {
if enabled {
fmt.Println(append([]interface{}{"DEBUG:"}, args...)...)
}
}
}

func (d debugging) Printf(format string, args ...interface{}) {
if d {
func (debugging) Printf(format string, args ...interface{}) {
if debug {
if enabled {
fmt.Printf("DEBUG: "+format, args...)
}
Expand All @@ -69,8 +66,8 @@ var derrors []string = nil
// Instead of actually panic'ing, which messes with tests, errors are sometimes
// collected onto `var derrors`. tests/file_test.go checks derrors after each
// test, and the file test fails if any unexpected debug errors were found.
func (d debugging) Errorf(format string, args ...interface{}) {
if d {
func (debugging) Errorf(format string, args ...interface{}) {
if debug {
if enabled {
derrors = append(derrors, fmt.Sprintf(format, args...))
}
Expand Down
5 changes: 5 additions & 0 deletions gnovm/pkg/gnolang/debug_false.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !debug

package gnolang

const debug debugging = false
5 changes: 5 additions & 0 deletions gnovm/pkg/gnolang/debug_true.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build debug

package gnolang

const debug debugging = true

0 comments on commit 98bd124

Please sign in to comment.