广告
返回顶部
首页 > 资讯 > 后端开发 > GO >Golang的关键字defer的使用方法
  • 938
分享到

Golang的关键字defer的使用方法

2024-04-02 19:04:59 938人浏览 泡泡鱼
摘要

目录核心思想defer链源码分析优化核心思想 在defer出现的地方插入了指令CALL runtime.deferproc,在函数返回的地方插入了CALL runtime.defer

核心思想

在defer出现的地方插入了指令CALL runtime.deferproc,在函数返回的地方插入了CALL runtime.deferreturn。Goroutine的控制结构中,有一张表记录defer,调用runtime.deferproc时会将需要defer的表达式记录在表中,而在调用runtime.deferreturn的时候,则会依次从defer表中“出栈”并执行

如果有多个defer,调用顺序类似栈,越后面的defer表达式越先被调用

defer链

defer信息会注册到链表,当前执行的 goroutine 持有这个链表的头指针,每个 goroutine 都有一个对应的结构体struct G,其中有一个字段指向这个defer链表头

type g struct {
	// Stack parameters.
	// stack describes the actual stack memory: [stack.lo, stack.hi).
	// stackguard0 is the stack pointer compared in the Go stack growth prologue.
	// It is stack.lo+StackGuard nORMally, but can be StackPreempt to trigger a preemption.
	// stackguard1 is the stack pointer compared in the C stack growth prologue.
	// It is stack.lo+StackGuard on g0 and gsignal stacks.
	// It is ~0 on other goroutine stacks, to trigger a call to morestackc (and crash).
	stack       stack   // offset known to runtime/cgo
	stackguard0 uintptr // offset known to liblink
	stackguard1 uintptr // offset known to liblink

	_panic       *_panic // innermost panic - offset known to liblink
    // _defer 这个字段指向defer链表头
	_defer       *_defer // innermost defer
    ...
}

新注册的defer会添加到链表头,所以感觉像是栈那样先进后出的调用:

源码分析

deferproc一共有两个参数,第一个是参数和返回值的大小,第二个是指向funcval的指针

// Create a new deferred function fn with siz bytes of arguments.
// The compiler turns a defer statement into a call to this.
//go:nosplit
func deferproc(siz int32, fn *funcval) { // arguments of fn follow fn
    // 获取当前goroutine
	gp := getg()
	if gp.m.curg != gp {
		// go code on the system stack can't defer
		throw("defer on system stack")
	}
	// the arguments of fn are in a perilous state. The stack map
	// for deferproc does not describe them. So we can't let garbage
	// collection or stack copying trigger until we've copied them out
	// to somewhere safe. The memmove below does that.
	// Until the copy completes, we can only call nosplit routines.
    // 获取调用者指针
	sp := getcallersp()
    // 通过偏移获得参数
	argp := uintptr(unsafe.Pointer(&fn)) + unsafe.Sizeof(fn)
	callerpc := getcallerpc()

    // 创建defer结构体
	d := newdefer(siz)
	if d._panic != nil {
		throw("deferproc: d.panic != nil after newdefer")
	}
    // 初始化
	d.link = gp._defer
	gp._defer = d
	d.fn = fn
	d.pc = callerpc
	d.sp = sp
	switch siz {
	case 0:
		// Do nothing.
	case sys.PtrSize:
		*(*uintptr)(deferArgs(d)) = *(*uintptr)(unsafe.Pointer(argp))
	default:
		memmove(deferArgs(d), unsafe.Pointer(argp), uintptr(siz))
	}

	// deferproc returns 0 normally.
	// a deferred func that stops a panic
	// makes the deferproc return 1.
	// the code the compiler generates always
	// checks the return value and jumps to the
	// end of the function if deferproc returns != 0.
	return0()
	// No code can go here - the C return reGISter has
	// been set and must not be clobbered.
}
// 以下是_defer结构体
// A _defer holds an entry on the list of deferred calls.
// If you add a field here, add code to clear it in freedefer and deferProcStack
// This struct must match the code in cmd/compile/internal/GC/reflect.go:deferstruct
// and cmd/compile/internal/gc/ssa.go:(*state).call.
// Some defers will be allocated on the stack and some on the heap.
// All defers are logically part of the stack, so write barriers to
// initialize them are not required. All defers must be manually scanned,
// and for heap defers, marked.
type _defer struct {
    // siz 记录defer的参数和返回值共占多少字节
    // 会直接分配在_defer后面,在注册时保存参数,在执行完成时拷贝到调用者参数和返回值空间
	siz     int32 // includes both arguments and results
	// started 标记是否已经执行
    started bool
    // heap go1.13优化,标识是否为堆分配
	heap    bool
	// openDefer indicates that this _defer is for a frame with open-coded
	// defers. We have only one defer record for the entire frame (which may
	// currently have 0, 1, or more defers active).
    // openDefer 是否是open defer,通过这些信息可以找到未注册到链表的defer函数
	openDefer bool
    // sp 记录调用者栈指针,可以通过它判断自己注册的defer是否已经执行完了
	sp        uintptr  // sp at time of defer
    // pc deferproc的返回地址
	pc        uintptr  // pc at time of defer
    // fn 要注册的funcval
	fn        *funcval // can be nil for open-coded defers
    // _panic 指向当前的panic,表示这个defer是由这个panic触发的
	_panic    *_panic  // panic that is running defer
    // link 链到前一个注册的defer结构体
	link      *_defer

	// If openDefer is true, the fields below record values about the stack
	// frame and associated function that has the open-coded defer(s). sp
	// above will be the sp for the frame, and pc will be address of the
	// deferreturn call in the function.
    // 通过这些信息可以找到未注册到链表的defer函数
	fd   unsafe.Pointer // funcdata for the function associated with the frame
	varp uintptr        // value of varp for the stack frame
	// framepc is the current pc associated with the stack frame. Together,
	// with sp above (which is the sp associated with the stack frame),
	// framepc/sp can be used as pc/sp pair to continue a stack trace via
	// gentraceback().
	framepc uintptr
}

defer将参数注册的时候拷贝到堆上,执行时再(将参数和返回值)拷贝回栈上

go会分配不同规格的_defer pool,执行时从空闲_defer中取一个出来用,没有合适的再进行堆分配。用完以后再放回空闲_defer pool。以避免频繁的堆分配和回收

优化

go1.12中defer存在的问题:

  • defer信息主要存储在堆上,要在堆和栈上来回拷贝返回值和参数很慢
  • defer结构体通过链表链起来,而链表的操作也很慢

go1.13中defer的优化:

  • 减少了defer信息的堆分配。再通过deferprocStack将整个defer注册到defer链表中
  • 将一般情况的defer信息存储在函数栈帧的局部变量区域
  • 显示循环或者是隐式循环的defer还是需要用到go1.12中defer信息的堆分配
  • 官方给出的性能提升是30%

go1.14中defer的优化:

  • 在编译阶段插入代码,把defer函数的执行逻辑展开在所属函数内,避免创建defer结构体,而且不需要注册到defer链表。称为 open coded defer
  • 与1.13一样不适用于循环中的defer
  • 性能几乎提升了一个数量级
  • open coded defer 中发生panic 或 调用runtime.Goexit(),后面未注册到的defer函数无法执行到,需要栈扫描。defer结构体中就多添加了一些字段,借助这些字段可以找到未注册到链表中的defer函数

结果就是defer变快了,但是panic变慢了

defer添加了局部变量去判断是否需要执行,需要执行的话就将标识df对应的位上或一下,如果是有条件的defer,需要根据具体条件去或df

deferprocStack

// deferprocStack queues a new deferred function with a defer record on the stack.
// The defer record must have its siz and fn fields initialized.
// All other fields can contain junk.
// The defer record must be immediately followed in memory by
// the arguments of the defer.
// Nosplit because the arguments on the stack won't be scanned
// until the defer record is spliced into the gp._defer list.
//go:nosplit
func deferprocStack(d *_defer) {
    // 获得当前 goroutine
	gp := getg()
	if gp.m.curg != gp {
		// go code on the system stack can't defer
		throw("defer on system stack")
	}
	// siz and fn are already set.
	// The other fields are junk on entry to deferprocStack and
	// are initialized here.
    // 初始化 _defer 信息
	d.started = false
	d.heap = false
	d.openDefer = false
	d.sp = getcallersp()
	d.pc = getcallerpc()
	d.framepc = 0
	d.varp = 0
	// The lines below implement:
	//   d.panic = nil
	//   d.fd = nil
	//   d.link = gp._defer
	//   gp._defer = d
	// But without write barriers. The first three are writes to
	// the stack so they don't need a write barrier, and furthermore
	// are to uninitialized memory, so they must not use a write barrier.
	// The fourth write does not require a write barrier because we
	// explicitly mark all the defer structures, so we don't need to
	// keep track of pointers to them with a write barrier.
	*(*uintptr)(unsafe.Pointer(&d._panic)) = 0
	*(*uintptr)(unsafe.Pointer(&d.fd)) = 0
	*(*uintptr)(unsafe.Pointer(&d.link)) = uintptr(unsafe.Pointer(gp._defer))
	*(*uintptr)(unsafe.Pointer(&gp._defer)) = uintptr(unsafe.Pointer(d))

	return0()
	// No code can go here - the C return register has
	// been set and must not be clobbered.
}

到此这篇关于golang的关键字defer的使用的文章就介绍到这了,更多相关 Golang关键字defer内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

您可能感兴趣的文档:

--结束END--

本文标题: Golang的关键字defer的使用方法

本文链接: https://www.lsjlt.com/news/150871.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Golang的关键字defer的使用方法
    目录核心思想defer链源码分析优化核心思想 在defer出现的地方插入了指令CALL runtime.deferproc,在函数返回的地方插入了CALL runtime.defer...
    99+
    2022-11-13
  • Golang关键字defer的用法详解
    目录1. defer的简单介绍与使用场景2. defer在return执行的时机3. 小结1. defer的简单介绍与使用场景 defer是Go里面的一个关键字,用在方法或函数前面,...
    99+
    2023-05-18
    Golang关键字defer使用 Golang defer使用 Golang defer
  • Golang的关键字defer如何使用
    今天小编给大家分享一下Golang的关键字defer如何使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。核心思想在defe...
    99+
    2023-07-02
  • Golang函数的defer关键字的多种用法
    Golang是一种非常流行的编程语言,其语言特性非常丰富,其中之一就是使用defer关键字来完成一些特定的功能。在本文中,我们将会介绍多种使用defer关键字的方式。延迟函数的执行在Golang中,defer关键字最常用的功能就是延迟函数的...
    99+
    2023-05-17
    函数 Golang defer
  • Golang函数的defer关键字的异常处理方法
    Golang是一种相对新兴、开源且具有高性能的编程语言,它的特点之一就是函数的defer关键字。这个关键字可以让我们在函数结束前执行一些需要操作,如资源清理、日志输出等,同时也可以用来处理异常情况,使我们的代码具有更好的健壮性和可靠性。本文...
    99+
    2023-05-17
    Golang 函数 defer关键字。
  • Golang Defer关键字特定的操作方法有哪些
    本篇内容主要讲解“Golang Defer关键字特定的操作方法有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Golang Defer关键字特定的操作方法有哪些”吧!Go...
    99+
    2023-07-05
  • Golang中的关键字(defer、:=、go func())详细解读
    目录Golang中的关键字(defer、:=、go func())一、defer二、var与 := 的区别1. var 声明变量,如下:2. :=3. 二者区别三、go func补充...
    99+
    2023-05-18
    go  defer := go func()关键字 go  defer := go func()关键字
  • Golang函数的defer关键字在异常处理中的应用
    Golang是一门现代化的编程语言,其简洁、高效的设计风格备受开发者的推崇。在Golang中,函数的defer关键字是一个非常有用的特性,它可以帮助我们在函数返回前进行一些资源的清理工作。不仅如此,defer还可以在异常处理中发挥巨大的作用...
    99+
    2023-05-16
    函数 Golang defer关键字
  • go语言中的defer关键字
    我是谁 defer - 顾名思义翻译过来叫 延迟, 所以我们通常称呼 defer func() 这样 defer 后面紧跟...
    99+
    2022-11-11
  • java关键字的使用方法
    本篇内容介绍了“java关键字的使用方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在平时写代码的时候我们会经常用到void,我们都知道他...
    99+
    2023-06-19
  • 一文搞懂Go语言中defer关键字的使用
    目录前言defer是什么多个defer的执行顺序延迟函数的参数在defer声明时就决定了defer和return的顺序defer和panicdefer下的函数参数包含子函数总结前言 ...
    99+
    2022-11-11
  • defer关键字、panic和recover的示例分析
    这篇文章给大家介绍defer关键字、panic和recover的示例分析,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。defer关键字defer关键字可以让函数或语句延迟到函数语句块的...
    99+
    2022-10-19
  • php clone关键字的使用方法
    这篇文章主要介绍“php clone关键字的使用方法”,在日常操作中,相信很多人在php clone关键字的使用方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”php clone关键字的使用方法”的疑惑有所...
    99+
    2023-06-20
  • SQL中关于distinct关键字的使用方法
    这篇文章主要介绍SQL中关于distinct关键字的使用方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有...
    99+
    2022-10-18
  • Java super关键字的使用方法详解
    构造方法中的super关键字在Java子类的构造方法中可以通过super关键字来调用父类的构造方法。其用法为: 1) super(); 访问父类中的无参构造函数 2) super (paras…); 访问父类中的成员函数yyy super(...
    99+
    2023-05-31
    java super ava
  • Golang函数的defer语句在文件关闭中的用法
    Golang是一种被广泛应用于Web开发的编程语言,其提供了多种强大的数据类型和语言特性,使得在项目开发中可以更加高效地进行编码。其中一个非常实用的特性就是defer语句,用于在函数返回前执行一段代码。在本文中,我们将讨论如何利用Golan...
    99+
    2023-05-16
    Golang defer 文件关闭
  • 关于java中final关键字的使用方法详解
    一、修饰类被final修饰的类不能被子类继承。//父类Animal public final class Animal{ private int age; //年龄 private String var; //品种 public...
    99+
    2015-04-20
    java final 关键字 使用方法 详解
  • C#中using关键字的使用方法示例
    目录1 :using 指令(命名空间)2 :using 语句(1 ):using (ResourceType Identifier = Expression ) State...
    99+
    2022-11-13
  • Java this关键字的使用方法有哪些
    这篇文章主要介绍“Java this关键字的使用方法有哪些”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Java this关键字的使用方法有哪些”文章能帮助大家解决问题。Boy类...
    99+
    2023-06-26
  • 详解java中private关键字的使用方法
    private 关键字中文就是私有关键字,那么到底要怎么使用呢?1、只能在同一类中访问class A { private String msg="Try to access the private variable outside ...
    99+
    2016-08-22
    java入门 java private
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作