Don't suspend for read_u8 if data is available.

It's not obvious why the benchmarks get worse when the code looks
better, but the long term goal is for all of decode_huffman_fast to not
suspend, and this is one step along that path.

name                           old speed      new speed      delta

puffs_adler32_10k/clang        2.40GB/s ± 1%  2.40GB/s ± 1%     ~     (p=0.841 n=5+5)
puffs_adler32_100k/clang       2.40GB/s ± 1%  2.39GB/s ± 2%     ~     (p=0.421 n=5+5)
puffs_flate_decode_1k/clang     131MB/s ± 1%   128MB/s ± 0%   -2.51%  (p=0.008 n=5+5)
puffs_flate_decode_10k/clang    171MB/s ± 1%   167MB/s ± 2%   -2.86%  (p=0.008 n=5+5)
puffs_flate_decode_100k/clang   202MB/s ± 0%   193MB/s ± 2%   -4.64%  (p=0.016 n=4+5)
puffs_zlib_decode_10k/clang     159MB/s ± 1%   157MB/s ± 0%   -1.65%  (p=0.008 n=5+5)
puffs_zlib_decode_100k/clang    185MB/s ± 1%   180MB/s ± 1%   -3.11%  (p=0.008 n=5+5)

puffs_adler32_10k/gcc          3.26GB/s ± 0%  3.26GB/s ± 0%     ~     (p=0.421 n=5+5)
puffs_adler32_100k/gcc         3.27GB/s ± 0%  3.26GB/s ± 0%     ~     (p=0.151 n=5+5)
puffs_flate_decode_1k/gcc       132MB/s ± 0%   129MB/s ± 0%   -2.29%  (p=0.008 n=5+5)
puffs_flate_decode_10k/gcc      170MB/s ± 0%   164MB/s ± 0%   -3.13%  (p=0.008 n=5+5)
puffs_flate_decode_100k/gcc     207MB/s ± 0%   185MB/s ± 0%  -10.45%  (p=0.016 n=5+4)
puffs_zlib_decode_10k/gcc       162MB/s ± 0%   157MB/s ± 0%   -2.95%  (p=0.008 n=5+5)
puffs_zlib_decode_100k/gcc      194MB/s ± 0%   175MB/s ± 0%   -9.78%  (p=0.016 n=5+4)
This commit is contained in:
Nigel Tao committed 2017-11-05 08:03:21 +11:00
1 parent cf61ca11c3
commit 55e0bc513b
4 files changed
+107 -54

No files matched your search

