From 13731f989fd580ff67bd7628ffa780a09cf8fa8f Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 5 Nov 2017 14:59:32 +1100 Subject: [PATCH] Add a reader1.unread_u8 method. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also re-enable a number of std/flate tests and benchmarks. It's not an optimization per se, but as a data point, here are some benchmark numbers between 36a93fa and this commit. 36a93fa is "36a93fa Add a TODO about impure functions", which is one commit before "f968950 Read bits up front in decode_huffman_fast", and f968950 was the commit that made decode_huffman_fast read more than one byte at a time. name old speed new speed delta puffs_adler32_10k/clang 2.45GB/s ± 0% 2.45GB/s ± 0% ~ (p=0.684 n=10+10) puffs_adler32_100k/clang 2.45GB/s ± 0% 2.46GB/s ± 0% +0.07% (p=0.019 n=10+10) puffs_flate_decode_1k/clang 124MB/s ± 0% 132MB/s ± 0% +6.06% (p=0.000 n=10+10) puffs_flate_decode_10k/clang 145MB/s ± 0% 174MB/s ± 0% +20.19% (p=0.000 n=9+10) puffs_flate_decode_100k/clang 168MB/s ± 0% 208MB/s ± 0% +23.31% (p=0.000 n=10+10) puffs_zlib_decode_10k/clang 136MB/s ± 0% 162MB/s ± 0% +18.94% (p=0.000 n=10+9) puffs_zlib_decode_100k/clang 158MB/s ± 0% 192MB/s ± 0% +21.58% (p=0.000 n=10+9) puffs_adler32_10k/gcc 3.26GB/s ± 0% 3.26GB/s ± 0% ~ (p=0.631 n=10+10) puffs_adler32_100k/gcc 3.27GB/s ± 0% 3.27GB/s ± 0% ~ (p=0.237 n=10+8) puffs_flate_decode_1k/gcc 95.3MB/s ± 0% 105.5MB/s ± 0% +10.67% (p=0.000 n=10+10) puffs_flate_decode_10k/gcc 110MB/s ± 0% 140MB/s ± 0% +27.32% (p=0.000 n=9+9) puffs_flate_decode_100k/gcc 121MB/s ± 0% 150MB/s ± 0% +24.02% (p=0.000 n=8+10) puffs_zlib_decode_10k/gcc 112MB/s ± 0% 120MB/s ± 0% +6.96% (p=0.000 n=10+9) puffs_zlib_decode_100k/gcc 123MB/s ± 0% 134MB/s ± 0% +8.93% (p=0.000 n=9+8) --- cmd/puffs-c/internal/cgen/statement.go | 9 +++++++++ gen/c/std/flate.c | 6 ++++++ lang/check/bounds.go | 10 +++++++--- lang/check/type.go | 2 +- lang/token/list.go | 5 +++++ std/flate/decode_huffman_fast.puffs | 2 +- test/c/std/flate.c | 20 +++++++++----------- 7 files changed, 38 insertions(+), 16 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index 07c47bb2..18188847 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -459,6 +459,15 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error { } b.printf(" = *%srptr_src++;\n", bPrefix) + } else if isInSrc(g.tm, n, t.KeyUnreadU8, 0) { + if !n.ProvenNotToSuspend() { + b.printf("if (%srptr_src == %srstart_src) { status = %sERROR_INVALID_I_O_OPERATION;", + bPrefix, bPrefix, g.PKGPREFIX) + b.writes("goto exit;") + b.writes("}\n") + } + b.printf("%srptr_src--;\n", bPrefix) + } else if isInSrc(g.tm, n, t.KeyReadU16BE, 0) { return g.writeReadUXX(b, n, "src", 16, "be") diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index b2b2a5bb..fec01c28 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -2349,6 +2349,12 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast( label_0_break:; while (v_n_bits >= 8) { v_n_bits -= 8; + PUFFS_BASE__COROUTINE_SUSPENSION_POINT(11); + if (b_rptr_src == b_rstart_src) { + status = PUFFS_FLATE__ERROR_INVALID_I_O_OPERATION; + goto exit; + } + b_rptr_src--; } self->private_impl.f_bits = v_bits; self->private_impl.f_n_bits = v_n_bits; diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 0c48ec00..4ecdff39 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -734,7 +734,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, } // TODO: delete this hack that only matches "in.src.read_u8?()" etc. - if isInSrc(q.tm, n, t.KeyReadU8, 0) || + if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeyUnreadU8, 0) || isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) || isInSrc(q.tm, n, t.KeySkip32, 1) || isInSrc(q.tm, n, t.KeySinceMark, 0) || @@ -1170,7 +1170,8 @@ func (q *checker) bcheckIOMethods(n *a.Expr, depth uint32) error { return nil } - // Figure out the "receiver" in "receiver.read_u8?()". + // Figure out the "receiver" in "receiver.read_xxx?()", for some I/O method + // "read_xxx". if n.ID0().Key() != t.KeyOpenParen { return nil } @@ -1179,7 +1180,10 @@ func (q *checker) bcheckIOMethods(n *a.Expr, depth uint32) error { return nil } advance := (*big.Int)(nil) - if key := receiver.ID1().Key(); key < t.Key(len(ioMethodAdvances)) { + if key := receiver.ID1().Key(); key == t.KeyUnreadU8 { + // TODO: call SetProvenNotToSuspend, since unread_u8 can never suspend, + // only succeed or fail. + } else if key < t.Key(len(ioMethodAdvances)) { advance = ioMethodAdvances[key] } if advance == nil { diff --git a/lang/check/type.go b/lang/check/type.go index d15e644e..8293fef4 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -407,7 +407,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { // require a TypeExpr being able to express function and method types. // TODO: delete this hack that only matches "in.src.read_u8?()" etc. - if isInSrc(q.tm, n, t.KeyReadU8, 0) || + if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeyUnreadU8, 0) || isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) || isInSrc(q.tm, n, t.KeySkip32, 1) || isInSrc(q.tm, n, t.KeySinceMark, 0) || diff --git a/lang/token/list.go b/lang/token/list.go index 3b6eec73..8447cff3 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -150,6 +150,8 @@ func (t Token) IsXAssociativeOp() bool { return t.ID.Key().isXOp() && t.ID.IsAss // // "Squiggly" means a sequence of non-alpha-numeric characters, such as "+" and // "&=". Their Keys range in [0x01, 0x7F]. +// +// TODO: re-pack the keys. const nBuiltInKeys = Key(256) const ( @@ -301,6 +303,7 @@ const ( KeyPrefix = Key(IDPrefix >> KeyShift) KeySuffix = Key(IDSuffix >> KeyShift) KeyLimit = Key(IDLimit >> KeyShift) + KeyUnreadU8 = Key(IDUnreadU8 >> KeyShift) KeyXUnaryPlus = Key(IDXUnaryPlus >> KeyShift) KeyXUnaryMinus = Key(IDXUnaryMinus >> KeyShift) @@ -488,6 +491,7 @@ const ( IDPrefix = ID(0xCB<= 8 { n_bits -= 8 - // TODO: in.src.unread_u8?() + in.src.unread_u8?() } this.bits = bits diff --git a/test/c/std/flate.c b/test/c/std/flate.c index daebc644..4cc547d9 100644 --- a/test/c/std/flate.c +++ b/test/c/std/flate.c @@ -877,18 +877,17 @@ proc tests[] = { test_puffs_flate_decode_flate_backref_crosses_blocks, // test_puffs_flate_decode_midsummer, // test_puffs_flate_decode_pi, // - // TODO: uncomment. - //test_puffs_flate_decode_pi_many_big_reads, // - //test_puffs_flate_decode_pi_many_medium_reads, // - //test_puffs_flate_decode_pi_many_small_writes_reads, // - //test_puffs_flate_decode_romeo, // - //test_puffs_flate_decode_romeo_fixed, // + test_puffs_flate_decode_pi_many_big_reads, // + test_puffs_flate_decode_pi_many_medium_reads, // + test_puffs_flate_decode_pi_many_small_writes_reads, // + test_puffs_flate_decode_romeo, // + test_puffs_flate_decode_romeo_fixed, // test_puffs_flate_decode_split_src, // test_puffs_flate_history_full, // test_puffs_flate_history_partial, // test_puffs_flate_table_redirect, // - //test_puffs_zlib_checksum_mismatch, // - //test_puffs_zlib_decode_midsummer, // + test_puffs_zlib_checksum_mismatch, // + test_puffs_zlib_decode_midsummer, // test_puffs_zlib_decode_pi, // #ifdef PUFFS_MIMIC @@ -915,11 +914,10 @@ proc benches[] = { // Flate Benches bench_puffs_adler32_10k, // bench_puffs_adler32_100k, // - // TODO: uncomment. - //bench_puffs_flate_decode_1k, // + bench_puffs_flate_decode_1k, // bench_puffs_flate_decode_10k, // bench_puffs_flate_decode_100k, // - //bench_puffs_zlib_decode_10k, // + bench_puffs_zlib_decode_10k, // bench_puffs_zlib_decode_100k, // #ifdef PUFFS_MIMIC