mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 08:47:28 +01:00
Write LZW output 8 bytes at a time, not 1
The benchmarks are mixed, but I think it's still a net win.
----
name old speed new speed delta
wuffs_gif_decode_1k_bw/clang 258MB/s ± 0% 253MB/s ± 1% -2.04% (p=0.008 n=5+5)
wuffs_gif_decode_1k_color/clang 159MB/s ± 0% 116MB/s ± 0% -26.76% (p=0.008 n=5+5)
wuffs_gif_decode_10k/clang 167MB/s ± 0% 138MB/s ± 0% -17.42% (p=0.008 n=5+5)
wuffs_gif_decode_100k_artificial/clang 205MB/s ± 0% 439MB/s ± 0% +114.53% (p=0.008 n=5+5)
wuffs_gif_decode_100k_realistic/clang 151MB/s ± 0% 164MB/s ± 0% +8.41% (p=0.008 n=5+5)
wuffs_gif_decode_1000k/clang 155MB/s ± 0% 165MB/s ± 0% +6.50% (p=0.008 n=5+5)
wuffs_gif_decode_anim_screencap/clang 255MB/s ± 0% 921MB/s ± 0% +261.50% (p=0.008 n=5+5)
wuffs_gif_decode_1k_bw/gcc 316MB/s ± 2% 267MB/s ± 1% -15.78% (p=0.008 n=5+5)
wuffs_gif_decode_1k_color/gcc 184MB/s ± 1% 134MB/s ± 0% -27.57% (p=0.008 n=5+5)
wuffs_gif_decode_10k/gcc 196MB/s ± 0% 161MB/s ± 0% -17.50% (p=0.008 n=5+5)
wuffs_gif_decode_100k_artificial/gcc 287MB/s ± 0% 449MB/s ± 1% +56.22% (p=0.008 n=5+5)
wuffs_gif_decode_100k_realistic/gcc 182MB/s ± 0% 182MB/s ± 1% ~ (p=0.690 n=5+5)
wuffs_gif_decode_1000k/gcc 187MB/s ± 1% 185MB/s ± 1% ~ (p=0.056 n=5+5)
wuffs_gif_decode_anim_screencap/gcc 397MB/s ± 0% 973MB/s ± 0% +145.42% (p=0.008 n=5+5)
wuffs_lzw_decode_20k/clang 170MB/s ± 0% 191MB/s ± 1% +12.52% (p=0.008 n=5+5)
wuffs_lzw_decode_100k/clang 242MB/s ± 0% 312MB/s ± 2% +29.00% (p=0.008 n=5+5)
wuffs_lzw_decode_20k/gcc 220MB/s ± 0% 227MB/s ± 0% +3.31% (p=0.008 n=5+5)
wuffs_lzw_decode_100k/gcc 317MB/s ± 0% 369MB/s ± 1% +16.53% (p=0.008 n=5+5)
----
LZW is the compression method used by GIF images. Roughly speaking, LZW
is a sequence of codes, and each code emits either 1 byte (a literal
code) or multiple bytes (a copy code). The speed gain in this commit
comes from, when emitting an N-byte copy code, this now requires N/8
iterations instead of N iterations, as each iteration now writes 8 bytes
instead of 1. Literal codes, both before and after this commit, requires
a single iteration.
Decoding should therefore get faster or slower depending on whether it's
copy-code-heavy or literal-code-heavy respectively. For what it's worth,
copy-code-heavy GIF images also usually compress better.
Running script/extract-giflzw.go (or examining the cached output at
test/data/analysis/giflzw-emission-lengths.txt) on the anim_screencap
(which got faster) and 1k_color (which got slower) cases (also known as
gifplayer-muybridge.gif and hippopotamus.regular.gif) shows that the
proportion of literal codes agrees with that explanation.
----
gifplayer-muybridge.gif (all frames)
Percent Count Emission_Length_Bucket
12.78 34658 1 (Literal Code)
14.05 38106 2
13.25 35937 3
10.59 28707 4
7.85 21286 5
6.08 16481 6
4.85 13156 7
3.60 9764 8
9.34 25318 9 .. 16
4.89 13257 17 .. 32
4.78 12954 33 .. 64
4.72 12806 65 .. 128
3.02 8190 127 .. 256
0.20 550 256+
hippopotamus.regular.gif (all frames)
Percent Count Emission_Length_Bucket
82.66 672 1 (Literal Code)
13.04 106 2
2.83 23 3
0.86 7 4
0.37 3 5
0.25 2 6
0.00 0 7
0.00 0 8
0.00 0 9 .. 16
0.00 0 17 .. 32
0.00 0 33 .. 64
0.00 0 65 .. 128
0.00 0 127 .. 256
0.00 0 256+
----
The new algorithm also adds more CPU and RAM overhead, regardless of
whether the input GIF image's LZW data is copy-code-heavy or
literal-code-heavy. For example, the suffix table grows by 8x, from 4KiB
to 32 KiB.
The gifplayer-muybridge.gif image is also larger (472 x 298) and the
hippopotamus.regular.gif image is also smaller (36 x 28). Higher fixed
costs may have larger (negative) relative effects on decoding smaller
images.
To repeat, I think it's still a net win.
An upcoming commit will update the std/lzw/README.md file to provide
more detail on the algorithm.
Thanks to Tushar Khandelwal for the idea.
This commit is contained in:
1 parent
8d7bd36179
commit
3338d6d1bc
7 files changed
+292
-67
No files matched your search
@@ -22,7 +22,10 @@ import (
|
||||
t "github.com/google/wuffs/lang/token"
|
||||
)
|
||||
|
||||
var errNoSuchBuiltin = errors.New("cgen: internal error: no such built-in")
|
||||
var (
|
||||
errNoSuchBuiltin = errors.New("cgen: internal error: no such built-in")
|
||||
errOptimizationNotApplicable = errors.New("cgen: internal error: optimization not applicable")
|
||||
)
|
||||
|
||||
func (g *gen) writeBuiltinCall(b *buffer, n *a.Expr, rp replacementPolicy, depth uint32) error {
|
||||
if n.Operator() != t.IDOpenParen {
|
||||
@@ -323,6 +326,10 @@ func (g *gen) writeBuiltinNumType(b *buffer, recv *a.Expr, method t.ID, args []*
|
||||
func (g *gen) writeBuiltinSlice(b *buffer, recv *a.Expr, method t.ID, args []*a.Node, rp replacementPolicy, depth uint32) error {
|
||||
switch method {
|
||||
case t.IDCopyFromSlice:
|
||||
if err := g.writeBuiltinSliceCopyFromSlice8(b, recv, method, args, rp, depth); err != errOptimizationNotApplicable {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: don't assume that the slice is a slice of base.u8.
|
||||
b.writes("wuffs_base__slice_u8__copy_from_slice(")
|
||||
if err := g.writeExpr(b, recv, rp, depth); err != nil {
|
||||
@@ -351,6 +358,57 @@ func (g *gen) writeBuiltinSlice(b *buffer, recv *a.Expr, method t.ID, args []*a.
|
||||
return errNoSuchBuiltin
|
||||
}
|
||||
|
||||
// writeBuiltinSliceCopyFromSlice8 writes an optimized version of:
|
||||
//
|
||||
// foo[fIndex:fIndex + 8].copy_from_slice!(s:bar[bIndex:bIndex + 8])
|
||||
func (g *gen) writeBuiltinSliceCopyFromSlice8(b *buffer, recv *a.Expr, method t.ID, args []*a.Node, rp replacementPolicy, depth uint32) error {
|
||||
if method != t.IDCopyFromSlice || len(args) != 1 {
|
||||
return errOptimizationNotApplicable
|
||||
}
|
||||
foo, fIndex := matchFooIndexIndexPlus8(recv)
|
||||
bar, bIndex := matchFooIndexIndexPlus8(args[0].AsArg().Value())
|
||||
if foo == nil || bar == nil {
|
||||
return errOptimizationNotApplicable
|
||||
}
|
||||
b.writes("memcpy((")
|
||||
if err := g.writeExpr(b, foo, rp, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes(")+(")
|
||||
if err := g.writeExpr(b, fIndex, rp, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes("),(")
|
||||
if err := g.writeExpr(b, bar, rp, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes(")+(")
|
||||
if err := g.writeExpr(b, bIndex, rp, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: don't assume that the slice is a slice of base.u8.
|
||||
b.writes("), 8)")
|
||||
return nil
|
||||
}
|
||||
|
||||
// matchFooIndexIndexPlus8 matches n with "foo[index:index + 8]". It returns
|
||||
// nil values if there isn't a match.
|
||||
func matchFooIndexIndexPlus8(n *a.Expr) (foo *a.Expr, index *a.Expr) {
|
||||
if n.Operator() != t.IDColon {
|
||||
return nil, nil
|
||||
}
|
||||
foo = n.LHS().AsExpr()
|
||||
index = n.MHS().AsExpr()
|
||||
rhs := n.RHS().AsExpr()
|
||||
if index == nil || rhs == nil || rhs.Operator() != t.IDXBinaryPlus || !rhs.LHS().AsExpr().Eq(index) {
|
||||
return nil, nil
|
||||
}
|
||||
if cv := rhs.RHS().AsExpr().ConstValue(); cv == nil || cv.Cmp(eight) != 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return foo, index
|
||||
}
|
||||
|
||||
func (g *gen) writeBuiltinTable(b *buffer, recv *a.Expr, method t.ID, args []*a.Node, rp replacementPolicy, depth uint32) error {
|
||||
field := ""
|
||||
|
||||
|
||||
@@ -40,8 +40,11 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
zero = big.NewInt(0)
|
||||
one = big.NewInt(1)
|
||||
zero = big.NewInt(0)
|
||||
one = big.NewInt(1)
|
||||
eight = big.NewInt(8)
|
||||
|
||||
mibi = big.NewInt(1 << 20)
|
||||
)
|
||||
|
||||
// Prefixes are prepended to names to form a namespace and to avoid e.g.
|
||||
|
||||
@@ -98,26 +98,43 @@ func (g *gen) writeStatementAssign(b *buffer, n *a.Assign, depth uint32) error {
|
||||
if err := g.writeSuspendibles(b, n.RHS(), depth); err != nil {
|
||||
return err
|
||||
}
|
||||
opName, tilde := "", false
|
||||
opName, closer := "", ""
|
||||
|
||||
op := n.Operator()
|
||||
switch op {
|
||||
case t.IDTildeSatPlusEq, t.IDTildeSatMinusEq:
|
||||
uBits := uintBits(n.LHS().MType().QID())
|
||||
if uBits == 0 {
|
||||
return fmt.Errorf("unsupported tilde-operator type %q", n.LHS().MType().Str(g.tm))
|
||||
}
|
||||
uOp := "add"
|
||||
if op != t.IDTildeSatPlusEq {
|
||||
uOp = "sub"
|
||||
}
|
||||
b.printf("wuffs_base__u%d__sat_%s_indirect(&", uBits, uOp)
|
||||
opName, tilde = ",", true
|
||||
if lTyp := n.LHS().MType(); lTyp.IsNumType() || lTyp.IsBool() || lTyp.IsSliceType() {
|
||||
op := n.Operator()
|
||||
switch op {
|
||||
case t.IDTildeSatPlusEq, t.IDTildeSatMinusEq:
|
||||
uBits := uintBits(n.LHS().MType().QID())
|
||||
if uBits == 0 {
|
||||
return fmt.Errorf("unsupported tilde-operator type %q", lTyp.Str(g.tm))
|
||||
}
|
||||
uOp := "add"
|
||||
if op != t.IDTildeSatPlusEq {
|
||||
uOp = "sub"
|
||||
}
|
||||
b.printf("wuffs_base__u%d__sat_%s_indirect(&", uBits, uOp)
|
||||
opName, closer = ",", ")"
|
||||
|
||||
default:
|
||||
opName = cOpName(op)
|
||||
if opName == "" {
|
||||
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
|
||||
default:
|
||||
opName = cOpName(op)
|
||||
if opName == "" {
|
||||
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if lTyp.IsArrayType() {
|
||||
if nElem := lTyp.ArrayLength().ConstValue(); nElem.Sign() > 0 && nElem.Cmp(mibi) <= 0 {
|
||||
if elemTyp := lTyp.Inner(); elemTyp.IsNumType() {
|
||||
if nBits := uintBits(elemTyp.QID()); nBits != 0 {
|
||||
b.writes("memcpy(")
|
||||
opName, closer = ",", fmt.Sprintf(", %d)", nElem.Int64()*int64(nBits/8))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if closer == "" {
|
||||
return fmt.Errorf("unsupported assignment type %q", lTyp.Str(g.tm))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,9 +145,7 @@ func (g *gen) writeStatementAssign(b *buffer, n *a.Assign, depth uint32) error {
|
||||
if err := g.writeExpr(b, n.RHS(), replaceCallSuspendibles, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
if tilde {
|
||||
b.writeb(')')
|
||||
}
|
||||
b.writes(closer)
|
||||
b.writes(";\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user