Arena can replace MemPool in most scenarios. Except for memory reuse, MemPool supports reuse of previous memory chunks after clear, but Arena does not.
Some comparisons between MemPool and Arena:
1. Expansion
Arena is less than 128M index 2 alloc chunk; more than 128M memory, allocate 128M * n > `size`, n is equal to the minimum value that satisfies the expression;
MemPool less than 512K index 2 alloc chunk, greater than 512K memory, separately apply for a `size` length chunk
After Arena applied for a chunk larger than 128M last time, the minimum chunk applied for after that is 128M. Does this seem to be a waste of memory? MemPool is also similar. After the chunk of 512K was applied for last time, the minimum chunk of subsequent applications is 512K.
2. Alignment
MemPool defaults to 16 alignment, because memtable and other places that use int128 require 16 alignment;
Arena has no default alignment;
3. Memory reuse
Arena only supports `rollback`, which reuses the memory of the current chunk, usually the memory requested last time.
MemPool supports clear(), all chunks can be reused; or call ReturnPartialAllocation() to roll back the last requested memory; if the last chunk has no memory, search for the most free chunk for allocation
4. Realloc
Arena supports realloc contiguous memory; it also supports realloc contiguous memory from any position at the time of the last allocation. The difference between `alloc_continue` and `realloc` is:
1. Alloc_continue does not need to specify the old size, but the default old size = head->pos - range_start
2. alloc_continue supports expansion from range_start when additional_bytes is between head and pos, which is equivalent to reusing a part of memory, while realloc completely allocates a new memory
MemPool does not support realloc, but supports transferring or absorbing chunks between two MemPools
5. check mem limit
MemPool checks the mem limit, and Arena checks at the Allocator layer.
6. Support for ASAN
Arena does something extra
7. Error handling
MemPool supports returning the error message of application failure directly through `Status`, and Arena throws Exception.
Tests that Arena can consider
1. After the last applied chunk is larger than 128M, the minimum applied chunk is 128M, which seems to waste memory;
2. Support clear, memory multiplexing;
3. Increase the large list, alloc the memory larger than 128M, and the size is equal to `size`, so as to avoid the current chunk not being fully used, which is wasteful.
4. In some cases, it may be possible to allocate backwards to find chunks t
107 lines
3.1 KiB
C++
107 lines
3.1 KiB
C++
// Licensed to the Apache Software Foundation (ASF) under one
|
|
// or more contributor license agreements. See the NOTICE file
|
|
// distributed with this work for additional information
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
// to you under the Apache License, Version 2.0 (the
|
|
// "License"); you may not use this file except in compliance
|
|
// with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing,
|
|
// software distributed under the License is distributed on an
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
// KIND, either express or implied. See the License for the
|
|
// specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
#include "util/decompress.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include "gen_cpp/Descriptors_types.h"
|
|
|
|
using namespace std;
|
|
|
|
namespace doris {
|
|
|
|
// Fixture for testing class Decompressor
|
|
class DecompressorTest : public ::testing::Test {
|
|
protected:
|
|
DecompressorTest() {
|
|
uint8_t* ip = _input;
|
|
|
|
for (int i = 0; i < 1024; i++) {
|
|
for (uint8_t ch = 'a'; ch <= 'z'; ++ch) {
|
|
*ip++ = ch;
|
|
}
|
|
|
|
for (uint8_t ch = 'Z'; ch >= 'A'; --ch) {
|
|
*ip++ = ch;
|
|
}
|
|
}
|
|
}
|
|
|
|
void RunTest(THdfsCompression::type format) {
|
|
std::unique_ptr<Codec> compressor;
|
|
std::unique_ptr<Codec> decompressor;
|
|
vectorized::Arena* arena = new vectorized::Arena;
|
|
|
|
EXPECT_TRUE(Codec::create_compressor(nullptr, arena, true, format, &compressor).ok());
|
|
EXPECT_TRUE(Codec::create_compressor(nullptr, arena, true, format, &decompressor).ok());
|
|
|
|
uint8_t* compressed = nullptr;
|
|
int compressed_length = 0;
|
|
EXPECT_TRUE(
|
|
compressor->process_block(sizeof(_input), _input, &compressed_length, &compressed)
|
|
.ok());
|
|
uint8_t* output = nullptr;
|
|
int out_len = 0;
|
|
EXPECT_TRUE(
|
|
decompressor->process_block(compressed_length, compressed, &out_len, &output).ok());
|
|
|
|
EXPECT_TRUE(memcmp(&_input, output, sizeof(_input)) == 0);
|
|
|
|
// Try again specifying the output buffer and length.
|
|
out_len = sizeof(_input);
|
|
output = arena->alloc(out_len);
|
|
EXPECT_TRUE(
|
|
decompressor->process_block(compressed_length, compressed, &out_len, &output).ok());
|
|
|
|
EXPECT_TRUE(memcmp(&_input, output, sizeof(_input)) == 0);
|
|
}
|
|
|
|
private:
|
|
uint8_t _input[2 * 26 * 1024];
|
|
};
|
|
|
|
TEST_F(DecompressorTest, Default) {
|
|
RunTest(THdfsCompression::DEFAULT);
|
|
}
|
|
|
|
TEST_F(DecompressorTest, Gzip) {
|
|
RunTest(THdfsCompression::GZIP);
|
|
}
|
|
|
|
TEST_F(DecompressorTest, Deflate) {
|
|
RunTest(THdfsCompression::DEFLATE);
|
|
}
|
|
|
|
TEST_F(DecompressorTest, Bzip) {
|
|
RunTest(THdfsCompression::BZIP2);
|
|
}
|
|
|
|
TEST_F(DecompressorTest, Snappy) {
|
|
RunTest(THdfsCompression::SNAPPY);
|
|
}
|
|
|
|
TEST_F(DecompressorTest, SnappyBlocked) {
|
|
RunTest(THdfsCompression::SNAPPY_BLOCKED);
|
|
}
|
|
|
|
} // namespace doris
|