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

Improved Runtime of Function Process in process.go #1245

Merged
merged 1 commit into from Sep 11, 2023

Conversation

neilnaveen
Copy link
Contributor

@neilnaveen neilnaveen commented Sep 8, 2023

Description of the PR

Fixes: #1225

This PR addresses the performance issue raised in #1225 regarding the noticeable performance drawbacks when handling deeply nested structures in the processHelper function.

Changes:

  1. Initially attempted to refactor processHelper from a recursive function to an iterative approach. However, this change did not yield significant runtime improvements.
  2. Profiling revealed that the Unpack function, which utilized encoding/json, was the primary contributor to the extended runtime.
  3. Switched from using encoding/json to json-iterator/go for JSON operations, resulting in a significant runtime improvement.

Benchmark Results:

Test case used for benchmarking:

sd := SimpleDoc{  
   Issuer: "google.com",  
   Info:   "this is a cool document",  
   Nested: []SimpleDoc{},  
}  
  
cur := sd  
for i := 0; i < 4000; i++ {  
   k := SimpleDoc{Issuer: "google.com", Info: fmt.Sprintf("this is a cooler nested doc %d", i), Nested: []SimpleDoc{cur}}  
  
   cur = k  
}  
// marshal the document  
blob, _ := json.Marshal(cur)  
  
// create a document  
  
overflowDoc := processor.Document{  
   Blob:   blob,  
   Type:   simpledoc.SimpleDocType,  
   Format: processor.FormatJSON,
}
  • Using encoding/json: Document processed in 29.227243671s
  • Using json-iterator/go: Document processed in 9.27833562s

As observed, json-iterator/go reduced the runtime by approximately two-thirds.

PR Checklist

  • All commits have a Developer Certificate of Origin (DCO) -- they are generated using -s flag to git commit.
  • All new changes are covered by tests
  • If GraphQL schema is changed, make generate has been run
  • If collectsub protobuf has been changed, make proto has been run
  • All CI checks are passing (tests and formatting)
  • All dependent PRs have already been merged

Copy link
Collaborator

@pxp928 pxp928 left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the performance improvement @neilnaveen!

@pxp928
Copy link
Collaborator

pxp928 commented Sep 11, 2023

@neilnaveen a quick rebase and this will auto merge

- Changed json encoding package from encoding/json to json-iterator/go
- Using `json-iterator/go` reduced the runtime by approximately two-thirds.

Signed-off-by: Neil Naveen <42328488+neilnaveen@users.noreply.github.com>
@neilnaveen
Copy link
Contributor Author

@neilnaveen a quick rebase and this will auto merge

Thanks, I have fixed the conflicts.

@kodiakhq kodiakhq bot merged commit 0268d1a into guacsec:main Sep 11, 2023
9 checks passed
@neilnaveen neilnaveen deleted the neil/feature/process.go branch September 11, 2023 18:29
neilnaveen added a commit to neilnaveen/guac that referenced this pull request Sep 11, 2023
- Reasons can be found in PR guacsec#1245
Signed-off-by: Neil Naveen <42328488+neilnaveen@users.noreply.github.com>
neilnaveen added a commit to neilnaveen/guac that referenced this pull request Sep 11, 2023
- Reasons can be found in PR guacsec#1245
Signed-off-by: Neil Naveen <42328488+neilnaveen@users.noreply.github.com>

Signed-off-by: Neil Naveen <42328488+neilnaveen@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants