Pull out duplicate short_read_src code.

name                          old speed      new speed       delta
lzw_puffs_decode_20k/clang     127MB/s ± 0%    127MB/s ± 0%  +0.40%  (p=0.008 n=5+5)
lzw_puffs_decode_100k/clang    176MB/s ± 0%    176MB/s ± 0%  +0.23%  (p=0.008 n=5+5)
gif_puffs_decode_1k/clang      380MB/s ± 1%    389MB/s ± 2%  +2.26%  (p=0.008 n=5+5)
gif_puffs_decode_10k/clang     136MB/s ± 0%    137MB/s ± 0%  +0.59%  (p=0.016 n=4+5)
gif_puffs_decode_100k/clang    121MB/s ± 0%    121MB/s ± 0%  +0.33%  (p=0.008 n=5+5)
gif_puffs_decode_1000k/clang   124MB/s ± 0%    124MB/s ± 0%  +0.25%  (p=0.008 n=5+5)
lzw_puffs_decode_20k/gcc       150MB/s ± 1%    151MB/s ± 0%  +0.97%  (p=0.008 n=5+5)
lzw_puffs_decode_100k/gcc      207MB/s ± 0%    197MB/s ± 0%  -4.84%  (p=0.008 n=5+5)
gif_puffs_decode_1k/gcc        413MB/s ± 1%    406MB/s ± 1%  -1.71%  (p=0.008 n=5+5)
gif_puffs_decode_10k/gcc       147MB/s ± 0%    158MB/s ± 0%  +7.15%  (p=0.008 n=5+5)
gif_puffs_decode_100k/gcc      136MB/s ± 0%    138MB/s ± 0%  +1.68%  (p=0.008 n=5+5)
gif_puffs_decode_1000k/gcc     139MB/s ± 0%    142MB/s ± 0%  +2.07%  (p=0.008 n=5+5)
This commit is contained in:
Nigel Tao committed 2017-07-02 16:11:51 +10:00
1 parent d974988eab
commit 187bde7540
4 files changed
+105 -72

No files matched your search

+8
View File
@@ -17,4 +17,12 @@
#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))
#if defined(__clang__) || defined(__GNUC__)
#define PUFFS_LIKELY(expr) (__builtin_expect((expr), 1))
#define PUFFS_UNLIKELY(expr) (__builtin_expect((expr), 0))
#else
#define PUFFS_LIKELY(expr) (expr)
#define PUFFS_UNLIKELY(expr) (expr)
#endif
#endif // PUFFS_BASE_IMPL_H
+1 -1
View File
@@ -12,5 +12,5 @@ const baseHeader = "" +
""
const baseImpl = "" +
"#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n// Use switch cases for coroutine state, similar to the technique in\n// https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_STATE(n) \\\n coro_state = n; \\\n case n:\n\n#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))\n\n#endif // PUFFS_BASE_IMPL_H\n" +
"#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n// Use switch cases for coroutine state, similar to the technique in\n// https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_STATE(n) \\\n coro_state = n; \\\n case n:\n\n#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))\n\n#if defined(__clang__) || defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect((expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect((expr), 0))\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n#endif // PUFFS_BASE_IMPL_H\n" +
""
+28 -11
View File
@@ -22,6 +22,7 @@ type perFunc struct {
public bool
suspendible bool
limitVarName string
shortReads []string
}
func (g *gen) writeFuncSignature(n *a.Func) error {
@@ -181,6 +182,17 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
g.writes("self->private_impl.status = status;\n")
}
g.writes("return status;\n\n")
shortReadsSeen := map[string]struct{}{}
for _, sr := range g.perFunc.shortReads {
if _, ok := shortReadsSeen[sr]; ok {
continue
}
shortReadsSeen[sr] = struct{}{}
if err := g.writeShortRead(sr); err != nil {
return err
}
}
}
g.writes("}\n\n")
@@ -780,17 +792,8 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
temp := g.perFunc.tempW
g.perFunc.tempW++
g.printf("if (%srptr_src == %srend_src) {", bPrefix, bPrefix)
// TODO: ri == wi isn't the right condition.
g.printf("status = ((%ssrc.buf->closed) && (%ssrc.buf->ri == %ssrc.buf->wi)) ?"+
"puffs_%s_error_unexpected_eof : puffs_%s_status_short_read;",
aPrefix, aPrefix, aPrefix, g.pkgName, g.pkgName)
if g.perFunc.public && g.perFunc.suspendible {
g.writes("goto suspend;")
} else {
g.writes("return status;")
}
g.writes("}\n")
g.printf("if (PUFFS_UNLIKELY(%srptr_src == %srend_src)) { goto short_read_src; }", bPrefix, bPrefix)
g.perFunc.shortReads = append(g.perFunc.shortReads, "src")
// TODO: watch for passing an array type to writeCTypeName? In C, an
// array type can decay into a pointer.
if err := g.writeCTypeName(n.MType(), tPrefix, fmt.Sprint(temp)); err != nil {
@@ -919,6 +922,20 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
return nil
}
func (g *gen) writeShortRead(name string) error {
g.printf("\nshort_read_%s:\n", name)
// TODO: ri == wi isn't the right condition.
g.printf("status = ((%s%s.buf->closed) && (%s%s.buf->ri == %s%s.buf->wi)) ?"+
"puffs_%s_error_unexpected_eof : puffs_%s_status_short_read;",
aPrefix, name, aPrefix, name, aPrefix, name, g.pkgName, g.pkgName)
if g.perFunc.public && g.perFunc.suspendible { // TODO: drop the g.perFunc.public?
g.writes("goto suspend;")
} else {
g.writes("return status;")
}
return nil
}
func (g *gen) writeExpr(n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
if depth > a.MaxExprDepth {
return fmt.Errorf("expression recursion depth too large")