Skip to content

Commit

Permalink
perf: issues syncpool and issues.Collect function to collect issues i…
Browse files Browse the repository at this point in the history
…nto sync pool (#93)
  • Loading branch information
Oudwins authored Feb 19, 2025
1 parent 70ad348 commit 91d6b3e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
4 changes: 4 additions & 0 deletions internals/Issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func (e *ZogErr) String() string {
return fmt.Sprintf("ZogError{Code: %v, Params: %v, Type: %v, Value: %v, Message: '%v', Error: %v}", SafeString(e.C), SafeString(e.ParamsM), SafeString(e.Typ), SafeString(e.Val), SafeString(e.Msg), SafeError(e.Err))
}

func (e *ZogErr) Free() {
ZogIssuePool.Put(e)
}

// list of errors. This is returned by processors for simple types (e.g. strings, numbers, booleans)
type ZogIssueList = []ZogError

Expand Down
53 changes: 27 additions & 26 deletions internals/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

// 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)
}
Expand All @@ -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
Expand All @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions internals/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ var InternalIssueMapPool = sync.Pool{
},
}

var ZogIssuePool = sync.Pool{
New: func() any {
return &ZogErr{}
},
}

func ClearPools() {
ExecCtxPool = sync.Pool{
New: func() any {
Expand All @@ -47,6 +53,11 @@ func ClearPools() {
return &ErrsMap{}
},
}
ZogIssuePool = sync.Pool{
New: func() any {
return &ZogErr{}
},
}

}

Expand Down
26 changes: 26 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func (i *issueHelpers) SanitizeMap(m ZogIssueMap) map[string][]string {
return errs
}

func (i issueHelpers) SanitizeMapAndCollect(m ZogIssueMap) map[string][]string {
errs := i.SanitizeMap(m)
i.CollectMap(m)
return errs
}

func (i *issueHelpers) SanitizeList(l ZogIssueList) []string {
errs := make([]string, len(l))
for i, err := range l {
Expand All @@ -104,6 +110,26 @@ func (i *issueHelpers) SanitizeList(l ZogIssueList) []string {
return errs
}

func (i *issueHelpers) SanitizeListAndCollect(l ZogIssueList) []string {
errs := i.SanitizeList(l)
i.CollectList(l)
return errs
}

func (i *issueHelpers) CollectMap(issues ZogIssueMap) {
for _, list := range issues {
i.CollectList(list)
}
}

func (i *issueHelpers) CollectList(issues ZogIssueList) {
for _, err := range issues {
if err, ok := err.(*p.ZogErr); ok {
err.Free()
}
}
}

// ! ERRORS -> Deprecated
// Deprecated: This will be removed in the future.
type errHelpers struct {
Expand Down

0 comments on commit 91d6b3e

Please sign in to comment.