Skip to content

Optimize bufio.NewScanner with bytes.NewReader#791

Open
andersendsa wants to merge 1 commit into
open-edge-platform:mainfrom
andersendsa:bytes_newreader
Open

Optimize bufio.NewScanner with bytes.NewReader#791
andersendsa wants to merge 1 commit into
open-edge-platform:mainfrom
andersendsa:bytes_newreader

Conversation

@andersendsa
Copy link
Copy Markdown

Description

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.

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:

  1. Test the Platform Manageability Agent:

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.

  1. Test the Platform Update Agent:

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:

  • I agree to use the APACHE-2.0 license for my code changes
  • I have not introduced any 3rd party dependency changes
  • I have performed a self-review of my code

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.
@andersendsa
Copy link
Copy Markdown
Author

Hi @jkossak could you pls tell me if this pr needs any changes or if it is good to merge

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.

1 participant