-
Notifications
You must be signed in to change notification settings - Fork 0
/
lz4.cpp
208 lines (185 loc) · 4.49 KB
/
lz4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <array>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <fstream>
#include <future>
#include <iostream>
#include <lz4.h>
#include <thread>
#include <vector>
namespace
{
constexpr size_t BLOCK_BYTES = 8 * 1024;
using BLOCK = std::array<char, LZ4_COMPRESSBOUND(BLOCK_BYTES)>;
using atmint = std::atomic_int;
struct WriteInfo
{
char* buff_;
int size_;
std::mutex lock_;
WriteInfo() = default;
WriteInfo(const WriteInfo& o) { *this = o; }
WriteInfo& operator=(const WriteInfo& o)
{
buff_ = o.buff_;
size_ = o.size_;
return *this;
}
};
void
test_write(std::ofstream& ofst, WriteInfo& wi)
{
std::lock_guard g(wi.lock_);
ofst.write((const char*)&wi.size_, sizeof(wi.size_));
ofst.write(wi.buff_, wi.size_);
}
void
test_compress(std::ofstream& ofst, std::ifstream& ifst)
{
LZ4_stream_t lz4Stream;
std::vector<BLOCK> inpBuf;
std::vector<BLOCK> outBuf;
inpBuf.resize(2);
outBuf.resize(40);
int inpBufIndex = 0;
int outBufIndex = 0;
atmint inPage = -1;
atmint outPage = 0;
std::vector<WriteInfo> wrlist;
wrlist.resize(40);
auto ntime = std::chrono::steady_clock::now();
LZ4_initStream(&lz4Stream, sizeof(lz4Stream));
auto f = std::async(std::launch::async, [&]() {
while (inPage.load() != -2)
{
while (outPage.load() > inPage.load())
;
int page = outPage.load() % outBuf.size();
auto& winfo = wrlist[page];
test_write(ofst, winfo);
outPage++;
}
std::cout << "done" << std::endl;
});
for (;;)
{
int pn = inpBufIndex % 2;
char* const inpPtr = inpBuf[pn].data();
ifst.read(inpPtr, BLOCK_BYTES);
const int inpBytes = ifst.gcount();
if (0 == inpBytes)
break;
int opage = outBufIndex % outBuf.size();
auto& winfo = wrlist[opage];
auto& cmpBuf = outBuf[opage];
const int cmpBytes = LZ4_compress_fast_continue(&lz4Stream, inpPtr, cmpBuf.data(), inpBytes, cmpBuf.size(), 1);
if (cmpBytes <= 0)
break;
{
std::lock_guard g(winfo.lock_);
winfo.size_ = cmpBytes;
winfo.buff_ = cmpBuf.data();
inPage = outBufIndex;
}
outBufIndex++;
inpBufIndex++;
while ((inPage.load() - outPage.load()) > 5)
;
}
while (inPage.load() != outPage.load())
;
inPage = -2;
f.wait();
ofst << 0;
auto etime = std::chrono::steady_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(etime - ntime);
std::cout << "Elapsed time: " << elapsed_time.count() << std::endl;
}
void
test_decompress(std::ofstream& ofst, std::ifstream& ifst)
{
LZ4_streamDecode_t lz4StreamDecode_body;
LZ4_streamDecode_t* lz4StreamDecode = &lz4StreamDecode_body;
std::vector<BLOCK> decBuf;
decBuf.resize(4);
int decBufIndex = 0;
LZ4_setStreamDecode(lz4StreamDecode, NULL, 0);
for (;;)
{
char cmpBuf[LZ4_COMPRESSBOUND(BLOCK_BYTES)];
int cmpBytes = 0;
{
ifst.read((char*)&cmpBytes, sizeof(cmpBytes));
const size_t readCount0 = ifst.gcount();
if (readCount0 < 1 || cmpBytes <= 0)
{
break;
}
ifst.read(cmpBuf, cmpBytes);
const size_t readCount1 = ifst.gcount();
if (readCount1 != (size_t)cmpBytes)
{
break;
}
}
{
char* const decPtr = decBuf[decBufIndex % decBuf.size()].data();
const int decBytes = LZ4_decompress_safe_continue(lz4StreamDecode, cmpBuf, decPtr, cmpBytes, BLOCK_BYTES);
if (decBytes <= 0)
{
break;
}
ofst.write(decPtr, decBytes);
}
decBufIndex++;
}
std::cout << "done" << std::endl;
}
} // namespace
//
//
//
int
main(int argc, char** argv)
{
auto help = []() {
std::cerr << "lz4: [d] input output\n"
"\td\tdecompress"
<< std::endl;
};
if (argc < 3)
{
help();
return 1;
}
try
{
std::string arg = argv[1];
if (arg == "d")
{
// decompress
if (argc < 4)
{
std::cout << "need output file" << std::endl;
help();
return 1;
}
std::ifstream ifst(argv[2], std::ios::binary);
std::ofstream ofst(argv[3], std::ios::binary);
test_decompress(ofst, ifst);
}
else
{
// compress
std::ifstream ifst(argv[1], std::ios::binary);
std::ofstream ofst(argv[2], std::ios::binary);
test_compress(ofst, ifst);
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}