-
Notifications
You must be signed in to change notification settings - Fork 298
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
process: reuse and preallocate memory #355
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Florian Lehner <[email protected]>
Signed-off-by: Florian Lehner <[email protected]>
Can you add memory allocations for before and after? |
Updated benchmark on 4b538b5:
BenchmarkGetMappings
|
Updated benchmark on 4b538b5:
BenchmarkParseMappings
|
Co-authored-by: Tim Rühsen <[email protected]>
Signed-off-by: Florian Lehner <[email protected]>
2f72f9d
to
53cb4e0
Compare
Co-authored-by: Christos Kalkanis <[email protected]>
Co-authored-by: Christos Kalkanis <[email protected]>
// Reset memory and return it for reuse. | ||
for j := 0; j < len(*scanBuf); j++ { | ||
(*scanBuf)[j] = 0x0 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a second thought... why clear the memory at all here? parseMapping() does not rely on the contents of the buffer retrieved from the pool. The scanner read data and only acts on the read data in the buffer. It doesn't matter if we fill the bufferswith 0 bytes or random bytes or leave it as is.
// Reset memory and return it for reuse. | |
for j := 0; j < len(*scanBuf); j++ { | |
(*scanBuf)[j] = 0x0 | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scanner, that uses this buffer, uses the default split function ScanLines. By not clearing the buffer, we might introduce subtle bugs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we might introduce subtle bugs
Possibly yes. Can you add a test case that triggers such a bug. That makes it obvious to if someone ever touches that code or wonders if it is required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I can't see how bufio.Scanner.Scan()
uses ScanLines()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we don't set a specific Split function, the buffer uses the default https://pkg.go.dev/bufio#NewScanner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're not sure, I'd rather we clear the buffer than introduce a subtle bug. The performance implications here should be minimal to non-existent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see no indication of subtle bugs here (the std library code is pretty clear). If you think there are, please add a test case to catch it. The split function only operates on the bytes that have been read by Read()
, so whatever bytes we set to 0, they will be overwritten before being acted upon. I don't think we should introduce code because "there could be bugs inside the std library", that isn't fact-driven development.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have reverted the applied suggestion, as I should not have applied in the first place. And I also reasked for review.
I have reverted the applied suggestion as resetting a buffer before reusing is something I will ask in a review, until it is shown that the operation can be done correctly with every state of the buffer. And this proposed change does not show that it will work as supposed with every state of the shared buffer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still didn't see any argument why or what exactly can fail. Without an explicit statement it is not possible to create a test case. Can't we just go with the facts instead of having some vague "fear"?
Co-authored-by: Tim Rühsen <[email protected]>
This reverts commit 2b04dbc.
No description provided.