Or look at the source of bytes.Equal() first. :)
https://golang.org/src/bytes/bytes.go?s=505:533#L8
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 in
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
wrote:
> On Sun, Nov 17,
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
On Saturday, 16 November 2019 03:32:32 UTC, Gert wrote:
>
> Is it possible to write this without creating a
On Sun, Nov 17, 2019 at 1:00 AM Kevin Malachowski
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 does
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
wrote:
> "make"ing a byre slice every time you call Equal is not likely as
> eff
"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
On Saturday, November 16, 2019 at 4:32:32 AM UTC+1, Gert wrote:
> 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) {}
>
>
if bytes.Equal(make([]byte, 64), x) {}
answer