From 9b4cc179c459915ce4c65caf70e3841c6652e5ed Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 9 Nov 2017 14:28:33 +1100 Subject: [PATCH] Use puffs_base__writer1__copy_from_history32__bco. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old speed new speed delta puffs_adler32_10k/clang 2.45GB/s ± 0% 2.45GB/s ± 0% ~ (p=0.063 n=5+5) puffs_adler32_100k/clang 2.45GB/s ± 0% 2.45GB/s ± 0% ~ (p=0.905 n=5+4) puffs_flate_decode_1k/clang 144MB/s ± 1% 146MB/s ± 0% +1.41% (p=0.016 n=5+5) puffs_flate_decode_10k/clang 186MB/s ± 0% 204MB/s ± 0% +9.39% (p=0.008 n=5+5) puffs_flate_decode_100k/clang 228MB/s ± 0% 240MB/s ± 0% +5.24% (p=0.008 n=5+5) puffs_zlib_decode_10k/clang 173MB/s ± 0% 188MB/s ± 0% +8.69% (p=0.008 n=5+5) puffs_zlib_decode_100k/clang 209MB/s ± 0% 219MB/s ± 0% +4.81% (p=0.008 n=5+5) puffs_adler32_10k/gcc 3.26GB/s ± 0% 3.26GB/s ± 0% ~ (p=0.286 n=5+4) puffs_adler32_100k/gcc 3.26GB/s ± 0% 3.26GB/s ± 0% ~ (p=0.190 n=5+4) puffs_flate_decode_1k/gcc 148MB/s ± 0% 148MB/s ± 0% +0.27% (p=0.016 n=5+4) puffs_flate_decode_10k/gcc 245MB/s ± 0% 254MB/s ± 0% +3.73% (p=0.008 n=5+5) puffs_flate_decode_100k/gcc 284MB/s ± 0% 298MB/s ± 0% +4.90% (p=0.008 n=5+5) puffs_zlib_decode_10k/gcc 228MB/s ± 0% 236MB/s ± 0% +3.33% (p=0.008 n=5+5) puffs_zlib_decode_100k/gcc 261MB/s ± 0% 273MB/s ± 0% +4.46% (p=0.016 n=5+4) --- cmd/puffs-c/internal/cgen/expr.go | 8 +- gen/c/std/flate.c | 2 +- lang/check/bounds.go | 145 +++++++++++++++++++++++++++--- 3 files changed, 142 insertions(+), 13 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index ab153230..9528c816 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -235,8 +235,12 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare return nil } if isInDst(g.tm, n, t.KeyCopyFromHistory32, 2) { - b.printf("puffs_base__writer1__copy_from_history32(&%swptr_dst, %sdst.private_impl.mark , %swend_dst", - bPrefix, aPrefix, bPrefix) + bco := "" + if n.BoundsCheckOptimized() { + bco = "__bco" + } + b.printf("puffs_base__writer1__copy_from_history32%s(&%swptr_dst, %sdst.private_impl.mark , %swend_dst", + bco, bPrefix, aPrefix, bPrefix) for _, o := range n.Args() { b.writeb(',') if err := g.writeExpr(b, o.Arg().Value(), rp, parenthesesOptional, depth); err != nil { diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index b093d701..e4556d48 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -2327,7 +2327,7 @@ label_0_continue:; goto exit; } } - puffs_base__writer1__copy_from_history32( + puffs_base__writer1__copy_from_history32__bco( &b_wptr_dst, a_dst.private_impl.mark, b_wend_dst, v_distance, v_length); goto label_2_break; diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 1dd16963..674c204a 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -1165,17 +1165,18 @@ func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) { return b[0], b[1], nil } -// splitReceiverMethod returns the "receiver" and "method" in the expression -// "receiver.method(etc)". -func splitReceiverMethod(n *a.Expr) (receiver *a.Expr, method t.Key) { +// splitReceiverMethodArgs returns the "receiver", "method" and "args" in the +// expression "receiver.method(args)". +func splitReceiverMethodArgs(n *a.Expr) (receiver *a.Expr, method t.Key, args []*a.Node) { if n.ID0().Key() != t.KeyOpenParen { - return nil, 0 + return nil, 0, nil } + args = n.Args() n = n.LHS().Expr() if n.ID0().Key() != t.KeyDot { - return nil, 0 + return nil, 0, nil } - return n.LHS().Expr(), n.ID1().Key() + return n.LHS().Expr(), n.ID1().Key(), args } // TODO: should bcheckOptimizeNonSuspendible be bcheckOptimizeExpr and check @@ -1188,16 +1189,19 @@ func (q *checker) bcheckOptimizeNonSuspendible(n *a.Expr) error { // // Should we have an explicit "foo = call bar?()" syntax? - nReceiver, nMethod := splitReceiverMethod(n) + nReceiver, nMethod, _ := splitReceiverMethodArgs(n) if nReceiver == nil { return nil } // TODO: check that nReceiver's type is actually a reader1 or writer1. - if nMethod == t.KeySinceMark { + switch nMethod { + case t.KeyCopyFromHistory32: + return q.bcheckOptimizeCopyFromHistory32(n) + case t.KeySinceMark: for _, x := range q.facts { - xReceiver, xMethod := splitReceiverMethod(x) + xReceiver, xMethod, _ := splitReceiverMethodArgs(x) if xMethod == t.KeyIsMarked && xReceiver.Eq(nReceiver) { n.SetBoundsCheckOptimized() return nil @@ -1208,6 +1212,127 @@ func (q *checker) bcheckOptimizeNonSuspendible(n *a.Expr) error { return nil } +// bcheckOptimizeCopyFromHistory32 checks if the code generator can call the +// Bounds Check Optimized version of CopyFromHistory32. As per +// cgen/base-impl.h, the conditions are: +// - start != NULL +// - distance != 0 +// - distance <= (*ptr_ptr - start) +// - length <= (end - *ptr_ptr) +func (q *checker) bcheckOptimizeCopyFromHistory32(n *a.Expr) error { + // The arguments are (distance, length). + nReceiver, _, args := splitReceiverMethodArgs(n) + if len(args) != 2 { + return nil + } + d := args[0].Arg().Value() + l := args[1].Arg().Value() + + // Check "start != NULL". +check0: + for { + for _, x := range q.facts { + xReceiver, xMethod, _ := splitReceiverMethodArgs(x) + if xMethod == t.KeyIsMarked && xReceiver.Eq(nReceiver) { + break check0 + } + } + return nil + } + + // Check "distance != 0". + // + // TODO: check that typeof(distance) is an unsigned integer type, so that + // an equivalent test is "distance > 0". +check1: + for { + for _, x := range q.facts { + if x.ID0().Key() != t.KeyXBinaryGreaterThan { + continue + } + if lhs := x.LHS().Expr(); !lhs.Eq(d) { + continue + } + if rcv := x.RHS().Expr().ConstValue(); rcv == nil || rcv.Cmp(zero) != 0 { + continue + } + break check1 + } + return nil + } + + // Check "distance <= (*ptr_ptr - start)". +check2: + for { + for _, x := range q.facts { + if x.ID0().Key() != t.KeyXBinaryLessEq { + continue + } + + // Check that the LHS is "d as u64". + lhs := x.LHS().Expr() + if lhs.ID0().Key() != t.KeyXBinaryAs { + continue + } + llhs, lrhs := lhs.LHS().Expr(), lhs.RHS().TypeExpr() + if !llhs.Eq(d) || !lrhs.Eq(typeExprU64) { + continue + } + + // Check that the RHS is "nReceiver.since_mark().length()". + y, method, yArgs := splitReceiverMethodArgs(x.RHS().Expr()) + if method != t.KeyLength || len(yArgs) != 0 { + continue + } + z, method, zArgs := splitReceiverMethodArgs(y) + if method != t.KeySinceMark || len(zArgs) != 0 { + continue + } + if !z.Eq(nReceiver) { + continue + } + + break check2 + } + return nil + } + + // Check "length <= (end - *ptr_ptr)". +check3: + for { + for _, x := range q.facts { + if x.ID0().Key() != t.KeyXBinaryLessEq { + continue + } + + // Check that the LHS is "l as u64". + lhs := x.LHS().Expr() + if lhs.ID0().Key() != t.KeyXBinaryAs { + continue + } + llhs, lrhs := lhs.LHS().Expr(), lhs.RHS().TypeExpr() + if !llhs.Eq(l) || !lrhs.Eq(typeExprU64) { + continue + } + + // Check that the RHS is "nReceiver.available()". + y, method, yArgs := splitReceiverMethodArgs(x.RHS().Expr()) + if method != t.KeyAvailable || len(yArgs) != 0 { + continue + } + if !y.Eq(nReceiver) { + continue + } + + break check3 + } + return nil + } + + n.SetBoundsCheckOptimized() + return nil +} + func (q *checker) bcheckOptimizeSuspendible(n *a.Expr, depth uint32) error { if depth > a.MaxExprDepth { return fmt.Errorf("check: expression recursion depth too large") @@ -1232,7 +1357,7 @@ func (q *checker) bcheckOptimizeSuspendible(n *a.Expr, depth uint32) error { return nil } - nReceiver, nMethod := splitReceiverMethod(n) + nReceiver, nMethod, _ := splitReceiverMethodArgs(n) if nReceiver == nil { return nil }