[64]byte()[:]

201 views
Skip to first unread message

Gert

unread,
Nov 15, 2019, 10:32:32 PM11/15/19
to golang-nuts
Is it possible to write this without creating a separate var b64 first? Basically just write it in one line?

var b64 [64]byte // 64 zero bytes
if bytes.Equal(b64[:], x) {}

Aston Motes

unread,
Nov 15, 2019, 11:23:29 PM11/15/19
to Gert, golang-nuts
var b64 [64]byte // 64 zero bytes
var x = make([]byte, 64)
fmt.Println(bytes.Equal(b64[:], x))


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/52e310f4-1fd1-4eb7-833c-70c858edacad%40googlegroups.com.

Gert

unread,
Nov 15, 2019, 11:24:40 PM11/15/19
to golang-nuts
if bytes.Equal(make([]byte, 64), x) {}

answer from slack (Dorian Thiessen)

 

Kevin Malachowski

unread,
Nov 16, 2019, 7:00:02 PM11/16/19
to golang-nuts
"make"ing a byre slice every time you call Equal is not likely as efficient; surely it will put pressure on the garbage collector, assuming it's not just called a few times.

Writing something in less lines is not strictly better. I'd probably just make package level variable and reuse it among multiple calls to that function.

Marcin Romaszewicz

unread,
Nov 16, 2019, 7:32:17 PM11/16/19
to Kevin Malachowski, golang-nuts
It's not even worth calling bytes.Equal to see if a bunch of bytes is zero. A more efficient way would be to simply loop over them and check for non-zero.

-- Marcin

On Sat, Nov 16, 2019 at 4:00 PM Kevin Malachowski <nifta...@gmail.com> wrote:
"make"ing a byre slice every time you call Equal is not likely as efficient; surely it will put pressure on the garbage collector, assuming it's not just called a few times.

Writing something in less lines is not strictly better. I'd probably just make package level variable and reuse it among multiple calls to that function.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Axel Wagner

unread,
Nov 17, 2019, 4:48:22 AM11/17/19
to Kevin Malachowski, golang-nuts
On Sun, Nov 17, 2019 at 1:00 AM Kevin Malachowski <nifta...@gmail.com> wrote:
"make"ing a byre slice every time you call Equal is not likely as efficient; surely it will put pressure on the garbage collector, assuming it's not just called a few times.

AFAICT the compiler correctly deduces that the slice doesn't escape and puts it on the stack. So, no, it doesn't :)
 

Writing something in less lines is not strictly better. I'd probably just make package level variable and reuse it among multiple calls to that function.

Ren Thraysk

unread,
Nov 17, 2019, 11:36:53 AM11/17/19
to golang-nuts
Trick is knowing the compiler can optimize string(b) away. 

const z8 = "\x00\x00\x00\x00\x00\x00\x00\x00"
const z64 = z8 + z8 + z8 + z8 + z8 + z8 + z8 + z8

if string(b64) == z64 {

}

Ren

Kevin Malachowski

unread,
Nov 17, 2019, 12:54:47 PM11/17/19
to Axel Wagner, golang-nuts
Good call, I don't know why I didn't think of the stack allocation.

The conversion to string first is also quite interesting. If bytes.Equal is inlined in practice, profiling is probably the only way to tell which is faster.

Ren Thraysk

unread,
Nov 17, 2019, 1:22:30 PM11/17/19
to golang-nuts

Or look at the source of bytes.Equal() first. :)


Ren


On Sunday, 17 November 2019 17:54:47 UTC, Kevin Malachowski wrote:
Good call, I don't know why I didn't think of the stack allocation.

The conversion to string first is also quite interesting. If bytes.Equal is inlined in practice, profiling is probably the only way to tell which is faster.




On Sun, Nov 17, 2019, 2:48 AM Axel Wagner <axel.w...@googlemail.com> wrote:
On Sun, Nov 17, 2019 at 1:00 AM Kevin Malachowski <nifta...@gmail.com> wrote:
"make"ing a byre slice every time you call Equal is not likely as efficient; surely it will put pressure on the garbage collector, assuming it's not just called a few times.

AFAICT the compiler correctly deduces that the slice doesn't escape and puts it on the stack. So, no, it doesn't :)
 

Writing something in less lines is not strictly better. I'd probably just make package level variable and reuse it among multiple calls to that function.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages