Skip to content

feat: cpu profile #764

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

Closed
wants to merge 1 commit into from
Closed

Conversation

zhuliquan
Copy link
Contributor

This is proof of concept PR, which help to analyze expr profile. I notice that expr has profile feature, but Config doesn't explode it for user (i.e. user can't set Profile true).

expr/compiler/compiler.go

Lines 212 to 232 in 80f0ea6

if c.config != nil && c.config.Profile {
span := &Span{
Name: reflect.TypeOf(node).String(),
Expression: node.String(),
}
if len(c.spans) > 0 {
prev := c.spans[len(c.spans)-1]
prev.Children = append(prev.Children, span)
}
c.spans = append(c.spans, span)
defer func() {
if len(c.spans) > 1 {
c.spans = c.spans[:len(c.spans)-1]
}
}()
c.emit(OpProfileStart, c.addConstant(span))
defer func() {
c.emit(OpProfileEnd, c.addConstant(span))
}()
}

I think it's very likely that the reason lies in how you show the profile results. I've noticed that the results are saved to a structure called Span.

expr/vm/utils.go

Lines 27 to 33 in 80f0ea6

type Span struct {
Name string `json:"name"`
Expression string `json:"expression"`
Duration int64 `json:"duration"`
Children []*Span `json:"children"`
start time.Time
}

My idea is to convert the information stored in the Span result into a pprof file, so that we can use go tool pprof command to visualise how the expression is running and find out where it is time-consuming.

I create new sub-module profile. I write a function GeneratePprofProfile to convert Span struct to pprof file (Of course, this function is not perfect, and the profile may need to be more refined. Profile construct). Finally I write a example code and got below result.
Screenshot_2025-03-09_17-04-28
Screenshot_2025-03-09_17-06-24
Screenshot_2025-03-09_17-06-38

@antonmedv
Copy link
Member

This is a nice idea to use pprof. Only for complex rules only reporting ast node type maybe not enough. And actually expr profile is a part of Expr Pro https://expr-lang.org/docs/expr-pro/profile I sell Expr Pro to boost Expr development ;)
Additionally I created a separate renderer for profile, with information about which part of expression is causing errors:
image

@antonmedv antonmedv closed this Mar 24, 2025
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.

2 participants