Last active
July 24, 2021 10:19
-
-
Save avivklas/b87c06c17613e1e437789b8e715b3457 to your computer and use it in GitHub Desktop.
selectn blog snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func select4(ctx context.Context, chanz []chan interface{}, res chan *r, r *r, i int) { | |
select { | |
case r.v, r.ok = <-chanz[0]: | |
r.i = i + 0 | |
res <- r | |
case r.v, r.ok = <-chanz[1]: | |
r.i = i + 1 | |
res <- r | |
case r.v, r.ok = <-chanz[2]: | |
r.i = i + 2 | |
res <- r | |
case r.v, r.ok = <-chanz[3]: | |
r.i = i + 3 | |
res <- r | |
case <-ctx.Done(): | |
break | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func select8(ctx context.Context, chanz []chan interface{}, res chan *r, r *r, i int) { | |
select { | |
case r.v, r.ok = <-chanz[0]: | |
r.i = i + 0 | |
res <- r | |
case r.v, r.ok = <-chanz[1]: | |
r.i = i + 1 | |
res <- r | |
case r.v, r.ok = <-chanz[2]: | |
r.i = i + 2 | |
res <- r | |
case r.v, r.ok = <-chanz[3]: | |
r.i = i + 3 | |
res <- r | |
case r.v, r.ok = <-chanz[4]: | |
r.i = i + 4 | |
res <- r | |
case r.v, r.ok = <-chanz[5]: | |
r.i = i + 5 | |
res <- r | |
case r.v, r.ok = <-chanz[6]: | |
r.i = i + 6 | |
res <- r | |
case r.v, r.ok = <-chanz[7]: | |
r.i = i + 7 | |
res <- r | |
case <-ctx.Done(): | |
break | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
agg := aggPool.Get().(chan *r) | |
defer aggPool.Put(agg) | |
for i < len(channels) { | |
l = len(channels) - i | |
switch { | |
case l > 31 && maxBachSize >= 32: | |
go select32(ctx, channels[i:i+32], agg, rPool.Get().(*r), i) | |
i += 32 | |
case l > 15 && maxBachSize >= 16: | |
go select16(ctx, channels[i:i+16], agg, rPool.Get().(*r), i) | |
i += 16 | |
case l > 7 && maxBachSize >= 8: | |
go select8(ctx, channels[i:i+8], agg, rPool.Get().(*r), i) | |
i += 8 | |
case l > 3 && maxBachSize >= 4: | |
go select4(ctx, channels[i:i+4], agg, rPool.Get().(*r), i) | |
i += 4 | |
case l > 1 && maxBachSize >= 2: | |
go select2(ctx, channels[i:i+2], agg, rPool.Get().(*r), i) | |
i += 2 | |
case l > 0: | |
go select1(ctx, channels[i], agg, rPool.Get().(*r), i) | |
i += 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what if the len(channel) is dynamic?