Skip to content

Commit ab726d7

Browse files
committed
Optimizations around compression buffer
1 parent f02c267 commit ab726d7

File tree

4 files changed

+340
-132
lines changed

4 files changed

+340
-132
lines changed

include/lsp-plug.in/resource/buffer.h

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (C) 2021 Linux Studio Plugins Project <https://lsp-plug.in/>
3-
* (C) 2021 Vladimir Sadovnikov <[email protected]>
2+
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3+
* (C) 2025 Vladimir Sadovnikov <[email protected]>
44
*
55
* This file is part of lsp-runtime-lib
66
* Created on: 13 мар. 2021 г.
@@ -54,9 +54,10 @@ namespace lsp
5454
public:
5555
uint8_t *data; // Buffer data (2 x capacity)
5656
uint32_t *index; // Index
57-
ssize_t head; // Head of the buffer
58-
ssize_t tail; // Buffer tail
59-
ssize_t cap; // Buffer capacity
57+
uint32_t *root; // Root index
58+
uint32_t head; // Head of the buffer
59+
uint32_t length; // Buffer length
60+
uint32_t cap; // Buffer capacity
6061

6162
public:
6263
explicit cbuffer_t();
@@ -66,11 +67,38 @@ namespace lsp
6667
void destroy();
6768

6869
public:
69-
void append(const void *src, ssize_t count);
70+
/**
71+
* Append buffer to compression buffer
72+
* @param src buffer to append
73+
* @param count the length of the buffer to append
74+
*/
75+
void append(const void *src, size_t count);
76+
77+
/**
78+
* Append single byte to compression buffer
79+
* @param v byte to append
80+
*/
7081
void append(uint8_t v);
71-
size_t lookup(ssize_t *out, const void *src, size_t avail);
82+
83+
/**
84+
* Lookup for byte sequence inside of the buffer
85+
* @param out relative offset of the sub-sequence in the buffer to the last byte stored in the buffer
86+
* @param src byte sequence to search inside of the buffer
87+
* @param avail number of bytes available in the sequence
88+
* @return the length of sub-sequence found in the buffer
89+
*/
90+
size_t lookup(size_t *out, const void *src, size_t avail);
91+
92+
/**
93+
* Cleanup state of the buffer
94+
*/
7295
void clear();
73-
inline size_t size() const { return tail - head; }
96+
97+
/**
98+
* Get size of data currently stored in the buffer
99+
* @return size of data currently stored in the buffer
100+
*/
101+
inline size_t size() const { return lsp_min(length, cap); }
74102

75103
} cbuffer_t;
76104

@@ -99,8 +127,9 @@ namespace lsp
99127
inline size_t size() const { return tail - head; }
100128

101129
} duffer_t;
102-
}
103-
}
130+
131+
} /* namespace resource */
132+
} /* namespace lsp */
104133

105134

106135

src/main/resource/Compressor.cpp

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ namespace lsp
200200
status_t res = STATUS_OK;
201201
const uint8_t *head = sTemp.data();
202202
const uint8_t *tail = &head[flength];
203-
ssize_t offset = 0, length = 0, append = 0;
204-
size_t rep = 0;
203+
size_t offset = 0;
205204

206205
// IF_TRACE(
207206
// wssize_t coffset = sOS.position();
@@ -213,38 +212,32 @@ namespace lsp
213212
while (head < tail)
214213
{
215214
// Estimate the length of match
216-
length = sBuffer.lookup(&offset, head, tail-head);
217-
if (length == 0)
218-
length = 1;
215+
size_t length = sBuffer.lookup(&offset, head, tail-head);
216+
const size_t emit = lsp_min(length, 1u);
219217

220218
// Calc number of repeats
221-
rep = calc_repeats(&head[length], tail);
222-
append = length + lsp_min(rep, REPEAT_BUF_MAX);
219+
size_t rep = calc_repeats(&head[emit], tail);
220+
size_t append = emit + lsp_min(rep, REPEAT_BUF_MAX);
223221

224222
// Estimate size of output
225-
size_t est1 = (est_uint(sBuffer.size() + *head, 5, 5) + est_uint(rep, 0, 4)) * length; // How many bits per octet
226-
size_t est2 = (offset < 0) ? est1 + 1 :
227-
est_uint(offset, 5, 5) +
228-
est_uint(length - 1, 5, 5) +
229-
est_uint(rep, 0, 4);
223+
const size_t est1 = est_uint(sBuffer.size() + *head, 5, 5); // How many bits used to encode buffer replay command
224+
const size_t est2 = (length > 0) ? est_uint(offset, 5, 5) + est_uint(emit - 1, 5, 5) : est1 + 1; // How many bits used to encode octet command
230225

231-
if (est1 > est2) // Prefer buffer over dictionary
226+
// IF_TRACE(
227+
// if (rep)
228+
// ++ repeats;
229+
// )
230+
231+
if (est2 < est1) // Prefer buffer replay over octet emission
232232
{
233-
// REPLAY
234-
// Offset
233+
// REPLAY BUFFER
234+
// Emit Offset
235235
if ((res = emit_uint(offset, 5, 5)) != STATUS_OK)
236236
break;
237-
// Length
238-
if ((res = emit_uint(length - 1, 5, 5)) != STATUS_OK)
239-
break;
240-
// Repeat
241-
if ((res = emit_uint(rep, 0, 4)) != STATUS_OK)
237+
// Emit Length
238+
if ((res = emit_uint(emit - 1, 5, 5)) != STATUS_OK)
242239
break;
243240

244-
// Append data to buffer
245-
sBuffer.append(head, append);
246-
head += length + rep;
247-
248241
// IF_TRACE(
249242
// ++ replays;
250243
// if (rep)
@@ -253,20 +246,22 @@ namespace lsp
253246
}
254247
else
255248
{
256-
// OCTET
257-
// Value
249+
// EMIT OCTET
250+
// Emit Value
258251
if ((res = emit_uint(sBuffer.size() + *head, 5, 5)) != STATUS_OK)
259252
break;
260-
// Repeat
261-
if ((res = emit_uint(rep, 0, 4)) != STATUS_OK)
262-
break;
263-
264-
// Append data to buffer
265-
sBuffer.append(head, append);
266-
head += length + rep;
267253

268254
// IF_TRACE(++octets);
269255
}
256+
257+
// Emit Repeat counter
258+
if ((res = emit_uint(rep, 0, 4)) != STATUS_OK)
259+
break;
260+
261+
262+
// Append data to buffer
263+
sBuffer.append(head, append);
264+
head += emit + rep;
270265
}
271266

272267
// Flush the bit sequence

0 commit comments

Comments
 (0)