std/gif: remove the std/lzw dependency

name                                         old speed      new speed      delta

wuffs_gif_decode_1k_bw/clang11                683MB/s ± 0%   682MB/s ± 0%     ~     (p=0.690 n=5+5)
wuffs_gif_decode_1k_color_full_init/clang11   168MB/s ± 0%   184MB/s ± 0%  +10.01%  (p=0.008 n=5+5)
wuffs_gif_decode_1k_color_part_init/clang11   204MB/s ± 0%   230MB/s ± 0%  +12.77%  (p=0.008 n=5+5)
wuffs_gif_decode_10k_bgra/clang11             746MB/s ± 0%   819MB/s ± 0%   +9.90%  (p=0.008 n=5+5)
wuffs_gif_decode_10k_indexed/clang11          200MB/s ± 0%   218MB/s ± 0%   +8.97%  (p=0.008 n=5+5)
wuffs_gif_decode_20k/clang11                  246MB/s ± 0%   257MB/s ± 0%   +4.33%  (p=0.008 n=5+5)
wuffs_gif_decode_100k_artificial/clang11      543MB/s ± 0%   562MB/s ± 0%   +3.44%  (p=0.008 n=5+5)
wuffs_gif_decode_100k_realistic/clang11       210MB/s ± 0%   220MB/s ± 0%   +4.90%  (p=0.008 n=5+5)
wuffs_gif_decode_1000k_full_init/clang11      214MB/s ± 0%   224MB/s ± 0%   +4.96%  (p=0.008 n=5+5)
wuffs_gif_decode_1000k_part_init/clang11      214MB/s ± 0%   224MB/s ± 0%   +4.98%  (p=0.008 n=5+5)
wuffs_gif_decode_anim_screencap/clang11      1.24GB/s ± 0%  1.29GB/s ± 0%   +3.85%  (p=0.008 n=5+5)

wuffs_gif_decode_1k_bw/gcc10                  649MB/s ± 0%   588MB/s ± 0%   -9.42%  (p=0.008 n=5+5)
wuffs_gif_decode_1k_color_full_init/gcc10     167MB/s ± 0%   198MB/s ± 0%  +18.57%  (p=0.008 n=5+5)
wuffs_gif_decode_1k_color_part_init/gcc10     195MB/s ± 0%   251MB/s ± 0%  +28.64%  (p=0.008 n=5+5)
wuffs_gif_decode_10k_bgra/gcc10               753MB/s ± 0%   871MB/s ± 0%  +15.64%  (p=0.008 n=5+5)
wuffs_gif_decode_10k_indexed/gcc10            199MB/s ± 0%   233MB/s ± 0%  +16.86%  (p=0.008 n=5+5)
wuffs_gif_decode_20k/gcc10                    258MB/s ± 0%   265MB/s ± 0%   +2.93%  (p=0.008 n=5+5)
wuffs_gif_decode_100k_artificial/gcc10        560MB/s ± 0%   561MB/s ± 0%   +0.19%  (p=0.008 n=5+5)
wuffs_gif_decode_100k_realistic/gcc10         221MB/s ± 0%   227MB/s ± 0%   +2.65%  (p=0.008 n=5+5)
wuffs_gif_decode_1000k_full_init/gcc10        227MB/s ± 0%   232MB/s ± 0%   +2.29%  (p=0.008 n=5+5)
wuffs_gif_decode_1000k_part_init/gcc10        227MB/s ± 0%   232MB/s ± 0%   +2.28%  (p=0.008 n=5+5)
wuffs_gif_decode_anim_screencap/gcc10        1.29GB/s ± 0%  1.28GB/s ± 0%   -0.22%  (p=0.008 n=5+5)
This commit is contained in:
Nigel Tao committed 2023-10-04 20:19:25 +11:00
1 parent 6f79c182ea
commit 8e42e01d3c
17 files changed
+600 -102

No files matched your search

+4 -1
View File
@@ -12,8 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// TODO: remove this no-longer-used `use` line. That affects the topological
// sort of std/etc packages, which makes a large diff for the generated C code.
use "std/lzw"
pub status "#bad LZW code"
pub status "#bad extension label"
pub status "#bad frame size"
pub status "#bad graphic control"
@@ -22,7 +25,7 @@ pub status "#bad literal width"
pub status "#bad palette"
pub status "#truncated input"
pri status "#internal error: inconsistent ri/wi"
pri status "#internal error: inconsistent I/O"
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
+52 -27
View File
@@ -35,9 +35,13 @@ pub struct decoder? implements base.image_decoder(
quirks : array[QUIRKS_COUNT] base.bool,
delayed_num_decoded_frames : base.bool,
previous_lzw_decode_ended_abruptly : base.bool,
seen_header : base.bool,
delayed_num_decoded_frames : base.bool,
seen_header : base.bool,
// This field used to be called "previous_lzw_decode_ended_abruptly".
// It's no longer used, but removing it changes the subsequent field
// offsets, which can worsen the benchmarks.
ignored_but_affects_benchmarks : base.bool,
has_global_palette : base.bool,
@@ -79,6 +83,20 @@ pub struct decoder? implements base.image_decoder(
swizzler : base.pixel_swizzler,
lzw_pending_literal_width_plus_one : base.u32[..= 9],
lzw_literal_width : base.u32[..= 8],
lzw_clear_code : base.u32[..= 256],
lzw_end_code : base.u32[..= 257],
lzw_save_code : base.u32[..= 4096],
lzw_prev_code : base.u32[..= 4095],
lzw_width : base.u32[..= 12],
lzw_bits : base.u32,
lzw_n_bits : base.u32[..= 31],
lzw_output_ri : base.u32[..= 8191],
lzw_output_wi : base.u32[..= 8191],
lzw_read_from_return_value : base.u32,
lzw_prefixes : array[4096] base.u16[..= 4095],
util : base.utility,
) + (
compressed : array[4096] base.u8,
@@ -88,7 +106,9 @@ pub struct decoder? implements base.image_decoder(
// dst_palette is the swizzled color table.
dst_palette : array[4 * 256] base.u8,
lzw : lzw.decoder,
lzw_suffixes : array[4096] array[8] base.u8,
lzw_lm1s : array[4096] base.u16,
lzw_output : array[8192 + 7] base.u8,
)
pub func decoder.get_quirk(key: base.u32) base.u64 {
@@ -905,8 +925,8 @@ pri func decoder.decode_id_part1?(dst: ptr base.pixel_buffer, src: base.io_reade
// code (i.e. returning the "ok" status code), the LZW decoder is stuck in
// a coroutine-in-progress lzw_decoder.decode call, still waiting for that
// end code. To cancel that coroutine, we reset the LZW decoder.
if this.previous_lzw_decode_ended_abruptly {
this.lzw.reset!()
if this.ignored_but_affects_benchmarks {
// No-op. This used to be "this.lzw.reset!()".
}
// Process the LZW literal width. The spec says that "images which have one
@@ -916,9 +936,9 @@ pri func decoder.decode_id_part1?(dst: ptr base.pixel_buffer, src: base.io_reade
if lw > 8 {
return "#bad literal width"
}
this.lzw.set_quirk!(key: lzw.QUIRK_LITERAL_WIDTH_PLUS_ONE, value: (1 + lw) as base.u64)
this.lzw_pending_literal_width_plus_one = (1 + lw) as base.u32
this.previous_lzw_decode_ended_abruptly = true
this.ignored_but_affects_benchmarks = true
}
pri func decoder.decode_id_part2?(dst: ptr base.pixel_buffer, src: base.io_reader, workbuf: slice base.u8) {
@@ -928,10 +948,9 @@ pri func decoder.decode_id_part2?(dst: ptr base.pixel_buffer, src: base.io_reade
var n_compressed : base.u64
var r : base.io_reader
var mark : base.u64
var lzw_status : base.status
var copy_status : base.status
var uncompressed : roslice base.u8
this.lzw_init!()
need_block_size = true
while.outer true {
if need_block_size {
@@ -972,50 +991,56 @@ pri func decoder.decode_id_part2?(dst: ptr base.pixel_buffer, src: base.io_reade
while.inner true {
if (this.compressed_ri > this.compressed_wi) or (this.compressed_wi > 4096) {
return "#internal error: inconsistent ri/wi"
return "#internal error: inconsistent I/O"
}
io_bind (io: r, data: this.compressed[this.compressed_ri .. this.compressed_wi], history_position: 0) {
mark = r.mark()
lzw_status =? this.lzw.transform_io?(
dst: this.util.empty_io_writer(), src: r, workbuf: this.util.empty_slice_u8())
this.lzw_read_from!(src: r)
this.compressed_ri ~sat+= r.count_since(mark: mark)
}
uncompressed = this.lzw.flush!()
if uncompressed.length() > 0 {
copy_status = this.copy_to_image_buffer!(pb: args.dst, src: uncompressed)
if this.lzw_output_ri < this.lzw_output_wi {
copy_status = this.copy_to_image_buffer!(
pb: args.dst, src: this.lzw_output[this.lzw_output_ri .. this.lzw_output_wi])
if copy_status.is_error() {
return copy_status
}
this.lzw_output_ri = 0
this.lzw_output_wi = 0
}
if lzw_status.is_ok() {
this.previous_lzw_decode_ended_abruptly = false
if this.lzw_read_from_return_value == 0 {
this.ignored_but_affects_benchmarks = false
// Skip any trailing blocks.
if need_block_size or (block_size > 0) {
args.src.skip_u32?(n: block_size as base.u32)
this.skip_blocks?(src: args.src)
}
break.outer
} else if lzw_status == base."$short read" {
continue.outer
} else if lzw_status == base."$short write" {
} else if this.lzw_read_from_return_value == 1 {
// This is an LZW flush.
continue.inner
} else if this.lzw_read_from_return_value == 2 {
// This is an LZW "$short read" equivalent.
continue.outer
} else if this.quirks[QUIRK_IGNORE_TOO_MUCH_PIXEL_DATA - QUIRKS_BASE] and
(this.dst_y >= this.frame_rect_y1) and (this.interlace == 0) {
// It's invalid LZW-compressed data, but we still have a full
// frame and have opted in to QUIRK_IGNORE_TOO_MUCH_PIXEL_DATA,
// so treat it like the lzw_status.is_ok() case, other than not
// clearing this.previous_lzw_decode_ended_abruptly.
// so treat it like the OK case, other than not clearing
// this.ignored_but_affects_benchmarks.
if need_block_size or (block_size > 0) {
args.src.skip_u32?(n: block_size as base.u32)
this.skip_blocks?(src: args.src)
}
break.outer
} else if this.lzw_read_from_return_value == 3 {
return "#truncated input"
} else if this.lzw_read_from_return_value == 4 {
return "#bad LZW code"
}
return lzw_status
return "#internal error: inconsistent I/O"
} endwhile.inner
} endwhile.outer
@@ -1137,7 +1162,7 @@ pri func decoder.copy_to_image_buffer!(pb: ptr base.pixel_buffer, src: roslice b
if args.src.length() == src_ri {
break
} else if args.src.length() < src_ri {
return "#internal error: inconsistent ri/wi"
return "#internal error: inconsistent I/O"
}
// Second, skip over src for that part of the frame rect that is
@@ -1161,7 +1186,7 @@ pri func decoder.copy_to_image_buffer!(pb: ptr base.pixel_buffer, src: roslice b
}
if src_ri <> args.src.length() {
return "#internal error: inconsistent ri/wi"
return "#internal error: inconsistent I/O"
}
break
} endwhile
+264
View File
@@ -0,0 +1,264 @@
// Copyright 2023 The Wuffs Authors.
//
// Licensed 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
//
// https://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.
// --------
// This file (in std/gif) is a fork of std/lzw/decode_lzw.wuffs, inlined so
// that the gif 'caller' can take the this.lzw_output[this.lzw_output_ri ..
// this.lzw_output_wi] slice directly. Wuffs version 0.3 provided this as the
// lzw.decoder.flush!() method, but we'd like to deprecate (and eventually
// remove) methods that return anything pointer-y (including slices).
//
// It also changes the ? methods to ! methods, so that the LZW-decoder does not
// need a "this.lzw.reset!()" call in case of a recoverable error.
pri func decoder.lzw_init!() {
var i : base.u32[..= 8191]
this.lzw_literal_width = 8
if this.lzw_pending_literal_width_plus_one > 0 {
this.lzw_literal_width = this.lzw_pending_literal_width_plus_one - 1
}
this.lzw_clear_code = (1 as base.u32) << this.lzw_literal_width
this.lzw_end_code = this.lzw_clear_code + 1
this.lzw_save_code = this.lzw_end_code
this.lzw_prev_code = this.lzw_end_code
this.lzw_width = this.lzw_literal_width + 1
this.lzw_bits = 0
this.lzw_n_bits = 0
this.lzw_output_ri = 0
this.lzw_output_wi = 0
i = 0
while i < this.lzw_clear_code {
assert i < 256 via "a < b: a < c; c <= b"(c: this.lzw_clear_code)
this.lzw_lm1s[i] = 0
this.lzw_suffixes[i][0] = i as base.u8
i += 1
} endwhile
}
pri func decoder.lzw_read_from!(src: base.io_reader) {
var clear_code : base.u32[..= 256]
var end_code : base.u32[..= 257]
var save_code : base.u32[..= 4096]
var prev_code : base.u32[..= 4095]
var width : base.u32[..= 12]
var bits : base.u32
var n_bits : base.u32[..= 31]
var output_wi : base.u32[..= 8191]
var code : base.u32[..= 4095]
var c : base.u32[..= 4095]
var o : base.u32[..= 8191]
var steps : base.u32
var first_byte : base.u8
var lm1_b : base.u16[..= 4095]
var lm1_a : base.u16[..= 4095]
clear_code = this.lzw_clear_code
end_code = this.lzw_end_code
save_code = this.lzw_save_code
prev_code = this.lzw_prev_code
width = this.lzw_width
bits = this.lzw_bits
n_bits = this.lzw_n_bits
output_wi = this.lzw_output_wi
while true {
if n_bits < width {
assert n_bits < 12 via "a < b: a < c; c <= b"(c: width)
if args.src.length() >= 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() ~mod<< n_bits
args.src.skip_u32_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 if args.src.length() <= 0 {
if args.src.is_closed() {
this.lzw_read_from_return_value = 3
} else {
this.lzw_read_from_return_value = 2
}
break
} else {
bits |= args.src.peek_u8_as_u32() << n_bits
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
n_bits += 8
if n_bits >= width {
// No-op.
} else if args.src.length() <= 0 {
if args.src.is_closed() {
this.lzw_read_from_return_value = 3
} else {
this.lzw_read_from_return_value = 2
}
break
} else {
bits |= args.src.peek_u8_as_u32() << n_bits
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
n_bits += 8
assert width <= n_bits via "a <= b: a <= c; c <= b"(c: 12)
assert n_bits >= width via "a >= b: b <= a"()
// This if condition is always false, but for some unknown
// reason, removing it worsens the benchmarks slightly.
if n_bits < width {
this.lzw_read_from_return_value = 5
break
}
}
}
}
code = bits.low_bits(n: width)
bits >>= width
n_bits -= width
if code < clear_code {
assert code < 256 via "a < b: a < c; c <= b"(c: clear_code)
this.lzw_output[output_wi] = code as base.u8
output_wi = (output_wi + 1) & 8191
if save_code <= 4095 {
lm1_a = (this.lzw_lm1s[prev_code] ~mod+ 1) & 4095
this.lzw_lm1s[save_code] = lm1_a
if (lm1_a % 8) <> 0 {
this.lzw_prefixes[save_code] = this.lzw_prefixes[prev_code]
this.lzw_suffixes[save_code] = this.lzw_suffixes[prev_code]
this.lzw_suffixes[save_code][lm1_a % 8] = code as base.u8
} else {
this.lzw_prefixes[save_code] = prev_code as base.u16
this.lzw_suffixes[save_code][0] = code as base.u8
}
save_code += 1
if width < 12 {
width += 1 & (save_code >> width)
}
prev_code = code
}
} else if code <= end_code {
if code == end_code {
this.lzw_read_from_return_value = 0
break
}
save_code = end_code
prev_code = end_code
width = this.lzw_literal_width + 1
} else if code <= save_code {
c = code
if code == save_code {
c = prev_code
}
// Letting old_wi and new_wi denote the values of output_wi before
// and after these two lines of code, the decoded bytes will be
// written to output[old_wi:new_wi]. They will be written
// back-to-front, 8 bytes at a time, starting by writing
// output[o:o + 8], which will contain output[new_wi - 1].
//
// In the special case that code == save_code, the decoded bytes
// contain an extra copy (at the end) of the first byte, and will
// be written to output[old_wi:new_wi + 1].
o = (output_wi + ((this.lzw_lm1s[c] as base.u32) & 0xFFFF_FFF8)) & 8191
output_wi = (output_wi + 1 + (this.lzw_lm1s[c] as base.u32)) & 8191
steps = (this.lzw_lm1s[c] as base.u32) >> 3
while true {
assert o <= (o + 8) via "a <= (a + b): 0 <= b"(b: 8)
// The final "8" is redundant semantically, but helps the
// wuffs-c code generator recognize that both slices have the
// same constant length, and hence produce efficient C code.
this.lzw_output[o .. o + 8].copy_from_slice!(s: this.lzw_suffixes[c][.. 8])
if steps <= 0 {
break
}
steps -= 1
// This line is essentially "o -= 8". The "& 8191" is a no-op
// in practice, but is necessary for the overflow checker.
o = (o ~mod- 8) & 8191
c = this.lzw_prefixes[c] as base.u32
} endwhile
first_byte = this.lzw_suffixes[c][0]
if code == save_code {
this.lzw_output[output_wi] = first_byte
output_wi = (output_wi + 1) & 8191
}
if save_code <= 4095 {
lm1_b = (this.lzw_lm1s[prev_code] ~mod+ 1) & 4095
this.lzw_lm1s[save_code] = lm1_b
if (lm1_b % 8) <> 0 {
this.lzw_prefixes[save_code] = this.lzw_prefixes[prev_code]
this.lzw_suffixes[save_code] = this.lzw_suffixes[prev_code]
this.lzw_suffixes[save_code][lm1_b % 8] = first_byte
} else {
this.lzw_prefixes[save_code] = prev_code as base.u16
this.lzw_suffixes[save_code][0] = first_byte as base.u8
}
save_code += 1
if width < 12 {
width += 1 & (save_code >> width)
}
prev_code = code
}
} else {
this.lzw_read_from_return_value = 4
break
}
// Flush the output if it could be too full to contain the entire
// decoding of the next code. The longest possible decoding is slightly
// less than 4096 and output's length is 8192, so a conservative
// threshold is ensuring that output_wi <= 4095.
if output_wi > 4095 {
this.lzw_read_from_return_value = 1
break
}
} endwhile
// Rewind args.src, if we're not in "$short read" and we've read too many
// bits.
if this.lzw_read_from_return_value <> 2 {
while n_bits >= 8 {
n_bits -= 8
if args.src.can_undo_byte() {
args.src.undo_byte!()
} else {
this.lzw_read_from_return_value = 5
break
}
} endwhile
}
this.lzw_save_code = save_code
this.lzw_prev_code = prev_code
this.lzw_width = width
this.lzw_bits = bits
this.lzw_n_bits = n_bits
this.lzw_output_wi = output_wi
}
+4
View File
@@ -375,6 +375,10 @@ pri func decoder.write_to?(dst: base.io_writer) {
} endwhile
}
// Deprecated. Use transform_io and an io_writer instead.
//
// We'd like to deprecate (and eventually remove) methods that return anything
// pointer-y (including slices).
pub func decoder.flush!() roslice base.u8 {
var ri : base.u32[..= 8191]
var wi : base.u32[..= 8191]