Optimize bufio.NewScanner with bytes.NewReader#791
Open
andersendsa wants to merge 1 commit into
Open
Conversation
Replaced `strings.NewReader(string(bytes))` with `bytes.NewReader(bytes)` for initializing `bufio.NewScanner`. This avoids allocating and copying `[]byte` into a `string` before reading, which reduces memory usage and garbage collection overhead.
Author
|
Hi @jkossak could you pls tell me if this pr needs any changes or if it is good to merge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Replaced
strings.NewReader(string(bytes))withbytes.NewReader(bytes)for initializingbufio.NewScanner.This avoids allocating and copying
[]byteinto astringbefore reading, which reduces memory usage and garbage collection overhead.Any Newly Introduced Dependencies
This optimization strictly relies on the Go standard library. Specifically, the change imports the standard library bytes package to replace the usage of the strings package in creating a new reader.
No external libraries or modules were added to the go.mod files of either platform-manageability-agent or platform-update-agent.
How Has This Been Tested?
Here are the detailed steps to reproduce the testing and verify the changes:
Navigate to the agent's directory
cd platform-manageability-agent
Run the test suite using the provided Makefile command
make test
Expected Result: The test output will show ok for github.com/open-edge-platform/edge-node-agents/platform-manageability-agent/internal/comms, confirming that parseAMTInfoField and IsActivationInProgress (which had changes) still function properly.
Navigate to the agent's directory
cd ../platform-update-agent
Run the test suite using the provided Makefile command
make test
Expected Result: The test output will show ok for github.com/open-edge-platform/edge-node-agents/platform-update-agent/internal/utils, confirming that DetectOS accurately identifies the operating system string using the modified bufio.NewScanner mechanism.
Test Configuration Details:
The tests are run using standard Go testing tools invoked via Makefiles (make test maps to go test ./internal/... -race).
No special environmental variables or configurations are required to run these unit tests locally. The changes solely replace how an io.Reader is instantiated over an existing byte slice, so external dependencies or hardware requirements aren't a factor for this validation.
Checklist: