Inline functions that [might] call another function inside?

230 views
Skip to first unread message

zhaox...@gmail.com

unread,
Aug 29, 2016, 2:37:50 PM8/29/16
to golang-dev
Hi,

I want to know if there are any plan/updates to inline functions that [might] call another function inside? It has both code readability benefits and performance benefit? I know this question has been asked before, just want to check if there are any updates? Thanks!

Example of improving readability:

Original:
  
type Visitor struct {
    OnVertex    func (v VertexID) error
    OnEdge      func (e EdgeID) error
}

func Walk(g Graph, visitor Visitor) error {
    ...
    if visitor.OnVertex != nil {
        if err := v.OnVertex(v); err != nil {
            return err
        }
    }
}

If we can do it like this without losing any performance, it will be great!

type Visitor struct {
    OnVertex    func (v VertexID) error
    OnEdge      func (e EdgeID) error
}
func (v Visitor) CallOnVertex(v VertexID) error {
    if v.OnVertex != nil {
        return v.OnVertex(v)
    }
    return nil
}

func (v Visitor) CallOnEdge(e EdgeID) error {
    if v.OnEdge != nil {
        return v.OnEdge(e)
    }
    return nil
}

func Walk(g Graph, visitor Visitor) error {
    ...
    if err := v.OnVertex(v); err != nil {
        return err
    }
}


Example of improving performance;

func (c *Cache) Lookup(e Item) Value {
    v, ok := c.cacheMap[e];
    if !ok {  // This happens very rare.
        v = c.interalHeavyLookup(e)
        c.cacheMap[e] = v
        return v
    }
    return v
}

Otherwise we have to manually inline this function everywhere for performance :(

Keith Randall

unread,
Aug 29, 2016, 2:45:02 PM8/29/16
to zhaox...@gmail.com, golang-dev
We definitely want such a thing.  See https://github.com/golang/go/issues/9337
I don't think anyone is actively working on it.  But if I remember correctly we may have an intern to work on it in the near future.  But don't let that dissuade you if you want to start hacking on it!

--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

zhaox...@gmail.com

unread,
Aug 30, 2016, 1:49:21 PM8/30/16
to golang-dev, zhaox...@gmail.com
Thank you very much for your information! Looking forward to it!
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages