From 622a76d4c07ecbdd1611f4441138597a04ec72b7 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 16 Dec 2018 09:57:15 +1100 Subject: [PATCH] Avoid buffer copies from LZW to GIF MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old speed new speed delta wuffs_lzw_decode_20k/clang 213MB/s ± 0% 224MB/s ± 1% +4.89% (p=0.008 n=5+5) wuffs_lzw_decode_100k/clang 359MB/s ± 1% 393MB/s ± 2% +9.36% (p=0.008 n=5+5) wuffs_lzw_decode_20k/gcc 188MB/s ± 0% 187MB/s ± 0% -0.70% (p=0.008 n=5+5) wuffs_lzw_decode_100k/gcc 223MB/s ± 2% 224MB/s ± 0% ~ (p=0.690 n=5+5) wuffs_gif_decode_1k_bw/clang 227MB/s ± 1% 240MB/s ± 0% +5.89% (p=0.008 n=5+5) wuffs_gif_decode_1k_color/clang 117MB/s ± 0% 125MB/s ± 0% +6.16% (p=0.008 n=5+5) wuffs_gif_decode_10k_bgra/clang 567MB/s ± 1% 599MB/s ± 0% +5.56% (p=0.008 n=5+5) wuffs_gif_decode_10k_indexed/clang 152MB/s ± 0% 159MB/s ± 1% +4.76% (p=0.008 n=5+5) wuffs_gif_decode_100k_artificial/clang 455MB/s ± 1% 504MB/s ± 0% +10.93% (p=0.008 n=5+5) wuffs_gif_decode_100k_realistic/clang 178MB/s ± 2% 194MB/s ± 0% +9.00% (p=0.008 n=5+5) wuffs_gif_decode_1000k/clang 179MB/s ± 1% 197MB/s ± 0% +9.81% (p=0.016 n=5+4) wuffs_gif_decode_anim_screencap/clang 952MB/s ± 0% 1050MB/s ± 1% +10.29% (p=0.008 n=5+5) wuffs_gif_decode_1k_bw/gcc 243MB/s ± 1% 252MB/s ± 0% +3.71% (p=0.008 n=5+5) wuffs_gif_decode_1k_color/gcc 130MB/s ± 0% 133MB/s ± 1% +2.69% (p=0.008 n=5+5) wuffs_gif_decode_10k_bgra/gcc 537MB/s ± 0% 547MB/s ± 1% +1.93% (p=0.008 n=5+5) wuffs_gif_decode_10k_indexed/gcc 163MB/s ± 0% 167MB/s ± 1% +2.40% (p=0.008 n=5+5) wuffs_gif_decode_100k_artificial/gcc 458MB/s ± 0% 471MB/s ± 0% +2.90% (p=0.016 n=5+4) wuffs_gif_decode_100k_realistic/gcc 186MB/s ± 0% 191MB/s ± 0% +3.08% (p=0.008 n=5+5) wuffs_gif_decode_1000k/gcc 189MB/s ± 0% 194MB/s ± 0% +2.70% (p=0.008 n=5+5) wuffs_gif_decode_anim_screencap/gcc 976MB/s ± 0% 1010MB/s ± 0% +3.49% (p=0.008 n=5+5) --- lang/parse/parse.go | 6 +- release/c/wuffs-unsupported-snapshot.h | 103 +++++++++++++++++-------- std/gif/decode_gif.wuffs | 16 ++-- std/lzw/decode_lzw.wuffs | 21 ++++- 4 files changed, 94 insertions(+), 52 deletions(-) diff --git a/lang/parse/parse.go b/lang/parse/parse.go index a653b5e8..600c4807 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -24,6 +24,8 @@ import ( ) type Options struct { + // TODO: drop AllowBuiltIns as those names are package-qualified + // ("base.u32" and not a bare "u32")? AllowBuiltIns bool AllowDoubleUnderscoreNames bool } @@ -162,10 +164,6 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { return nil, fmt.Errorf(`parse: built-in %q used for func receiver at %s:%d`, p.tm.ByID(id0), p.filename, p.line()) } - if id1.IsBuiltIn() { - return nil, fmt.Errorf(`parse: built-in %q used for func name at %s:%d`, - p.tm.ByID(id1), p.filename, p.line()) - } } if !p.opts.AllowDoubleUnderscoreNames && isDoubleUnderscore(p.tm.ByID(id1)) { return nil, fmt.Errorf(`parse: double-underscore %q used for func name at %s:%d`, diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index 61ec39df..cd05da55 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -2564,6 +2564,12 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, wuffs_base__io_writer a_dst, wuffs_base__io_reader a_src); +WUFFS_BASE__MAYBE_STATIC uint64_t // +wuffs_lzw__decoder__available(const wuffs_lzw__decoder* self); + +WUFFS_BASE__MAYBE_STATIC wuffs_base__slice_u8 // +wuffs_lzw__decoder__take_all(wuffs_lzw__decoder* self); + // ---------------- Struct Definitions // These structs' fields, and the sizeof them, are private implementation @@ -2606,7 +2612,6 @@ struct wuffs_lzw__decoder__struct { struct { uint32_t coro_susp_point; uint32_t v_i; - uint32_t v_j; } c_flush[1]; } private_impl; @@ -2628,6 +2633,16 @@ struct wuffs_lzw__decoder__struct { return wuffs_lzw__decoder__decode(this, a_dst, a_src); } + inline uint64_t // + available() const { + return wuffs_lzw__decoder__available(this); + } + + inline wuffs_base__slice_u8 // + take_all() { + return wuffs_lzw__decoder__take_all(this); + } + #endif // __cplusplus }; // struct wuffs_lzw__decoder__struct @@ -2759,9 +2774,7 @@ struct wuffs_gif__decoder__struct { wuffs_base__range_ie_u32 f_dirty_y; uint64_t f_compressed_ri; uint64_t f_compressed_wi; - uint64_t f_uncompressed_wi; uint8_t f_compressed[4096]; - uint8_t f_uncompressed[4096]; uint8_t f_palettes[2][1024]; uint8_t f_dst_palette[1024]; wuffs_base__pixel_swizzler f_swizzler; @@ -7368,7 +7381,6 @@ wuffs_lzw__decoder__flush(wuffs_lzw__decoder* self, wuffs_base__status status = NULL; uint32_t v_i; - uint32_t v_j; wuffs_base__slice_u8 v_s; uint64_t v_n; @@ -7395,7 +7407,6 @@ wuffs_lzw__decoder__flush(wuffs_lzw__decoder* self, uint32_t coro_susp_point = self->private_impl.c_flush[0].coro_susp_point; if (coro_susp_point) { v_i = self->private_impl.c_flush[0].v_i; - v_j = self->private_impl.c_flush[0].v_j; v_s = ((wuffs_base__slice_u8){}); v_n = 0; } else { @@ -7405,9 +7416,8 @@ wuffs_lzw__decoder__flush(wuffs_lzw__decoder* self, WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; v_i = 0; - v_j = self->private_impl.f_flush_j; - while (true) { - if (v_i > v_j) { + while (self->private_impl.f_flush_j > 0) { + if (v_i > self->private_impl.f_flush_j) { status = wuffs_lzw__error__internal_error_inconsistent_i_o; goto exit; } @@ -7416,7 +7426,7 @@ wuffs_lzw__decoder__flush(wuffs_lzw__decoder* self, .ptr = self->private_impl.f_output, .len = 8199, }), - v_i, v_j); + v_i, self->private_impl.f_flush_j); v_n = wuffs_base__io_writer__copy_from_slice(&iop_a_dst, io1_a_dst, v_s); if (v_n == ((uint64_t)(v_s.len))) { status = NULL; @@ -7437,7 +7447,6 @@ wuffs_lzw__decoder__flush(wuffs_lzw__decoder* self, suspend: self->private_impl.c_flush[0].coro_susp_point = coro_susp_point; self->private_impl.c_flush[0].v_i = v_i; - self->private_impl.c_flush[0].v_j = v_j; goto exit; exit: @@ -7449,6 +7458,42 @@ exit: return status; } +// -------- func lzw.decoder.available + +WUFFS_BASE__MAYBE_STATIC uint64_t // +wuffs_lzw__decoder__available(const wuffs_lzw__decoder* self) { + if (!self) { + return 0; + } + if (self->private_impl.magic != WUFFS_BASE__MAGIC) { + return 0; + } + + return ((uint64_t)(self->private_impl.f_flush_j)); +} + +// -------- func lzw.decoder.take_all + +WUFFS_BASE__MAYBE_STATIC wuffs_base__slice_u8 // +wuffs_lzw__decoder__take_all(wuffs_lzw__decoder* self) { + if (!self) { + return ((wuffs_base__slice_u8){}); + } + if (self->private_impl.magic != WUFFS_BASE__MAGIC) { + return ((wuffs_base__slice_u8){}); + } + + wuffs_base__slice_u8 v_s; + + v_s = wuffs_base__slice_u8__subslice_j(((wuffs_base__slice_u8){ + .ptr = self->private_impl.f_output, + .len = 8199, + }), + self->private_impl.f_flush_j); + self->private_impl.f_flush_j = 0; + return v_s; +} + #endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__LZW) #if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__GIF) @@ -9210,6 +9255,7 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(iop_v_r); WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(io1_v_r); wuffs_base__status v_z; + wuffs_base__slice_u8 v_uncompressed; uint8_t* iop_a_src = NULL; uint8_t* io0_a_src = NULL; @@ -9244,11 +9290,13 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, v_w = ((wuffs_base__io_writer){}); v_r = ((wuffs_base__io_reader){}); v_z = self->private_impl.c_decode_id_part1[0].v_z; + v_uncompressed = ((wuffs_base__slice_u8){}); } else { v_dst_palette = ((wuffs_base__slice_u8){}); v_compressed = ((wuffs_base__slice_u8){}); v_w = ((wuffs_base__io_writer){}); v_r = ((wuffs_base__io_reader){}); + v_uncompressed = ((wuffs_base__slice_u8){}); } switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; @@ -9455,8 +9503,7 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, v_r = ((wuffs_base__io_reader){}); if ((self->private_impl.f_compressed_ri > self->private_impl.f_compressed_wi) || - (self->private_impl.f_compressed_wi > 4096) || - (self->private_impl.f_uncompressed_wi > 4096)) { + (self->private_impl.f_compressed_wi > 4096)) { status = wuffs_gif__error__internal_error_inconsistent_ri_wi; goto exit; } @@ -9466,12 +9513,12 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, uint8_t* o_0_io1_v_w = io1_v_w; wuffs_base__io_writer__set( &v_w, &u_w, &iop_v_w, &io1_v_w, - wuffs_base__slice_u8__subslice_i( + wuffs_base__slice_u8__subslice_j( ((wuffs_base__slice_u8){ - .ptr = self->private_impl.f_uncompressed, + .ptr = self->private_impl.f_compressed, .len = 4096, }), - self->private_impl.f_uncompressed_wi)); + 0)); { wuffs_base__io_reader o_1_v_r = v_r; uint8_t* o_1_iop_v_r = iop_v_r; @@ -9494,9 +9541,6 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, iop_v_r = u_r.data.ptr + u_r.meta.ri; v_z = t_5; } - wuffs_base__u64__sat_add_indirect( - &self->private_impl.f_uncompressed_wi, - ((uint64_t)(iop_v_w - v_w.private_impl.mark))); wuffs_base__u64__sat_add_indirect( &self->private_impl.f_compressed_ri, ((uint64_t)(iop_v_r - v_r.private_impl.mark))); @@ -9508,22 +9552,15 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, iop_v_w = o_0_iop_v_w; io1_v_w = o_0_io1_v_w; } - if (self->private_impl.f_uncompressed_wi > 4096) { - status = wuffs_gif__error__internal_error_inconsistent_ri_wi; - goto exit; + if (wuffs_lzw__decoder__available(&self->private_impl.f_lzw) > 0) { + v_uncompressed = + wuffs_lzw__decoder__take_all(&self->private_impl.f_lzw); + status = wuffs_gif__decoder__copy_to_image_buffer(self, a_dst, + v_uncompressed); + if (status) { + goto exit; + } } - status = wuffs_gif__decoder__copy_to_image_buffer( - self, a_dst, - wuffs_base__slice_u8__subslice_j( - ((wuffs_base__slice_u8){ - .ptr = self->private_impl.f_uncompressed, - .len = 4096, - }), - self->private_impl.f_uncompressed_wi)); - if (status) { - goto exit; - } - self->private_impl.f_uncompressed_wi = 0; if (wuffs_base__status__is_ok(v_z)) { self->private_impl.f_previous_lzw_decode_ended_abruptly = false; if (v_block_size > 0) { diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 351d26a8..27308248 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -109,10 +109,7 @@ pub struct decoder?( compressed_ri base.u64, compressed_wi base.u64, - uncompressed_wi base.u64, - compressed array[4096] base.u8, - uncompressed array[4096] base.u8, // palettes[0] and palettes[1] are the Global and Local Color Table. palettes array[2] array[4 * 256] base.u8, @@ -657,23 +654,20 @@ pri func decoder.decode_id_part1!??(dst ptr base.pixel_buffer, src base.io_reade while:inner true { var w base.io_writer var r base.io_reader - if (this.compressed_ri > this.compressed_wi) or - (this.compressed_wi > 4096) or (this.uncompressed_wi > 4096) { + if (this.compressed_ri > this.compressed_wi) or (this.compressed_wi > 4096) { return "?internal error: inconsistent ri/wi" } - io_bind (io:w, data:this.uncompressed[this.uncompressed_wi:]) { + io_bind (io:w, data:this.compressed[:0]) { // TODO: delete. io_bind (io:r, data:this.compressed[this.compressed_ri:this.compressed_wi]) { var z base.status =? this.lzw.decode!??(dst:w, src:r) - this.uncompressed_wi ~sat+= w.since_mark().length() this.compressed_ri ~sat+= r.since_mark().length() } } - if this.uncompressed_wi > 4096 { - return "?internal error: inconsistent ri/wi" + if this.lzw.available() > 0 { + var uncompressed slice base.u8 = this.lzw.take_all!() + this.copy_to_image_buffer!?(pb:args.dst, src:uncompressed) } - this.copy_to_image_buffer!?(pb:args.dst, src:this.uncompressed[:this.uncompressed_wi]) - this.uncompressed_wi = 0 if z.is_ok() { this.previous_lzw_decode_ended_abruptly = false diff --git a/std/lzw/decode_lzw.wuffs b/std/lzw/decode_lzw.wuffs index f0d8b50d..7e00dd7e 100644 --- a/std/lzw/decode_lzw.wuffs +++ b/std/lzw/decode_lzw.wuffs @@ -252,12 +252,11 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) { pri func decoder.flush!??(dst base.io_writer) { var i base.u32[..8191] - var j base.u32[..8191] = this.flush_j - while true { - if i > j { + while this.flush_j > 0 { + if i > this.flush_j { return "?internal error: inconsistent I/O" } - var s slice base.u8 = this.output[i:j] + var s slice base.u8 = this.output[i:this.flush_j] var n base.u64 = args.dst.copy_from_slice!(s:s) if n == s.length() { return ok @@ -266,3 +265,17 @@ pri func decoder.flush!??(dst base.io_writer) { yield base."$short write" } } + +// TODO: enforce that available and take_all are only valid to call if decode +// was passed a 0-length dst base.io_writer? The (non-obvious) hard part is to +// enforce that without regressing benchmarks. + +pub func decoder.available() base.u64 { + return this.flush_j as base.u64 +} + +pub func decoder.take_all!() slice base.u8 { + var s slice base.u8 = this.output[:this.flush_j] + this.flush_j = 0 + return s +}