-
Notifications
You must be signed in to change notification settings - Fork 485
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
ORC-1280:[C++] Implement block-based buffer(Part I) #1271
Conversation
c++/src/MemoryPool.hh
Outdated
#ifndef ORC_MEMORYPOOL_IMPL_HH | ||
#define ORC_MEMORYPOOL_IMPL_HH | ||
|
||
#include "orc/MemoryPool.hh" |
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.
It's a little confusing making another MemoryPool.hh
like this. Could you create a new file instead of MemoryPool.hh
file?
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.
Sure, I rename src/MemoryPool.hh into src/BlockBuffer.hh.
Thank you for making a PR. I have one comment about file structure, #1271 (comment). cc @wgtmac , @stiga-huang |
c++/src/BlockBuffer.hh
Outdated
@@ -0,0 +1,90 @@ | |||
|
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.
Redundant space.
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.
Done.
c++/test/TestBlockBuffer.cc
Outdated
@@ -0,0 +1,82 @@ | |||
|
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.
Redundant space.
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.
Done
It seems that the GitHub Action labeler is broken. Let me fix it. |
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.
Thanks @coderex2522 for the improvement! Would be great to equip the new class with more comment in detail.
c++/src/BlockBuffer.hh
Outdated
Block() : data(nullptr), size(0) {} | ||
Block(char* _data, uint64_t _size) : data(_data), size(_size) {} | ||
Block(const Block& block) = default; | ||
~Block() {} |
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.
~Block() = default;
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.
done.
c++/src/BlockBuffer.hh
Outdated
void resize(uint64_t size); | ||
void reserve(uint64_t capacity); | ||
}; | ||
} // namespace |
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.
// namespace orc
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.
done.
c++/src/BlockBuffer.hh
Outdated
/** | ||
* Get the Block object | ||
*/ | ||
Block getBlock(uint64_t blockIndex); |
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 need some detail explanation with regard to the mechanism and behavior of the BlockBuffer. Especially concepts like block, block index and block number.
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.
Added some description for block, blockbuffer, block index and block number.
c++/src/BlockBuffer.hh
Outdated
/** | ||
* Get the Block object | ||
*/ | ||
Block getBlock(uint64_t blockIndex); |
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.
Should we provide mutable and immutable overloads?
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.
Because the getBlock function does not modify class member variables, the const attribute is added to the function.
c++/src/BlockBuffer.hh
Outdated
} | ||
|
||
void resize(uint64_t size); | ||
void reserve(uint64_t capacity); |
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.
Insert a new line before reserve function. Better to explain what will happen in the reserve function
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.
Added some comments to explain the reserve function.
return currentCapacity; | ||
} | ||
|
||
void resize(uint64_t size); |
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.
Should we support shrinkToFit parameter?
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.
Class DataBuffer only recycles memory during destructor, so class BlockBuffer does not support shrink function, which can be further optimized later.
c++/src/BlockBuffer.hh
Outdated
* Block points to a section of memory allocated by BlockBuffer, | ||
* containing the corresponding physical memory address and size. | ||
*/ | ||
struct Block { |
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.
Is it better to make it nested class of BlockBuffer?
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 it stands struct Block only serves class BlockBuffer, so it makes reasonable to treat it as a nested class of class BlockBuffer.
c++/src/BlockBuffer.hh
Outdated
/** | ||
* Get a empty block or a new block if the buffer is exhausted. | ||
* If the last allocated block size is less than blockSize, | ||
* the empty block size is blockSize - lastBlockSize. |
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.
Where is the lastBlockSize?
It would be more user-friendly to get the available size of the returned block via output parameter.
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.
lastBlockSize means the size of the last allocated block. The comment has been modified here.
The returned block is used to describe the section of memory that can be read or written. And I add comment to the size in struct Block. So I don't recommend adding extra output parameter.
c++/src/BlockBuffer.hh
Outdated
* Otherwise, the empty block size is blockSize. | ||
* @return a empty block object | ||
*/ | ||
Block getEmptyBlock(); |
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.
rename to getNextBlock or getBlockToWrite?
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.
Done. Rename to getNextBlock.
c++/src/BlockBuffer.hh
Outdated
Block getEmptyBlock(); | ||
|
||
/** | ||
* Get the number of allocated blocks |
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.
Get the number of blocks that are fully or partially occupied
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.
Done.
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 left an inline comment. Overall looks good.
Block emptyBlock( | ||
blocks[currentSize / blockSize] + currentSize % blockSize, | ||
blockSize - currentSize % blockSize); | ||
currentSize = (currentSize / blockSize + 1) * blockSize; |
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.
Should we provide a function to update currentSize to reflect the actual size written? Maybe something setSize() or backup(). This looks weird when the returned block is written partially meaning that currentSize is larger than used
To provide better usability, we may provide a function like void append(const char data, size_t size)* to append data to the buffer and manage the blocks internally. This can be a separate patch.
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 user only uses part of the block, the size of BlockBuffer can currently be set via the resize() function. BlockBuffer class is temporarily used to replace DataBuffer in BufferedOutputStream, and the existing functions conform to BufferedOutputStream's usage behavior.
The append function can be added additionally later if there are usage scenarios.
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.
Since the flush() function of class BufferedOutputStream needs to access all allocated memory blocks, it needs class BlockBuffer to provide an interface in order for BufferedOutputStream to access all allocated memory blocks.
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.
+1 LGTM
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.
+1, LGTM, too.
I've submitted and closed it. Thanks @coderex2522 and @dongjoon-hyun |
What changes were proposed in this pull request?
This patch implements a block-based buffer allocator. Provides smarter memory management for the buffer of BufferedOutputStream class.
Why are the changes needed?
This patch implements smarter memory management, which can effectively solve the issue-1240. And replacing DataBuffer with BlockBuffer in BufferedOutputStream class will be implemented in another patch.
How was this patch tested?
The UTs in TestBlockBuffer.cc can test this patch.