Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster utils.FileWithLineNum #6981

Merged
merged 2 commits into from Apr 22, 2024
Merged

Faster utils.FileWithLineNum #6981

merged 2 commits into from Apr 22, 2024

Conversation

kkocdko
Copy link
Contributor

@kkocdko kkocdko commented Apr 20, 2024

  • Do only one thing
  • Non breaking API changes
  • Tested (may be untestable for ci)

What did this pull request do?

Faster utils.FileWithLineNum. Avoid call runtime.Caller multi times.

Trace log performace gained a little.

@kkocdko kkocdko changed the title faster FileWithLineNum Faster utils.FileWithLineNum Apr 20, 2024
@kkocdko
Copy link
Contributor Author

kkocdko commented Apr 20, 2024

Called here https://github.com/go-gorm/gorm/blob/v1.25.9/logger/logger.go#L180

Bench code:

package main

import (
	"context"
	"fmt"
	"strings"
	"time"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/utils"
)

type dlog struct{}

func (dlog) LogMode(logger.LogLevel) logger.Interface { return dlog{} }
func (dlog) Info(context.Context, string, ...any)     {}
func (dlog) Warn(context.Context, string, ...any)     {}
func (dlog) Error(context.Context, string, ...any)    {}
func (dlog) Trace(_ context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
	if strings.HasSuffix(utils.FileWithLineNum(), "main.go:34") {
		panic("not same")
	}
}

func main() {
	db, _ := gorm.Open(mysql.Open("root@tcp(127.0.0.1:3306)"), &gorm.Config{Logger: dlog{}})
	db.Exec("select 1")
	for j := 0; j < 10; j++ {
		benchBegin := time.Now()
		for i := 0; i < 9000; i++ {
			db.Exec("select 1")
		}
		fmt.Printf("bench: %v\n", time.Since(benchBegin).Nanoseconds())
	}
}

Before

bench: 49571265
bench: 48794627
bench: 48044889
bench: 48055469
bench: 47996819
bench: 48150127
bench: 49503758
bench: 55815334
bench: 50576992
bench: 48269490
[kkocdko@klf higoweb]$ 

After

bench: 36544193
bench: 37921297
bench: 35384326
bench: 36247647
bench: 35748119
bench: 35659121
bench: 35108277
bench: 35288646
bench: 35050349
bench: 35066890
[kkocdko@klf higoweb]$

On a long chain call, like db.Model(&User{}).Limit(10).Find(&APIUser{})..., the performance difference will be more significant.

if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) &&
!strings.HasSuffix(file, ".gen.go") {
return file + ":" + strconv.FormatInt(int64(line), 10)
pcs := [13]uintptr{}
Copy link
Contributor Author

@kkocdko kkocdko Apr 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Origin code 15-2=13. No breaking changes.

@@ -32,12 +32,16 @@ func sourceDir(file string) string {

// FileWithLineNum return the file name and line number of the current file
func FileWithLineNum() string {
// the second caller usually from gorm internal, so set i start from 2
for i := 2; i < 15; i++ {
_, file, line, ok := runtime.Caller(i)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runtime.Caller(i) 's inner is caller(skip+1), so we use runtime.Callers(3) in new implement.

@kkocdko
Copy link
Contributor Author

kkocdko commented Apr 20, 2024

The CI error is not my wrong, see #6982 , curious why?

@jinzhu jinzhu merged commit bc49365 into go-gorm:master Apr 22, 2024
33 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants