-
Notifications
You must be signed in to change notification settings - Fork 34
perf: errors syncpool #93
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -109,22 +109,21 @@ type TestCtx struct { | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (c *SchemaCtx) Issue() ZogIssue { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO handle catch here | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return &ZogErr{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| EPath: c.Path.String(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Typ: c.DType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Val: c.Val, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e := ZogIssuePool.Get().(*ZogErr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.EPath = c.Path.String() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.Typ = c.DType | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.Val = c.Val | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return e | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
110
to
117
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling for type assertion. The type assertion from pool.Get() could panic if the pool returns an incorrect type. Add error handling: func (c *SchemaCtx) Issue() ZogIssue {
- e := ZogIssuePool.Get().(*ZogErr)
+ e, ok := ZogIssuePool.Get().(*ZogErr)
+ if !ok {
+ // Fallback to creating a new instance if pool returns wrong type
+ e = &ZogErr{}
+ }
e.EPath = c.Path.String()
e.Typ = c.DType
e.Val = c.Val
return e
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Please don't depend on this method it may change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (c *SchemaCtx) IssueFromTest(test *Test, val any) ZogIssue { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e := &ZogErr{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| EPath: c.Path.String(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Typ: c.DType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Val: val, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| C: test.IssueCode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ParamsM: test.Params, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e := ZogIssuePool.Get().(*ZogErr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.C = test.IssueCode | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.EPath = c.Path.String() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.Typ = c.DType | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.Val = val | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.ParamsM = test.Params | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if test.IssueFmtFunc != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| test.IssueFmtFunc(e, c) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -136,13 +135,13 @@ func (c *SchemaCtx) IssueFromTest(test *Test, val any) ZogIssue { | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Please don't depend on this method it may change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (c *SchemaCtx) IssueFromCoerce(err error) ZogIssue { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return &ZogErr{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| C: zconst.IssueCodeCoerce, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| EPath: c.Path.String(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Typ: c.DType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Val: c.Val, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Err: err, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e := ZogIssuePool.Get().(*ZogErr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.C = zconst.IssueCodeCoerce | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.EPath = c.Path.String() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.Typ = c.DType | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.Val = c.Val | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.Err = err | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return e | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Please don't depend on this method it may change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -162,13 +161,15 @@ func (c *SchemaCtx) Free() { | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (c *TestCtx) Issue() ZogIssue { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO handle catch here | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return &ZogErr{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| EPath: c.Path.String(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Typ: c.DType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Val: c.Val, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| C: c.Test.IssueCode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ParamsM: c.Test.Params, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| zerr := ZogIssuePool.Get().(*ZogErr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| zerr.C = c.Test.IssueCode | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| zerr.EPath = c.Path.String() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| zerr.Err = nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| zerr.Msg = "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| zerr.ParamsM = c.Test.Params | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| zerr.Typ = c.DType | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| zerr.Val = c.Val | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return zerr | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (c *TestCtx) FmtErr(e ZogIssue) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset fields before returning to pool.
The Free() method should reset fields before returning the instance to the pool to prevent potential data leaks and ensure clean state for reuse.
Add field reset:
func (e *ZogErr) Free() { + // Reset fields to zero values before returning to pool + e.C = "" + e.EPath = "" + e.ParamsM = nil + e.Typ = "" + e.Val = nil + e.Msg = "" + e.Err = nil ZogIssuePool.Put(e) }📝 Committable suggestion