FAQ
Hi there,
Let’s say I have an interface defined somewhere:

type Foo interface {
     Foo() int32
}

and an implementation of this interface:

type foo int32

func (f foo) Foo() int32 {
     return int32(f)
}

so far so good. I work with a map[Foo]interface{} with a lot of entries.

Now I have some other part of my program, which defines the following interface:

type Bar interface {
     Bar() int64
}

along with a simple implementation of this interface to wrap any Foo
value as a bar:

type bar struct {
     f Foo
}

func (b bar) Bar() int64 {
     return int64(b.f.Foo())
}

This extra ‘Bar’ layer costs 16 bytes per instance. Is there a way to
reduce or, better, eliminate the memory overhead from Bar, given that
I need to turn my big map]Foo]interface{} into a map[Bar]interface{},
which requires copying all the entries?

Thanks in advance.

PS: I can’t use a pointer receiver since that would prevent me from
effectively using these values as map keys.

--
Benoit "tsuna" Sigoure

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Egon at Feb 12, 2016 at 1:31 pm

    On Friday, 12 February 2016 11:36:20 UTC+2, tsuna wrote:
    Hi there,
    Let’s say I have an interface defined somewhere:
    I've never needed to write a Foo interface in my code, what are you
    actually trying to accomplish? What are the actual types that you are
    working with? Knowing that upfront will avoid lot of suggestions that don't
    fit your domain.

    Without knowing anything particular about the code, instead of working on
    single items, work on collections. e.g. see how sort uses interfaces.

    + Egon

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Thwd at Feb 12, 2016 at 3:42 pm
    The most "straightforward" answer is to let type foo implement Bar. But as
    Egon said, there's probably a more fitting solution.
    On Friday, February 12, 2016 at 10:36:20 AM UTC+1, tsuna wrote:

    Hi there,
    Let’s say I have an interface defined somewhere:

    type Foo interface {
    Foo() int32
    }

    and an implementation of this interface:

    type foo int32

    func (f foo) Foo() int32 {
    return int32(f)
    }

    so far so good. I work with a map[Foo]interface{} with a lot of entries.

    Now I have some other part of my program, which defines the following
    interface:

    type Bar interface {
    Bar() int64
    }

    along with a simple implementation of this interface to wrap any Foo
    value as a bar:

    type bar struct {
    f Foo
    }

    func (b bar) Bar() int64 {
    return int64(b.f.Foo())
    }

    This extra ‘Bar’ layer costs 16 bytes per instance. Is there a way to
    reduce or, better, eliminate the memory overhead from Bar, given that
    I need to turn my big map]Foo]interface{} into a map[Bar]interface{},
    which requires copying all the entries?

    Thanks in advance.

    PS: I can’t use a pointer receiver since that would prevent me from
    effectively using these values as map keys.

    --
    Benoit "tsuna" Sigoure
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Brad Fitzpatrick at Feb 12, 2016 at 8:51 pm
    Generally, when bytes matter, you don't use interfaces and use some pretty
    custom data layout instead. Or use interfaces but don't store them in
    memory. (e.g. make methods on some type to return interfaces as needed)

    But like Egon said, it's hard to give recommendations without more
    specifics.

    On Fri, Feb 12, 2016 at 1:35 AM, tsuna wrote:

    Hi there,
    Let’s say I have an interface defined somewhere:

    type Foo interface {
    Foo() int32
    }

    and an implementation of this interface:

    type foo int32

    func (f foo) Foo() int32 {
    return int32(f)
    }

    so far so good. I work with a map[Foo]interface{} with a lot of entries.

    Now I have some other part of my program, which defines the following
    interface:

    type Bar interface {
    Bar() int64
    }

    along with a simple implementation of this interface to wrap any Foo
    value as a bar:

    type bar struct {
    f Foo
    }

    func (b bar) Bar() int64 {
    return int64(b.f.Foo())
    }

    This extra ‘Bar’ layer costs 16 bytes per instance. Is there a way to
    reduce or, better, eliminate the memory overhead from Bar, given that
    I need to turn my big map]Foo]interface{} into a map[Bar]interface{},
    which requires copying all the entries?

    Thanks in advance.

    PS: I can’t use a pointer receiver since that would prevent me from
    effectively using these values as map keys.

    --
    Benoit "tsuna" Sigoure

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • David DENG at Feb 13, 2016 at 6:58 am
    How about this?

    type bar int32

    func (b bar) Bar() int64 {
       return int64(b)
    }

    func Foo2Bar(foo Foo) Bar {
       return bar(foo.Foo())
    }


    On Friday, February 12, 2016 at 1:36:20 AM UTC-8, tsuna wrote:

    Hi there,
    Let’s say I have an interface defined somewhere:

    type Foo interface {
    Foo() int32
    }

    and an implementation of this interface:

    type foo int32

    func (f foo) Foo() int32 {
    return int32(f)
    }

    so far so good. I work with a map[Foo]interface{} with a lot of entries.

    Now I have some other part of my program, which defines the following
    interface:

    type Bar interface {
    Bar() int64
    }

    along with a simple implementation of this interface to wrap any Foo
    value as a bar:

    type bar struct {
    f Foo
    }

    func (b bar) Bar() int64 {
    return int64(b.f.Foo())
    }

    This extra ‘Bar’ layer costs 16 bytes per instance. Is there a way to
    reduce or, better, eliminate the memory overhead from Bar, given that
    I need to turn my big map]Foo]interface{} into a map[Bar]interface{},
    which requires copying all the entries?

    Thanks in advance.

    PS: I can’t use a pointer receiver since that would prevent me from
    effectively using these values as map keys.

    --
    Benoit "tsuna" Sigoure
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 12, '16 at 9:35a
activeFeb 13, '16 at 6:58a
posts5
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase