-
Notifications
You must be signed in to change notification settings - Fork 439
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
Suboptimal performance of method WritableTrackingBuffer.makeRoomFor #1546
Comments
Hi @pavel-zeman , I just did a bit research on this part and think the current logic make sense. I may not be 100% correct. For you proposal: The case that you change will be effective is when initialSize - position ( I understand you concern, but if we keep creating a larger size buffer, it may cause unnecessary memory consumption all the time. |
Hi @MichaelSun90, Let's take your example with
You can see, that a new buffer is allocated 6 times. With my proposal, the same situation looks as follows:
A new buffer is allocated just once. To observe the real performance impact, we can use the following simple test: const buffer = new WritableTrackingBuffer(10);
console.time("buffer");
for(let i = 0; i < 10000; i++) {
buffer.writeInt8(1);
}
console.timeEnd("buffer"); With the existing When run using Node.js with You may argue, that my test is just theoretical and such situation does not happen in |
@pavel-zeman Thanks for looking into this! You're right, the If the final length is not know, the correct way to handle this is to set @MichaelSun90, @mShan0 and I looked through all the uses of |
Software versions
Problem description
Source code of method
WritableTrackingBuffer.makeRoomFor
is as follows:The problem is the last line, i.e.
this.newBuffer(requiredLength);
. If you allocate a new buffer ofrequiredLength
, the buffer will be immediately full and a new buffer will have to be allocated next time you write something to it. As a result, each followingwriteXXX
method will result in a new buffer being allocated.I propose to respect the
initialSize
here, i.e. change the line tothis.newBuffer(Math.max(this.initialSize, requiredLength));
.The text was updated successfully, but these errors were encountered: