mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Read LZW input bits 4 bytes at a time, not 1
name old speed new speed delta wuffs_gif_decode_1k_bw/clang 306MB/s ± 0% 295MB/s ± 0% -3.62% (p=0.008 n=5+5) wuffs_gif_decode_1k_color/clang 156MB/s ± 0% 163MB/s ± 0% +4.64% (p=0.008 n=5+5) wuffs_gif_decode_10k/clang 153MB/s ± 0% 179MB/s ± 0% +17.34% (p=0.008 n=5+5) wuffs_gif_decode_100k/clang 147MB/s ± 0% 155MB/s ± 0% +5.13% (p=0.008 n=5+5) wuffs_gif_decode_1000k/clang 151MB/s ± 1% 159MB/s ± 0% +5.34% (p=0.008 n=5+5) wuffs_gif_decode_1k_bw/gcc 319MB/s ± 0% 313MB/s ± 0% -1.79% (p=0.008 n=5+5) wuffs_gif_decode_1k_color/gcc 165MB/s ± 0% 173MB/s ± 0% +4.82% (p=0.008 n=5+5) wuffs_gif_decode_10k/gcc 171MB/s ± 1% 184MB/s ± 1% +7.48% (p=0.008 n=5+5) wuffs_gif_decode_100k/gcc 161MB/s ± 1% 173MB/s ± 1% +7.32% (p=0.008 n=5+5) wuffs_gif_decode_1000k/gcc 166MB/s ± 0% 179MB/s ± 1% +7.53% (p=0.008 n=5+5) wuffs_lzw_decode_20k/clang 167MB/s ± 0% 185MB/s ± 0% +10.85% (p=0.008 n=5+5) wuffs_lzw_decode_100k/clang 239MB/s ± 0% 260MB/s ± 0% +8.96% (p=0.008 n=5+5) wuffs_lzw_decode_20k/gcc 192MB/s ± 0% 211MB/s ± 0% +9.47% (p=0.008 n=5+5) wuffs_lzw_decode_100k/gcc 295MB/s ± 0% 300MB/s ± 0% +1.65% (p=0.008 n=5+5)
This commit is contained in:
1 parent
4e27b1280e
commit
ff92d22ea6
3 files changed
+88
-28
No files matched your search
@@ -416,6 +416,12 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: don't hard code the fact that "x |= n" implies "x >= n", for
|
||||
// constant n.
|
||||
if (op == t.IDPipeEq) && (rhs.ConstValue() != nil) {
|
||||
q.facts.appendFact(a.NewExpr(0, t.IDXBinaryGreaterEq, 0, 0, lhs.AsNode(), nil, rhs.AsNode(), nil))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -9420,8 +9420,8 @@ exit:
|
||||
// ---------------- Status Codes Implementations
|
||||
|
||||
const char* wuffs_lzw__error__bad_code = "?lzw: bad code";
|
||||
const char* wuffs_lzw__error__internal_error_inconsistent_i_j =
|
||||
"?lzw: internal error: inconsistent i:j";
|
||||
const char* wuffs_lzw__error__internal_error_inconsistent_i_o =
|
||||
"?lzw: internal error: inconsistent I/O";
|
||||
|
||||
// ---------------- Private Consts
|
||||
|
||||
@@ -9564,17 +9564,24 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self,
|
||||
v_bits = 0;
|
||||
v_n_bits = 0;
|
||||
while (true) {
|
||||
while (v_n_bits < v_width) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) {
|
||||
status = wuffs_base__suspension__short_read;
|
||||
goto suspend;
|
||||
if (((uint64_t)(io1_a_src - iop_a_src)) >= 4) {
|
||||
v_bits |= (((uint32_t)(wuffs_base__load_u32le(iop_a_src))) << v_n_bits);
|
||||
(iop_a_src += ((31 - v_n_bits) >> 3),
|
||||
wuffs_base__return_empty_struct());
|
||||
v_n_bits |= 24;
|
||||
} else {
|
||||
while (v_n_bits < v_width) {
|
||||
{
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
|
||||
if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) {
|
||||
status = wuffs_base__suspension__short_read;
|
||||
goto suspend;
|
||||
}
|
||||
uint8_t t_0 = *iop_a_src++;
|
||||
v_bits |= (((uint32_t)(t_0)) << v_n_bits);
|
||||
}
|
||||
uint8_t t_0 = *iop_a_src++;
|
||||
v_bits |= (((uint32_t)(t_0)) << v_n_bits);
|
||||
v_n_bits += 8;
|
||||
}
|
||||
v_n_bits += 8;
|
||||
}
|
||||
v_code = ((v_bits) & ((1 << (v_width)) - 1));
|
||||
v_bits >>= v_width;
|
||||
@@ -9636,6 +9643,16 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self,
|
||||
goto exit;
|
||||
}
|
||||
if (v_j > 4095) {
|
||||
while (v_n_bits >= 8) {
|
||||
v_n_bits -= 8;
|
||||
if (iop_a_src > io0_a_src) {
|
||||
(iop_a_src--, wuffs_base__return_empty_struct());
|
||||
} else {
|
||||
status = wuffs_lzw__error__internal_error_inconsistent_i_o;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
v_bits = ((v_bits) & ((1 << (v_n_bits)) - 1));
|
||||
self->private_impl.f_flush_j = v_j;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
|
||||
status = wuffs_lzw__decoder__flush(self, a_dst);
|
||||
@@ -9647,6 +9664,16 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self,
|
||||
}
|
||||
label_0_break:;
|
||||
if (v_j > 0) {
|
||||
while (v_n_bits >= 8) {
|
||||
v_n_bits -= 8;
|
||||
if (iop_a_src > io0_a_src) {
|
||||
(iop_a_src--, wuffs_base__return_empty_struct());
|
||||
} else {
|
||||
status = wuffs_lzw__error__internal_error_inconsistent_i_o;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
v_bits = ((v_bits) & ((1 << (v_n_bits)) - 1));
|
||||
self->private_impl.f_flush_j = v_j;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
status = wuffs_lzw__decoder__flush(self, a_dst);
|
||||
@@ -9738,7 +9765,7 @@ wuffs_lzw__decoder__flush(wuffs_lzw__decoder* self,
|
||||
v_j = self->private_impl.f_flush_j;
|
||||
while (true) {
|
||||
if (v_i > v_j) {
|
||||
status = wuffs_lzw__error__internal_error_inconsistent_i_j;
|
||||
status = wuffs_lzw__error__internal_error_inconsistent_i_o;
|
||||
goto exit;
|
||||
}
|
||||
v_s = wuffs_base__slice_u8__subslice_ij(
|
||||
|
||||
+43
-16
@@ -14,7 +14,7 @@
|
||||
|
||||
pub status "?bad code"
|
||||
|
||||
pri status "?internal error: inconsistent i:j"
|
||||
pri status "?internal error: inconsistent I/O"
|
||||
|
||||
pub struct decoder?(
|
||||
literal_width base.u32[..8],
|
||||
@@ -60,20 +60,27 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
|
||||
|
||||
// These variables produce src's bits in Least Significant Bits order.
|
||||
var bits base.u32
|
||||
var n_bits base.u32
|
||||
var n_bits base.u32[..31]
|
||||
|
||||
while true,
|
||||
pre n_bits < 8,
|
||||
{
|
||||
assert n_bits < (width + 8) via "a < (b + c): a < c; 0 <= b"()
|
||||
while n_bits < width,
|
||||
inv n_bits < (width + 8),
|
||||
post n_bits >= width,
|
||||
{
|
||||
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
while true {
|
||||
if args.src.available() >= 4 {
|
||||
// Read 4 bytes, using the "Variant 4" technique of
|
||||
// https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/
|
||||
bits |= (args.src.peek_u32le() as base.u32) ~mod<< n_bits
|
||||
args.src.skip_fast!(actual:(31 - n_bits) >> 3, worst_case:3)
|
||||
n_bits |= 24
|
||||
assert width <= n_bits via "a <= b: a <= c; c <= b"(c:12)
|
||||
assert n_bits >= width via "a >= b: b <= a"()
|
||||
} else {
|
||||
while n_bits < width,
|
||||
post n_bits >= width,
|
||||
{
|
||||
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
}
|
||||
|
||||
var code base.u32[..4095] = bits.low_bits(n:width)
|
||||
bits >>= width
|
||||
n_bits -= width
|
||||
@@ -100,7 +107,6 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
|
||||
save_code = end_code
|
||||
prev_code = 0
|
||||
width = literal_width + 1
|
||||
|
||||
} else if code <= save_code {
|
||||
var c base.u32[..4095] = code
|
||||
if code == save_code {
|
||||
@@ -119,7 +125,6 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
|
||||
j = (o + 1) & 8191
|
||||
|
||||
while c >= clear_code,
|
||||
inv n_bits < 8,
|
||||
post c < 256 via "a < b: a < c; c <= b"(c:clear_code),
|
||||
{
|
||||
this.output[o] = this.suffixes[c]
|
||||
@@ -155,6 +160,17 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
|
||||
// less than 4096 and output's length is 8192, so a conservative
|
||||
// threshold is ensuring that j <= 4095.
|
||||
if j > 4095 {
|
||||
// Rewind args.src, if we've read too many bits.
|
||||
while n_bits >= 8 {
|
||||
n_bits -= 8
|
||||
if args.src.can_undo_byte() {
|
||||
args.src.undo_byte!()
|
||||
} else {
|
||||
return status "?internal error: inconsistent I/O"
|
||||
}
|
||||
}
|
||||
bits = bits.low_bits(n:n_bits)
|
||||
|
||||
this.flush_j = j
|
||||
this.flush!??(dst:args.dst)
|
||||
j = 0
|
||||
@@ -162,6 +178,17 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
|
||||
}
|
||||
|
||||
if j > 0 {
|
||||
// Rewind args.src, if we've read too many bits.
|
||||
while n_bits >= 8 {
|
||||
n_bits -= 8
|
||||
if args.src.can_undo_byte() {
|
||||
args.src.undo_byte!()
|
||||
} else {
|
||||
return status "?internal error: inconsistent I/O"
|
||||
}
|
||||
}
|
||||
bits = bits.low_bits(n:n_bits)
|
||||
|
||||
this.flush_j = j
|
||||
this.flush!??(dst:args.dst)
|
||||
}
|
||||
@@ -172,7 +199,7 @@ pri func decoder.flush!??(dst base.io_writer) {
|
||||
var j base.u32[..8191] = this.flush_j
|
||||
while true {
|
||||
if i > j {
|
||||
return status "?internal error: inconsistent i:j"
|
||||
return status "?internal error: inconsistent I/O"
|
||||
}
|
||||
var s slice base.u8 = this.output[i:j]
|
||||
var n base.u64 = args.dst.copy_from_slice!(s:s)
|
||||
|
||||
Reference in new issue
Block a user