+56 -4
View File
@@ -15,6 +15,7 @@
package cgen
import (
"errors"
"fmt"
"strings"
@@ -353,12 +354,58 @@ func (g *gen) writeSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
if !n.Suspendible() {
return nil
}
if err := g.writeCoroSuspPoint(b, false); err != nil {
err := g.mightActuallySuspend(n, depth)
if err != nil && err != errMightActuallySuspend {
return err
}
mightActuallySuspend := err != nil
if mightActuallySuspend {
if err := g.writeCoroSuspPoint(b, false); err != nil {
return err
}
}
return g.writeCallSuspendibles(b, n, depth)
}
// errMightActuallySuspend is the absence of ProvenNotToSuspend.
//
// TODO: find better, less clumsy names for this concept.
var errMightActuallySuspend = errors.New("internal: might actually suspend")
// TODO: this would be simpler with a call keyword and an explicit "foo = call
// bar?()" syntax.
func (g *gen) mightActuallySuspend(n *a.Expr, depth uint32) error {
if depth > a.MaxExprDepth {
return fmt.Errorf("expression recursion depth too large")
}
depth++
// The evaluation order for suspendible calls (which can have side effects)
// is important here: LHS, MHS, RHS, Args and finally the node itself.
if !n.CallSuspendible() {
for _, o := range n.Node().Raw().SubNodes() {
if o != nil && o.Kind() == a.KExpr {
if err := g.mightActuallySuspend(o.Expr(), depth); err != nil {
return err
}
}
}
for _, o := range n.Args() {
if o != nil && o.Kind() == a.KExpr {
if err := g.mightActuallySuspend(o.Expr(), depth); err != nil {
return err
}
}
}
return nil
}
if n.ProvenNotToSuspend() {
return nil
}
return errMightActuallySuspend
}
func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
if depth > a.MaxExprDepth {
return fmt.Errorf("expression recursion depth too large")
@@ -399,9 +446,12 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
temp := g.currFunk.tempW
g.currFunk.tempW++
b.printf("if (PUFFS_BASE__UNLIKELY(%srptr_src == %srend_src)) { goto short_read_src; }",
bPrefix, bPrefix)
g.currFunk.shortReads = append(g.currFunk.shortReads, "src")
if !n.ProvenNotToSuspend() {
b.printf("if (PUFFS_BASE__UNLIKELY(%srptr_src == %srend_src)) { goto short_read_src; }",
bPrefix, bPrefix)
g.currFunk.shortReads = append(g.currFunk.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(b, n.MType(), tPrefix, fmt.Sprint(temp)); err != nil {
@@ -627,6 +677,8 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia
return fmt.Errorf("internal error: bad writeReadUXX endianness %q", endianness)
}
// TODO: look at n.ProvenNotToSuspend().
if g.currFunk.tempW > maxTemp-1 {
return fmt.Errorf("too many temporary variables required")
}
+10 -18
View File
@@ -2039,19 +2039,11 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast(
while (((uint64_t)(b_rend_src - b_rptr_src)) >= 6) {
if (v_n_bits < 15) {
{
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
if (PUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src;
}
uint8_t t_0 = *b_rptr_src++;
v_bits |= (((uint32_t)(t_0)) << v_n_bits);
}
v_n_bits += 8;
{
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
if (PUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src;
}
uint8_t t_1 = *b_rptr_src++;
v_bits |= (((uint32_t)(t_1)) << v_n_bits);
}
@@ -2063,7 +2055,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast(
v_bits >>= v_table_entry_n_bits;
v_n_bits -= v_table_entry_n_bits;
if ((v_table_entry >> 31) != 0) {
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
if (b_wptr_dst == b_wend_dst) {
status = PUFFS_FLATE__SUSPENSION_SHORT_WRITE;
goto suspend;
@@ -2093,7 +2085,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast(
goto label_1_break;
}
{
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
if (PUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src;
}
@@ -2104,7 +2096,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast(
}
label_1_break:;
if ((v_table_entry >> 31) != 0) {
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
if (b_wptr_dst == b_wend_dst) {
status = PUFFS_FLATE__SUSPENSION_SHORT_WRITE;
goto suspend;
@@ -2139,7 +2131,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast(
v_table_entry_n_bits = ((v_table_entry >> 4) & 15);
while (v_n_bits < v_table_entry_n_bits) {
{
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
if (PUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src;
}
@@ -2162,7 +2154,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast(
goto label_2_break;
}
{
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
if (PUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src;
}
@@ -2191,7 +2183,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast(
goto label_3_break;
}
{
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(8);
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
if (PUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src;
}
@@ -2220,7 +2212,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast(
v_table_entry_n_bits = ((v_table_entry >> 4) & 15);
while (v_n_bits < v_table_entry_n_bits) {
{
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(9);
PUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
if (PUFFS_BASE__UNLIKELY(b_rptr_src == b_rend_src)) {
goto short_read_src;
}
@@ -2288,7 +2280,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast(
}
}
status = PUFFS_FLATE__SUSPENSION_SHORT_WRITE;
PUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(10);
PUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(8);
}
label_4_break:;
if (v_hlen > 0) {
@@ -2307,7 +2299,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast(
v_hlen -= v_n_copied;
v_hdist = ((v_hdist + (v_n_copied & 32767)) & 32767);
status = PUFFS_FLATE__SUSPENSION_SHORT_WRITE;
PUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(11);
PUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(9);
}
label_5_break:;
}
@@ -2324,7 +2316,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast(
}
v_length -= v_n_copied;
status = PUFFS_FLATE__SUSPENSION_SHORT_WRITE;
PUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(12);
PUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(10);
}
label_6_break:;
}
+28 -25
View File
@@ -87,15 +87,16 @@ var kindStrings = [...]string{
type Flags uint32
const (
FlagsImpure = Flags(0x00000001)
FlagsSuspendible = Flags(0x00000002)
FlagsCallImpure = Flags(0x00000004)
FlagsCallSuspendible = Flags(0x00000008)
FlagsPublic = Flags(0x00000010)
FlagsTypeChecked = Flags(0x00000020)
FlagsHasBreak = Flags(0x00000040)
FlagsHasContinue = Flags(0x00000080)
FlagsGlobalIdent = Flags(0x00000100)
FlagsImpure = Flags(0x00000001)
FlagsSuspendible = Flags(0x00000002)
FlagsCallImpure = Flags(0x00000004)
FlagsCallSuspendible = Flags(0x00000008)
FlagsPublic = Flags(0x00000010)
FlagsTypeChecked = Flags(0x00000020)
FlagsHasBreak = Flags(0x00000040)
FlagsHasContinue = Flags(0x00000080)
FlagsGlobalIdent = Flags(0x00000100)
FlagsProvenNotToSuspend = Flags(0x00000200)
)
type Node struct {
@@ -244,25 +245,27 @@ const MaxExprDepth = 255
// and ID1 is the message.
type Expr Node
func (n *Expr) Node() *Node { return (*Node)(n) }
func (n *Expr) Pure() bool { return n.flags&FlagsImpure == 0 }
func (n *Expr) Impure() bool { return n.flags&FlagsImpure != 0 }
func (n *Expr) Suspendible() bool { return n.flags&FlagsSuspendible != 0 }
func (n *Expr) CallImpure() bool { return n.flags&FlagsCallImpure != 0 }
func (n *Expr) CallSuspendible() bool { return n.flags&FlagsCallSuspendible != 0 }
func (n *Expr) GlobalIdent() bool { return n.flags&FlagsGlobalIdent != 0 }
func (n *Expr) ConstValue() *big.Int { return n.constValue }
func (n *Expr) MType() *TypeExpr { return n.mType }
func (n *Expr) ID0() t.ID { return n.id0 }
func (n *Expr) ID1() t.ID { return n.id1 }
func (n *Expr) LHS() *Node { return n.lhs }
func (n *Expr) MHS() *Node { return n.mhs }
func (n *Expr) RHS() *Node { return n.rhs }
func (n *Expr) Args() []*Node { return n.list0 }
func (n *Expr) Node() *Node { return (*Node)(n) }
func (n *Expr) Pure() bool { return n.flags&FlagsImpure == 0 }
func (n *Expr) Impure() bool { return n.flags&FlagsImpure != 0 }
func (n *Expr) Suspendible() bool { return n.flags&FlagsSuspendible != 0 }
func (n *Expr) CallImpure() bool { return n.flags&FlagsCallImpure != 0 }
func (n *Expr) CallSuspendible() bool { return n.flags&FlagsCallSuspendible != 0 }
func (n *Expr) GlobalIdent() bool { return n.flags&FlagsGlobalIdent != 0 }
func (n *Expr) ProvenNotToSuspend() bool { return n.flags&FlagsProvenNotToSuspend != 0 }
func (n *Expr) ConstValue() *big.Int { return n.constValue }
func (n *Expr) MType() *TypeExpr { return n.mType }
func (n *Expr) ID0() t.ID { return n.id0 }
func (n *Expr) ID1() t.ID { return n.id1 }
func (n *Expr) LHS() *Node { return n.lhs }
func (n *Expr) MHS() *Node { return n.mhs }
func (n *Expr) RHS() *Node { return n.rhs }
func (n *Expr) Args() []*Node { return n.list0 }
func (n *Expr) SetConstValue(x *big.Int) { n.constValue = x }
func (n *Expr) SetMType(x *TypeExpr) { n.mType = x }
func (n *Expr) SetGlobalIdent() { n.flags |= FlagsGlobalIdent }
func (n *Expr) SetMType(x *TypeExpr) { n.mType = x }
func (n *Expr) SetProvenNotToSuspend() { n.flags |= FlagsProvenNotToSuspend }
func NewExpr(flags Flags, operator t.ID, nameLiteralSelector t.ID, lhs *Node, mhs *Node, rhs *Node, args []*Node) *Expr {
if lhs != nil {
+13 -7
View File
@@ -1170,23 +1170,24 @@ func (q *checker) bcheckIOMethods(n *a.Expr, depth uint32) error {
return nil
}
// Figure out the "receiver" in "receiver.read_u8?()".
if n.ID0().Key() != t.KeyOpenParen {
return nil
}
n = n.LHS().Expr()
if n.ID0().Key() != t.KeyDot {
receiver := n.LHS().Expr()
if receiver.ID0().Key() != t.KeyDot {
return nil
}
advance := (*big.Int)(nil)
if key := n.ID1().Key(); key < t.Key(len(ioMethodAdvances)) {
if key := receiver.ID1().Key(); key < t.Key(len(ioMethodAdvances)) {
advance = ioMethodAdvances[key]
}
if advance == nil {
return nil
}
n = n.LHS().Expr()
receiver = receiver.LHS().Expr()
// TODO: check that n's type is actually a reader1 or writer1.
// TODO: check that receiver's type is actually a reader1 or writer1.
return q.facts.update(func(x *a.Expr) (*a.Expr, error) {
op := x.ID0().Key()
@@ -1199,7 +1200,7 @@ func (q *checker) bcheckIOMethods(n *a.Expr, depth uint32) error {
return x, nil
}
// Check that lhs is "n.available()".
// Check that lhs is "receiver.available()".
lhs := x.LHS().Expr()
if lhs.ID0().Key() != t.KeyOpenParen || len(lhs.Args()) != 0 {
return x, nil
@@ -1209,7 +1210,7 @@ func (q *checker) bcheckIOMethods(n *a.Expr, depth uint32) error {
return x, nil
}
lhs = lhs.LHS().Expr()
if !lhs.Eq(n) {
if !lhs.Eq(receiver) {
return x, nil
}
@@ -1235,6 +1236,11 @@ func (q *checker) bcheckIOMethods(n *a.Expr, depth uint32) error {
o.SetConstValue(rcv)
o.SetMType(typeExprIdeal)
if !n.CallSuspendible() {
return nil, fmt.Errorf("check: internal error: inconsistent suspendible-ness for %q", n.String(q.tm))
}
n.SetProvenNotToSuspend()
return a.NewExpr(x.Node().Raw().Flags(), t.IDXBinaryGreaterEq, 0, x.LHS(), nil, o.Node(), nil), nil
})
}