Go :使用编译器诊断标志测试内联是否有效


package fooimport ("runtime""unsafe"
)func add2(p *byte, n uintptr) *byte { // ERROR "can inline add2" "leaking param: p to result"return (*byte)(add1(unsafe.Pointer(p), n)) // ERROR "inlining call to add1"
}func add1(p unsafe.Pointer, x uintptr) unsafe.Pointer { // ERROR "can inline add1" "leaking param: p to result"return unsafe.Pointer(uintptr(p) + x)
}func f(x *byte) *byte { // ERROR "can inline f" "leaking param: x to result"return add2(x, 1) // ERROR "inlining call to add2" "inlining call to add1"
}//go:noinline
func g(x int) int {return x + 1
}func h(x int) int { // ERROR "can inline h"return x + 2
}func i(x int) int { // ERROR "can inline i"const y = 2return x + y
}func j(x int) int { // ERROR "can inline j"switch {case x > 0:return x + 2default:return x + 1}
}func f2() int { // ERROR "can inline f2"tmp1 := htmp2 := tmp1return tmp2(0) // ERROR "inlining call to h"
}var somethingWrong error// local closures can be inlined
func l(x, y int) (int, int, error) { // ERROR "can inline l"e := func(err error) (int, int, error) { // ERROR "can inline l.func1" "func literal does not escape" "leaking param: err to result"return 0, 0, err}if x == y {e(somethingWrong) // ERROR "inlining call to l.func1"} else {f := ef(nil) // ERROR "inlining call to l.func1"}return y, x, nil
}// any re-assignment prevents closure inlining
func m() int {foo := func() int { return 1 } // ERROR "can inline m.func1" "func literal does not escape"x := foo()foo = func() int { return 2 } // ERROR "can inline m.func2" "func literal does not escape"return x + foo()
}// address taking prevents closure inlining
func n() int {foo := func() int { return 1 } // ERROR "can inline n.func1" "func literal does not escape"bar := &foox := (*bar)() + foo()return x
}// make sure assignment inside closure is detected
func o() int {foo := func() int { return 1 } // ERROR "can inline o.func1" "func literal does not escape"func(x int) {                  // ERROR "can inline o.func2"if x > 10 {foo = func() int { return 2 } // ERROR "can inline o.func2"}}(11) // ERROR "func literal does not escape" "inlining call to o.func2"return foo()
}func p() int { // ERROR "can inline p"return func() int { return 42 }() // ERROR "can inline p.func1" "inlining call to p.func1"
}func q(x int) int { // ERROR "can inline q"foo := func() int { return x * 2 } // ERROR "can inline q.func1" "func literal does not escape"return foo()                       // ERROR "inlining call to q.func1"
}func r(z int) int {foo := func(x int) int { // ERROR "can inline r.func1" "func literal does not escape"return x + z}bar := func(x int) int { // ERROR "func literal does not escape" "can inline r.func2"return x + func(y int) int { // ERROR "can inline r.func2.1" "can inline r.func3"return 2*y + x*z}(x) // ERROR "inlining call to r.func2.1"}return foo(42) + bar(42) // ERROR "inlining call to r.func1" "inlining call to r.func2" "inlining call to r.func3"
}func s0(x int) int { // ERROR "can inline s0"foo := func() { // ERROR "can inline s0.func1" "func literal does not escape"x = x + 1}foo() // ERROR "inlining call to s0.func1"return x
}func s1(x int) int { // ERROR "can inline s1"foo := func() int { // ERROR "can inline s1.func1" "func literal does not escape"return x}x = x + 1return foo() // ERROR "inlining call to s1.func1"
}func switchBreak(x, y int) int { // ERROR "can inline switchBreak"var n intswitch x {case 0:n = 1Done:switch y {case 0:n += 10break Done}n = 2}return n
}func switchType(x interface{}) int { // ERROR "can inline switchType" "x does not escape"switch x.(type) {case int:return x.(int)default:return 0}
}// Test that switches on constant things, with constant cases, only cost anything for
// the case that matches. See issue 50253.
func switchConst1(p func(string)) { // ERROR "can inline switchConst" "p does not escape"const c = 1switch c {case 0:p("zero")case 1:p("one")case 2:p("two")default:p("other")}
}func switchConst2() string { // ERROR "can inline switchConst2"switch runtime.GOOS {case "linux":return "Leenooks"case "windows":return "Windoze"case "darwin":return "MackBone"case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100":return "Numbers"default:return "oh nose!"}
}
func switchConst3() string { // ERROR "can inline switchConst3"switch runtime.GOOS {case "Linux":panic("Linux")case "Windows":panic("Windows")case "Darwin":panic("Darwin")case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100":panic("Numbers")default:return "oh nose!"}
}func inlineRangeIntoMe(data []int) { // ERROR "can inline inlineRangeIntoMe" "data does not escape"rangeFunc(data, 12) // ERROR "inlining call to rangeFunc"
}func rangeFunc(xs []int, b int) int { // ERROR "can inline rangeFunc" "xs does not escape"for i, x := range xs {if x == b {return i}}return -1
}type T struct{}func (T) meth(int, int) {} // ERROR "can inline T.meth"func k() (T, int, int) { return T{}, 0, 0 } // ERROR "can inline k"func f3() { // ERROR "can inline f3"T.meth(k()) // ERROR "inlining call to k" "inlining call to T.meth"// ERRORAUTO "inlining call to T.meth"
}func small1() { // ERROR "can inline small1"runtime.GC()
}
func small2() int { // ERROR "can inline small2"return runtime.GOMAXPROCS(0)
}
func small3(t T) { // ERROR "can inline small3"t.meth2(3, 5)
}
func small4(t T) { // not inlineable - has 2 calls.t.meth2(runtime.GOMAXPROCS(0), 5)
}
func (T) meth2(int, int) { // not inlineable - has 2 calls.runtime.GC()runtime.GC()
}// Issue #29737 - make sure we can do inlining for a chain of recursive functions
func ee() { // ERROR "can inline ee"ff(100) // ERROR "inlining call to ff" "inlining call to gg" "inlining call to hh"
}func ff(x int) { // ERROR "can inline ff"if x < 0 {return}gg(x - 1)
}
func gg(x int) { // ERROR "can inline gg"hh(x - 1)
}
func hh(x int) { // ERROR "can inline hh"ff(x - 1) // ERROR "inlining call to ff"  // ERROR "inlining call to gg"
}// Issue #14768 - make sure we can inline for loops.
func for1(fn func() bool) { // ERROR "can inline for1" "fn does not escape"for {if fn() {break} else {continue}}
}func for2(fn func() bool) { // ERROR "can inline for2" "fn does not escape"
Loop:for {if fn() {break Loop} else {continue Loop}}
}// Issue #18493 - make sure we can do inlining of functions with a method value
type T1 struct{}func (a T1) meth(val int) int { // ERROR "can inline T1.meth"return val + 5
}func getMeth(t1 T1) func(int) int { // ERROR "can inline getMeth"return t1.meth // ERROR "t1.meth escapes to heap"// ERRORAUTO "inlining call to T1.meth"
}func ii() { // ERROR "can inline ii"var t1 T1f := getMeth(t1) // ERROR "inlining call to getMeth" "t1.meth does not escape"_ = f(3)
}// Issue #42194 - make sure that functions evaluated in
// go and defer statements can be inlined.
func gd1(int) {defer gd1(gd2()) // ERROR "inlining call to gd2"defer gd3()()    // ERROR "inlining call to gd3"go gd1(gd2())    // ERROR "inlining call to gd2"go gd3()()       // ERROR "inlining call to gd3"
}func gd2() int { // ERROR "can inline gd2"return 1
}func gd3() func() { // ERROR "can inline gd3"return ii
}// Issue #42788 - ensure ODEREF OCONVNOP* OADDR is low cost.
func EncodeQuad(d []uint32, x [6]float32) { // ERROR "can inline EncodeQuad" "d does not escape"_ = d[:6]d[0] = float32bits(x[0]) // ERROR "inlining call to float32bits"d[1] = float32bits(x[1]) // ERROR "inlining call to float32bits"d[2] = float32bits(x[2]) // ERROR "inlining call to float32bits"d[3] = float32bits(x[3]) // ERROR "inlining call to float32bits"d[4] = float32bits(x[4]) // ERROR "inlining call to float32bits"d[5] = float32bits(x[5]) // ERROR "inlining call to float32bits"
}// float32bits is a copy of math.Float32bits to ensure that
// these tests pass with `-gcflags=-l`.
func float32bits(f float32) uint32 { // ERROR "can inline float32bits"return *(*uint32)(unsafe.Pointer(&f))
}// Ensure OCONVNOP is zero cost.
func Conv(v uint64) uint64 { // ERROR "can inline Conv"return conv2(conv2(conv2(v))) // ERROR "inlining call to (conv1|conv2)"
}
func conv2(v uint64) uint64 { // ERROR "can inline conv2"return conv1(conv1(conv1(conv1(v)))) // ERROR "inlining call to conv1"
}
func conv1(v uint64) uint64 { // ERROR "can inline conv1"return uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(v)))))))))))
}func select1(x, y chan bool) int { // ERROR "can inline select1" "x does not escape" "y does not escape"select {case <-x:return 1case <-y:return 2}
}func select2(x, y chan bool) { // ERROR "can inline select2" "x does not escape" "y does not escape"
loop: // test that labeled select can be inlined.select {case <-x:break loopcase <-y:}
}func inlineSelect2(x, y chan bool) { // ERROR "can inline inlineSelect2" ERROR "x does not escape" "y does not escape"
loop:for i := 0; i < 5; i++ {if i == 3 {break loop}select2(x, y) // ERROR "inlining call to select2"}
}

该博文为原创文章,未经博主同意不得转。
本文章博客地址:https://cplusplus.blog.csdn.net/article/details/127639932

Go :使用编译器诊断标志测试内联是否有效(附完整源码)相关推荐

  1. 单独编译和使用webrtc音频降噪模块(附完整源码+测试音频文件)

    单独编译和使用webrtc音频增益模块(附完整源码+测试音频文件) 单独编译和使用webrtc音频回声消除模块(附完整源码+测试音频文件) webrtc的音频处理模块分为降噪ns,回音消除aec,回声 ...

  2. 投票源码程序_[内附完整源码和文档] 基于JSP实现的影视创作论坛系统

    摘 要 随着时代的发展,互联网的出现,给传统影视行业带来的最大便利就是,方便了影视从业人员以及爱好者的交流和互动,而为用户提供一个书写影评,阅读影评以及回复影评的平台,以影评为载体来使用户感受影评.解 ...

  3. [内附完整源码和文档] 基于JSP网上招聘系统的设计与实现

    摘 要 随着时代的发展,中国的互联网技术愈加成熟,已经有越来越多的社会群体开始学会使用互联网技术,整个社会正在朝着智能化.信息化的方向前进.有了互联网,用户便可以足不出户地利用互联网技术使得自己的生活 ...

  4. [内附完整源码和文档] 基于C#和SQL Server 2008的自助点餐系统设计与实现

    1.引言 1.1 编写背景 该项目开发的软件是饭店信息管理系统软件.随着人民的生活水品不断提高,生活也越来越好.就喜欢出去吃饭,酒店等人员来往爆炸性增长.饭店对自身和宾客信息的管理,越来越难.随着这方 ...

  5. [内附完整源码和文档] 基于Android的手机音乐播放器的设计与实现

    摘 要 随着Android系统和移动互联网的快速崛起,手机已经成为人们生活不可缺的一部分,在现代人的生活中,人们生活节奏的加快,生活压力越来越大,碎片化的时间越来越多,那么一个可以在碎片化的时间内调节 ...

  6. [内附完整源码和文档] 基于Android网络聊天室的设计与实现

    前 言 随着我国科技水平的提高和移动通讯的飞速发展与普及,人们通过移动网络可做的事情越来越多,人们之间的沟通不在局限于面对面的对话,人们通过手机可以很方便的上网并通过手机上的APP进行聊天这样既节省话 ...

  7. [内附完整源码和文档] 基于JAVA的合同管理系统

    摘 要 在当今社会的飞速发展,无数的公司和企业诞生,随之也有着大量员工的产生,公司与员工通过合同连接起来.信息化的发展,使得合同管理有了新的高效管理方式的可能. 企业合同管理是市场经济条件下企业经营管 ...

  8. [内附完整源码和文档] 基于Jsp的百货中心供应链管理系统

    摘要 近年来,随着计算机技术的发展,以及信息化时代下企业对效率的需求,计算机技术与通信技术已经被越来越多地应用到各行各业中去.百货中心作为物流产业链中重要的一环,为了应对新兴消费方式的冲击,从供货到销 ...

  9. java项目 干洗店源码,[内附完整源码和文档] 基于Java的洗衣店管理系统

    摘 要 随着科技的快速发展,人们的需求也是越来越多,为了方便对信息的管理我们小组就设计了一个洗衣店订单的管理系统. 洗衣店管理系统是典型的的信息管理系统,创建了六个类,分别是:Test类:Person ...

最新文章

  1. 数据持久化框架为什么放弃Hibernate、JPA、Mybatis,最终选择JDBCTemplate!
  2. jsp注册里密码强弱怎么弄_jsp+servlet实战酷炫博客+聊天系统
  3. VS项目属性中的C/C++运行库:MT、MTd、MD、MDd
  4. plsq如何快捷整理代码_我收藏的几个更快搬砖的vscode快捷键
  5. 用PHP打印出前一天的时间
  6. 美国著名核物理学家,前半生为美国造核弹,后半生为中国放牛
  7. Delphi中的dll操作
  8. OpenCV学习:找出人脸,同时比较两张图片中的人脸相似度
  9. 单片机仿真器和烧写器的区别
  10. 电大计算机网考怎么过,国家开放大学电大考试计算机网考题库大全(必过).doc...
  11. 中国ai创业公司 排行榜_加入AI创业公司之前,您需要问6个问题
  12. mysql limit 含义_深入分析Mysql中limit的用法
  13. 乌班图18.04搭建ssh服务器
  14. 我的物联网项目(三十一) 分销模式电商平台
  15. BMS数据格式规范【BMS Format Specification】
  16. 趣图 | 这该死的自信
  17. 计算机语言指令数据用0,汇编语言-中国大学mooc-题库零氪
  18. MFC界面 插入透明图片(logo)
  19. 360浏览器如何拦截和屏蔽网页广告
  20. 易车、毛豆新车、蛋蛋订车买车到底靠谱吗?

热门文章

  1. vue2--代码高亮
  2. 3.Java获得内网网段所有可通信的ip地址
  3. 会而不议,议而不决,决而不行,行而不果
  4. c语言strtok2个字符连在一起,在C中使用strtok使用多个分隔符的分割字符串
  5. Windows Server 2016 部署DNS
  6. SQL优化_高水位线导致的性能问题
  7. Mac M1上EasyConnect报客户端与服务器版本不一致
  8. python 股票自动下单_【邢不行|量化小讲堂系列24-Python量化入门】股票自动程序化下单交易|视频教程...
  9. ES6-13【正则方法、修饰符yus、UTF_16编码方式】
  10. 视频直播录制软件-OBS Studio 官方版提供下载