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

Improve insertion point behaviour #3092

Merged
merged 5 commits into from
Jun 20, 2024
Merged

Improve insertion point behaviour #3092

merged 5 commits into from
Jun 20, 2024

Conversation

emcfarlane
Copy link
Contributor

Small improvements on creating insertion points. Removes the need to trim the resulting file for newlines. Removes a buffer allocation to build each insertion.

On missing an insertion point fail with a user facing error and avoid
panicing due to an invalid slice on an empty array.
Small improvements on creating insertion points. Removes the need to
trim the resulting file for newlines. Removes a buffer allocation to
build the temporary insertion.
@emcfarlane emcfarlane requested a review from doriable June 17, 2024 18:03
for i := 0; targetScanner.Scan(); i++ {
if i > 0 {
// These writes cannot fail, they will panic if they cannot allocate.
_, _ = postInsertionContent.Write(newline)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Create a newline only if we are going to append more content to the file. This removes the need to trim after the buffer is finalized.

_, _ = postInsertionContent.Write(insertedContent)
// Insert the content from the insertion point file. Handle newlines in
// a platform-agnostic manner.
insertedContentReader := strings.NewReader(insertionPointFile.GetContent())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prefer strings.NewReader over bytes.NewBufferString as the string is RO.

// Insert the content from the insertion point file. Handle newlines in
// a platform-agnostic manner.
insertedContentReader := strings.NewReader(insertionPointFile.GetContent())
writeWithPrefixAndLineEnding(postInsertionContent, insertedContentReader, whitespace, newline)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Write to the buffer instead of allocating a new slice and then writing to the buffer.

Base automatically changed from ed/fixEmptyInsertionPoint to main June 17, 2024 22:56
Copy link
Member

@doriable doriable left a comment

Choose a reason for hiding this comment

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

Seems reasonable to me, I think these are some nice improvements.

@emcfarlane emcfarlane requested a review from bufdev June 17, 2024 23:07
Copy link
Member

@bufdev bufdev left a comment

Choose a reason for hiding this comment

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

How is this currently tested? Are you confident that the new code is being exercised?

I'm hesitant to change 3-4 year old code whose performance doesn't matter at all, that has been working, just for a perf improvement, unless we're confident that it is fully tested (maybe it is).

@emcfarlane
Copy link
Contributor Author

Tests are here with some benchmarks that show the improvements. #3090 improved the coverage for the error cases captured in that issue. Agree that the benchmarks don't matter for this flow but thought this simplified it and removed the workarounds for both newline printing, that caused the original panic issue, and the need to calculate a buffer size.

@emcfarlane emcfarlane merged commit 0b7c7fa into main Jun 20, 2024
9 checks passed
@emcfarlane emcfarlane deleted the ed/insertionPointReduce branch June 20, 2024 15:16
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.

3 participants