Add check for interleaved coroutines

It's not an optimization per se, but it seems to help:

name                                             old speed      new speed      delta

wuffs_deflate_decode_1k/clang5                    138MB/s ± 1%   150MB/s ± 1%   +8.96%  (p=0.008 n=5+5)
wuffs_deflate_decode_10k/clang5                   223MB/s ± 0%   243MB/s ± 0%   +9.25%  (p=0.008 n=5+5)
wuffs_deflate_decode_100k_just_one_read/clang5    263MB/s ± 2%   289MB/s ± 0%  +10.19%  (p=0.008 n=5+5)
wuffs_deflate_decode_100k_many_big_reads/clang5   220MB/s ± 1%   243MB/s ± 0%  +10.42%  (p=0.008 n=5+5)

wuffs_deflate_decode_1k/gcc7                      158MB/s ± 0%   157MB/s ± 0%     ~     (p=0.056 n=5+5)
wuffs_deflate_decode_10k/gcc7                     268MB/s ± 0%   268MB/s ± 0%     ~     (p=1.000 n=5+5)
wuffs_deflate_decode_100k_just_one_read/gcc7      312MB/s ± 0%   320MB/s ± 0%   +2.62%  (p=0.008 n=5+5)
wuffs_deflate_decode_100k_many_big_reads/gcc7     254MB/s ± 0%   253MB/s ± 0%   -0.48%  (p=0.016 n=5+5)
This commit is contained in:
Nigel Tao committed 2019-01-26 15:33:43 +11:00
1 parent d076b6e800
commit ecccafab09
5 files changed
+131

No files matched your search

+3
View File
@@ -325,6 +325,8 @@ type gen struct {
currFunk funk
funks map[t.QQID]funk
numPublicCoroutines uint32
}
func (g *gen) generate() ([]byte, error) {
@@ -712,6 +714,7 @@ func (g *gen) writeStructPrivateImpl(b *buffer, n *a.Struct) error {
b.writes("struct {\n")
if n.Classy() {
b.writes("uint32_t magic;\n")
b.writes("uint32_t active_coroutine;\n")
b.writes("\n")
}
+20
View File
@@ -31,6 +31,7 @@ type funk struct {
astFunc *a.Func
cName string
coroID uint32
returnsStatus bool
varList []*a.Var
@@ -174,9 +175,14 @@ func (g *gen) writeFuncImpl(b *buffer, n *a.Func) error {
}
func (g *gen) gatherFuncImpl(_ *buffer, n *a.Func) error {
if n.Public() && n.Effect().Coroutine() {
g.numPublicCoroutines++
}
g.currFunk = funk{
astFunc: n,
cName: g.funcCName(n),
coroID: g.numPublicCoroutines,
returnsStatus: n.Effect().Coroutine() ||
((n.Out() != nil) && n.Out().IsStatus()),
@@ -256,6 +262,17 @@ func (g *gen) writeFuncImplPrologue(b *buffer) error {
if err := g.writeFuncImplArgChecks(b, g.currFunk.astFunc); err != nil {
return err
}
// For public coroutines, check that we are not suspended in an active
// coroutine.
if g.currFunk.astFunc.Effect().Coroutine() {
b.printf("if ((self->private_impl.active_coroutine != 0) && "+
"(self->private_impl.active_coroutine != %d)) {\n", g.currFunk.coroID)
b.writes("self->private_impl.magic = WUFFS_BASE__DISABLED;\n")
b.writes("return wuffs_base__error__interleaved_coroutine_calls;\n")
b.writes("}\n")
b.writes("self->private_impl.active_coroutine = 0;\n")
}
}
if g.currFunk.astFunc.Effect().Coroutine() ||
@@ -325,6 +342,9 @@ func (g *gen) writeFuncImplBodySuspend(b *buffer) error {
b.printf("self->private_impl.%s%s[0].coro_susp_point = coro_susp_point;\n",
cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
if g.currFunk.astFunc.Public() {
b.printf("self->private_impl.active_coroutine = %d;\n", g.currFunk.coroID)
}
if err := g.writeResumeSuspend(b, &g.currFunk, true); err != nil {
return err
}
+1
View File
@@ -40,6 +40,7 @@ var Statuses = [...]string{
`"?check_wuffs_version not applicable"`,
`"?check_wuffs_version missing"`,
`"?disabled by previous error"`,
`"?interleaved coroutine calls"`,
}
// TODO: a collection of forbidden variable names like and, or, not, as, ref,
+59
View File
@@ -148,6 +148,7 @@ extern const char* wuffs_base__error__cannot_return_a_suspension;
extern const char* wuffs_base__error__check_wuffs_version_not_applicable;
extern const char* wuffs_base__error__check_wuffs_version_missing;
extern const char* wuffs_base__error__disabled_by_previous_error;
extern const char* wuffs_base__error__interleaved_coroutine_calls;
static inline bool //
wuffs_base__status__is_complete(wuffs_base__status z) {
@@ -2290,6 +2291,7 @@ struct wuffs_adler32__hasher__struct {
// struct can be stack allocated when WUFFS_IMPLEMENTATION is defined.
struct {
uint32_t magic;
uint32_t active_coroutine;
uint32_t f_state;
bool f_started;
@@ -2401,6 +2403,7 @@ struct wuffs_crc32__ieee_hasher__struct {
// struct can be stack allocated when WUFFS_IMPLEMENTATION is defined.
struct {
uint32_t magic;
uint32_t active_coroutine;
uint32_t f_state;
@@ -2527,6 +2530,7 @@ struct wuffs_deflate__decoder__struct {
// struct can be stack allocated when WUFFS_IMPLEMENTATION is defined.
struct {
uint32_t magic;
uint32_t active_coroutine;
uint32_t f_bits;
uint32_t f_n_bits;
@@ -2695,6 +2699,7 @@ struct wuffs_lzw__decoder__struct {
// struct can be stack allocated when WUFFS_IMPLEMENTATION is defined.
struct {
uint32_t magic;
uint32_t active_coroutine;
uint32_t f_lw;
uint32_t f_literal_width;
@@ -2877,6 +2882,7 @@ struct wuffs_gif__decoder__struct {
// struct can be stack allocated when WUFFS_IMPLEMENTATION is defined.
struct {
uint32_t magic;
uint32_t active_coroutine;
uint32_t f_width;
uint32_t f_height;
@@ -3137,6 +3143,7 @@ struct wuffs_gzip__decoder__struct {
// struct can be stack allocated when WUFFS_IMPLEMENTATION is defined.
struct {
uint32_t magic;
uint32_t active_coroutine;
wuffs_deflate__decoder f_flate;
wuffs_crc32__ieee_hasher f_checksum;
@@ -3271,6 +3278,7 @@ struct wuffs_zlib__decoder__struct {
// struct can be stack allocated when WUFFS_IMPLEMENTATION is defined.
struct {
uint32_t magic;
uint32_t active_coroutine;
wuffs_deflate__decoder f_flate;
wuffs_adler32__hasher f_checksum;
@@ -4201,6 +4209,8 @@ const char* wuffs_base__error__check_wuffs_version_missing =
"?base: check_wuffs_version missing";
const char* wuffs_base__error__disabled_by_previous_error =
"?base: disabled by previous error";
const char* wuffs_base__error__interleaved_coroutine_calls =
"?base: interleaved coroutine calls";
// ---------------- Images
@@ -5509,6 +5519,12 @@ wuffs_deflate__decoder__decode_io_writer(wuffs_deflate__decoder* self,
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__check_wuffs_version_missing;
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 1)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__error__interleaved_coroutine_calls;
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = NULL;
wuffs_base__status v_status = NULL;
@@ -5623,6 +5639,7 @@ wuffs_deflate__decoder__decode_io_writer(wuffs_deflate__decoder* self,
goto suspend;
suspend:
self->private_impl.c_decode_io_writer[0].coro_susp_point = coro_susp_point;
self->private_impl.active_coroutine = 1;
goto exit;
exit:
@@ -7300,6 +7317,12 @@ wuffs_lzw__decoder__decode_io_writer(wuffs_lzw__decoder* self,
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__check_wuffs_version_missing;
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 1)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__error__interleaved_coroutine_calls;
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = NULL;
uint32_t v_i = 0;
@@ -7367,6 +7390,7 @@ wuffs_lzw__decoder__decode_io_writer(wuffs_lzw__decoder* self,
goto suspend;
suspend:
self->private_impl.c_decode_io_writer[0].coro_susp_point = coro_susp_point;
self->private_impl.active_coroutine = 1;
goto exit;
exit:
@@ -7828,6 +7852,12 @@ wuffs_gif__decoder__decode_image_config(wuffs_gif__decoder* self,
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__check_wuffs_version_missing;
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 1)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__error__interleaved_coroutine_calls;
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = NULL;
bool v_ffio = false;
@@ -7884,6 +7914,7 @@ wuffs_gif__decoder__decode_image_config(wuffs_gif__decoder* self,
goto suspend;
suspend:
self->private_impl.c_decode_image_config[0].coro_susp_point = coro_susp_point;
self->private_impl.active_coroutine = 1;
goto exit;
exit:
@@ -8013,6 +8044,12 @@ wuffs_gif__decoder__decode_frame_config(wuffs_gif__decoder* self,
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__check_wuffs_version_missing;
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 2)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__error__interleaved_coroutine_calls;
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = NULL;
uint8_t v_blend = 0;
@@ -8085,6 +8122,7 @@ wuffs_gif__decoder__decode_frame_config(wuffs_gif__decoder* self,
goto suspend;
suspend:
self->private_impl.c_decode_frame_config[0].coro_susp_point = coro_susp_point;
self->private_impl.active_coroutine = 2;
goto exit;
exit:
@@ -8211,6 +8249,12 @@ wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self,
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__error__bad_argument;
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 3)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__error__interleaved_coroutine_calls;
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = NULL;
uint32_t coro_susp_point =
@@ -8250,6 +8294,7 @@ wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self,
goto suspend;
suspend:
self->private_impl.c_decode_frame[0].coro_susp_point = coro_susp_point;
self->private_impl.active_coroutine = 3;
goto exit;
exit:
@@ -10015,6 +10060,12 @@ wuffs_gzip__decoder__decode_io_writer(wuffs_gzip__decoder* self,
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__check_wuffs_version_missing;
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 1)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__error__interleaved_coroutine_calls;
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = NULL;
uint8_t v_c = 0;
@@ -10340,6 +10391,7 @@ wuffs_gzip__decoder__decode_io_writer(wuffs_gzip__decoder* self,
goto suspend;
suspend:
self->private_impl.c_decode_io_writer[0].coro_susp_point = coro_susp_point;
self->private_impl.active_coroutine = 1;
self->private_impl.c_decode_io_writer[0].v_flags = v_flags;
self->private_impl.c_decode_io_writer[0].v_checksum_got = v_checksum_got;
self->private_impl.c_decode_io_writer[0].v_decoded_length_got =
@@ -10460,6 +10512,12 @@ wuffs_zlib__decoder__decode_io_writer(wuffs_zlib__decoder* self,
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__check_wuffs_version_missing;
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 1)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__error__interleaved_coroutine_calls;
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = NULL;
uint16_t v_x = 0;
@@ -10638,6 +10696,7 @@ wuffs_zlib__decoder__decode_io_writer(wuffs_zlib__decoder* self,
goto suspend;
suspend:
self->private_impl.c_decode_io_writer[0].coro_susp_point = coro_susp_point;
self->private_impl.active_coroutine = 1;
self->private_impl.c_decode_io_writer[0].v_checksum_got = v_checksum_got;
goto exit;
+48
View File
@@ -460,6 +460,53 @@ const char* do_test_wuffs_gif_decode(const char* filename,
return NULL;
}
const char* test_wuffs_gif_call_interleaved() {
CHECK_FOCUS(__func__);
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
.data = global_src_slice,
});
const char* status = read_file(&src, "test/data/bricks-dither.gif");
if (status) {
return status;
}
wuffs_gif__decoder dec = ((wuffs_gif__decoder){});
status =
wuffs_gif__decoder__check_wuffs_version(&dec, sizeof dec, WUFFS_VERSION);
if (status) {
RETURN_FAIL("check_wuffs_version: \"%s\"", status);
}
{
wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src);
set_reader_limit(&src_reader, 700);
status = wuffs_gif__decoder__decode_frame_config(&dec, NULL, src_reader);
if (status != wuffs_base__suspension__short_read) {
RETURN_FAIL("decode_frame_config: got \"%s\", want \"%s\"", status,
wuffs_base__suspension__short_read);
}
}
// Calling another coroutine (decode_image_config) while suspended inside an
// active coroutine (decode_frame_config) should be an error:
// wuffs_base__error__interleaved_coroutine_calls.
{
wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src);
status = wuffs_gif__decoder__decode_image_config(&dec, NULL, src_reader);
if (status != wuffs_base__error__interleaved_coroutine_calls) {
RETURN_FAIL("decode_image_config: got \"%s\", want \"%s\"", status,
wuffs_base__error__interleaved_coroutine_calls);
}
}
return NULL;
}
const char* test_wuffs_gif_call_sequence() {
CHECK_FOCUS(__func__);
@@ -1534,6 +1581,7 @@ proc tests[] = {
test_basic_status_used_package, //
test_basic_sub_struct_initializer, //
test_wuffs_gif_call_interleaved, //
test_wuffs_gif_call_sequence, //
test_wuffs_gif_decode_animated_big, //
test_wuffs_gif_decode_animated_medium, //