#Is there an idiomatic way to make a 32bit variation of my 64bit slice data structure?

13 messages · Page 1 of 1 (latest)

sinful tiger
#

I have a structure like this:

package example

import (
  "encoding/binary"
)

const NodeKeySize = 8

var endian = binary.LittleEndian

type Node []byte

func (n Node) PairInsertAt(at uint8, key uint64, value uint64) {
}

func (n Node) PairAt(at uint8) ([]byte, uint64, uint64, bool) {
    return nil, endian.Uint64(section), endian.Uint64(section[NodeKeySize:]), true
}

I want to make a variation of this in 32bit but still retain much of the logic. For the example above, it would become like the following:

package example

import (
  "encoding/binary"
)

const NodeKeySize = 4

var endian = binary.LittleEndian

type Node []byte

func (n Node) PairInsertAt(at uint8, key uint32, value uint32) {
}

func (n Node) PairAt(at uint8) ([]byte, uint32, uint32, bool) {
    return nil, endian.Uint64(section), endian.Uint64(section[NodeKeySize:]), true
}

Is there an easy way to do this without changing much of the internal logic such as that if I modify the 64bit version, the 32bit version automatically adapts?

foggy palm
sinful tiger
#

I can hack up a sed script that replaces every Uint64 with Uint32 and the like; but I am wondering if there is a more efficient way

foggy palm
#

though idk how much you wanna be doing that seems messy to me

sinful tiger
foggy palm
#

the generics (mainly bc ur switching on T)

#

but that makes it slower whereas distinct functions would probably be better I would think

sinful tiger
#

I see, thanks for your help!

gusty light
#

you could consider using binary.Decode instead, but if performance is a primary concern be aware it may be slightly slower due to an internal type switch

#

it shouldn’t make it to the reflect decoding on simple types like this

#

try it and see 🙂