From 94dcc14b44d4c893f292c64fba50c75f8f4f23fc Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 30 Jun 2017 15:30:51 +1000 Subject: [PATCH] Have writer1 methods honor the limit. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The benchmark numbers are worse, but we need to honor the limit, which we weren't doing before. name old speed new speed delta bench_lzw_decode_20k/clang 128MB/s ± 0% 116MB/s ± 0% -9.87% (p=0.016 n=5+4) bench_lzw_decode_100k/clang 166MB/s ± 0% 154MB/s ± 0% -7.18% (p=0.008 n=5+5) bench_gif_decode_1k/clang 351MB/s ± 0% 285MB/s ± 1% -18.73% (p=0.008 n=5+5) bench_gif_decode_10k/clang 131MB/s ± 0% 122MB/s ± 0% -6.70% (p=0.008 n=5+5) bench_gif_decode_100k/clang 121MB/s ± 0% 110MB/s ± 0% -9.37% (p=0.008 n=5+5) bench_gif_decode_1000k/clang 124MB/s ± 0% 112MB/s ± 0% -9.13% (p=0.016 n=5+4) bench_lzw_decode_20k/gcc 143MB/s ± 0% 138MB/s ± 2% -2.94% (p=0.008 n=5+5) bench_lzw_decode_100k/gcc 195MB/s ± 0% 175MB/s ± 2% -10.23% (p=0.008 n=5+5) bench_gif_decode_1k/gcc 394MB/s ± 0% 389MB/s ± 3% -1.42% (p=0.008 n=5+5) bench_gif_decode_10k/gcc 137MB/s ± 0% 141MB/s ± 1% +2.87% (p=0.008 n=5+5) bench_gif_decode_100k/gcc 127MB/s ± 0% 128MB/s ± 2% ~ (p=0.730 n=4+5) bench_gif_decode_1000k/gcc 130MB/s ± 0% 130MB/s ± 2% ~ (p=0.730 n=4+5) --- cmd/puffs-gen-c/func.go | 45 ++++++++++++++++++++++++++++++++--------- gen/c/std/gif.c | 34 +++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/cmd/puffs-gen-c/func.go b/cmd/puffs-gen-c/func.go index 2d003887..f2a85c80 100644 --- a/cmd/puffs-gen-c/func.go +++ b/cmd/puffs-gen-c/func.go @@ -339,7 +339,23 @@ func (g *gen) writeLoadDerivedVar(name t.ID, typ *a.TypeExpr, decl bool) error { g.printf("}\n") case t.KeyWriter1: - // TODO. + if decl { + g.printf("uint8_t* %swptr_%s = NULL;", bPrefix, nameStr) + g.printf("uint8_t* %swend_%s = NULL;", bPrefix, nameStr) + } + g.printf("if (%s%s.buf) {", aPrefix, nameStr) + + g.printf("%swptr_%s = %s%s.buf->ptr + %s%s.buf->wi;", + bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr) + g.printf("size_t len = %s%s.buf->len - %s%s.buf->wi;", + aPrefix, nameStr, aPrefix, nameStr) + g.printf("puffs_base_limit1* lim;") + g.printf("for (lim = &%s%s.limit; lim; lim = lim->next) {", aPrefix, nameStr) + g.printf("if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { len = *lim->ptr_to_len; }") + g.printf("}") + g.printf("%swend_%s = %swptr_%s + len;", bPrefix, nameStr, bPrefix, nameStr) + + g.printf("}\n") } return nil } @@ -367,7 +383,17 @@ func (g *gen) writeSaveDerivedVar(name t.ID, typ *a.TypeExpr) error { g.printf("}\n") case t.KeyWriter1: - // TODO. + g.printf("if (%s%s.buf) {", aPrefix, nameStr) + + g.printf("size_t n = %swptr_%s - (%s%s.buf->ptr + %s%s.buf->wi);", + bPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr) + g.printf("%s%s.buf->wi += n;", aPrefix, nameStr) + g.printf("puffs_base_limit1* lim;") + g.printf("for (lim = &%s%s.limit; lim; lim = lim->next) {", aPrefix, nameStr) + g.printf("if (lim->ptr_to_len) { *lim->ptr_to_len -= n; }") + g.printf("}") + + g.printf("}\n") } return nil } @@ -811,8 +837,8 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error { g.writes("return status;") } g.writes("}\n") - g.printf("if ((%sdst.buf->len - %sdst.buf->wi) < (sizeof(self->private_impl.f_stack) - v_s)) {", - aPrefix, aPrefix) + g.printf("if ((%swend_dst - %swptr_dst) < (sizeof(self->private_impl.f_stack) - v_s)) {", + bPrefix, bPrefix) g.printf("status = puffs_%s_status_short_write;", g.pkgName) if g.perFunc.public && g.perFunc.suspendible { g.writes("goto suspend;") @@ -820,22 +846,21 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error { g.writes("return status;") } g.writes("}\n") - g.printf("memmove(" + - "a_dst.buf->ptr + a_dst.buf->wi," + + g.printf("memmove(b_wptr_dst," + "self->private_impl.f_stack + v_s," + "sizeof(self->private_impl.f_stack) - v_s);\n") - g.printf("a_dst.buf->wi += sizeof(self->private_impl.f_stack) - v_s;\n") + g.printf("b_wptr_dst += sizeof(self->private_impl.f_stack) - v_s;\n") } else if isInDst(g.tm, n, t.KeyWriteU8, 1) { - g.printf("if (%sdst.buf->wi >= %sdst.buf->len) { status = puffs_%s_status_short_write;", - aPrefix, aPrefix, g.pkgName) + g.printf("if (%swptr_dst == %swend_dst) { status = puffs_%s_status_short_write;", + bPrefix, bPrefix, g.pkgName) if g.perFunc.public && g.perFunc.suspendible { g.writes("goto suspend;") } else { g.writes("return status;") } g.writes("}\n") - g.printf("%sdst.buf->ptr[%sdst.buf->wi++] = ", aPrefix, aPrefix) + g.printf("*%swptr_dst++ = ", bPrefix) x := n.Args()[0].Arg().Value() if err := g.writeExpr(x, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { return err diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index f6d20911..d68590d7 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -1053,6 +1053,19 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, uint32_t v_s; uint32_t v_c; + uint8_t* b_wptr_dst = NULL; + uint8_t* b_wend_dst = NULL; + if (a_dst.buf) { + b_wptr_dst = a_dst.buf->ptr + a_dst.buf->wi; + size_t len = a_dst.buf->len - a_dst.buf->wi; + puffs_base_limit1* lim; + for (lim = &a_dst.limit; lim; lim = lim->next) { + if (lim->ptr_to_len && (len > *lim->ptr_to_len)) { + len = *lim->ptr_to_len; + } + } + b_wend_dst = b_wptr_dst + len; + } uint8_t* b_rptr_src = NULL; uint8_t* b_rend_src = NULL; if (a_src.buf) { @@ -1111,11 +1124,11 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, v_n_bits -= v_width; if (v_code < v_clear_code) { PUFFS_COROUTINE_STATE(2); - if (a_dst.buf->wi >= a_dst.buf->len) { + if (b_wptr_dst == b_wend_dst) { status = puffs_gif_status_short_write; goto suspend; } - a_dst.buf->ptr[a_dst.buf->wi++] = ((uint8_t)(v_code)); + *b_wptr_dst++ = ((uint8_t)(v_code)); if (v_use_save_code) { self->private_impl.f_suffixes[v_save_code] = ((uint8_t)(v_code)); self->private_impl.f_prefixes[v_save_code] = @@ -1155,15 +1168,14 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, status = puffs_gif_error_closed_for_writes; goto suspend; } - if ((a_dst.buf->len - a_dst.buf->wi) < + if ((b_wend_dst - b_wptr_dst) < (sizeof(self->private_impl.f_stack) - v_s)) { status = puffs_gif_status_short_write; goto suspend; } - memmove(a_dst.buf->ptr + a_dst.buf->wi, - self->private_impl.f_stack + v_s, + memmove(b_wptr_dst, self->private_impl.f_stack + v_s, sizeof(self->private_impl.f_stack) - v_s); - a_dst.buf->wi += sizeof(self->private_impl.f_stack) - v_s; + b_wptr_dst += sizeof(self->private_impl.f_stack) - v_s; if (v_use_save_code) { self->private_impl.f_suffixes[v_save_code] = ((uint8_t)(v_c)); self->private_impl.f_prefixes[v_save_code] = @@ -1200,6 +1212,16 @@ suspend: self->private_impl.c_decode[0].v_s = v_s; self->private_impl.c_decode[0].v_c = v_c; + if (a_dst.buf) { + size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi); + a_dst.buf->wi += n; + puffs_base_limit1* lim; + for (lim = &a_dst.limit; lim; lim = lim->next) { + if (lim->ptr_to_len) { + *lim->ptr_to_len -= n; + } + } + } if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); a_src.buf->ri += n;