Repository: teh-cmc/go-internals
Branch: master
Commit: dd226910127e
Files: 24
Total size: 175.5 KB
Directory structure:
gitextract_5lpvqa3t/
├── .gitignore
├── LICENSE
├── Makefile
├── README.md
├── chapter1_assembly_primer/
│ ├── Makefile
│ ├── README.md
│ └── direct_topfunc_call.go
├── chapter2_interfaces/
│ ├── Makefile
│ ├── README.md
│ ├── compound_interface.go
│ ├── direct_calls.go
│ ├── dump_sym.sh
│ ├── eface_scalar_test.go
│ ├── eface_to_type.go
│ ├── eface_to_type_test.go
│ ├── eface_type_hash.go
│ ├── escape.go
│ ├── escape_test.go
│ ├── iface.go
│ ├── iface_bench_test.go
│ ├── iface_type_hash.go
│ ├── zerobase.go
│ └── zeroval.go
└── chapter3_garbage_collector/
└── README.md
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
*.gor
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.bin
*.test
*.sublime-workspace
*.sw*
*.un*
================================================
FILE: LICENSE
================================================
Attribution-NonCommercial-ShareAlike 4.0 International
=======================================================================
Creative Commons Corporation ("Creative Commons") is not a law firm and
does not provide legal services or legal advice. Distribution of
Creative Commons public licenses does not create a lawyer-client or
other relationship. Creative Commons makes its licenses and related
information available on an "as-is" basis. Creative Commons gives no
warranties regarding its licenses, any material licensed under their
terms and conditions, or any related information. Creative Commons
disclaims all liability for damages resulting from their use to the
fullest extent possible.
Using Creative Commons Public Licenses
Creative Commons public licenses provide a standard set of terms and
conditions that creators and other rights holders may use to share
original works of authorship and other material subject to copyright
and certain other rights specified in the public license below. The
following considerations are for informational purposes only, are not
exhaustive, and do not form part of our licenses.
Considerations for licensors: Our public licenses are
intended for use by those authorized to give the public
permission to use material in ways otherwise restricted by
copyright and certain other rights. Our licenses are
irrevocable. Licensors should read and understand the terms
and conditions of the license they choose before applying it.
Licensors should also secure all rights necessary before
applying our licenses so that the public can reuse the
material as expected. Licensors should clearly mark any
material not subject to the license. This includes other CC-
licensed material, or material used under an exception or
limitation to copyright. More considerations for licensors:
wiki.creativecommons.org/Considerations_for_licensors
Considerations for the public: By using one of our public
licenses, a licensor grants the public permission to use the
licensed material under specified terms and conditions. If
the licensor's permission is not necessary for any reason--for
example, because of any applicable exception or limitation to
copyright--then that use is not regulated by the license. Our
licenses grant only permissions under copyright and certain
other rights that a licensor has authority to grant. Use of
the licensed material may still be restricted for other
reasons, including because others have copyright or other
rights in the material. A licensor may make special requests,
such as asking that all changes be marked or described.
Although not required by our licenses, you are encouraged to
respect those requests where reasonable. More_considerations
for the public:
wiki.creativecommons.org/Considerations_for_licensees
=======================================================================
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
Public License
By exercising the Licensed Rights (defined below), You accept and agree
to be bound by the terms and conditions of this Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International Public License
("Public License"). To the extent this Public License may be
interpreted as a contract, You are granted the Licensed Rights in
consideration of Your acceptance of these terms and conditions, and the
Licensor grants You such rights in consideration of benefits the
Licensor receives from making the Licensed Material available under
these terms and conditions.
Section 1 -- Definitions.
a. Adapted Material means material subject to Copyright and Similar
Rights that is derived from or based upon the Licensed Material
and in which the Licensed Material is translated, altered,
arranged, transformed, or otherwise modified in a manner requiring
permission under the Copyright and Similar Rights held by the
Licensor. For purposes of this Public License, where the Licensed
Material is a musical work, performance, or sound recording,
Adapted Material is always produced where the Licensed Material is
synched in timed relation with a moving image.
b. Adapter's License means the license You apply to Your Copyright
and Similar Rights in Your contributions to Adapted Material in
accordance with the terms and conditions of this Public License.
c. BY-NC-SA Compatible License means a license listed at
creativecommons.org/compatiblelicenses, approved by Creative
Commons as essentially the equivalent of this Public License.
d. Copyright and Similar Rights means copyright and/or similar rights
closely related to copyright including, without limitation,
performance, broadcast, sound recording, and Sui Generis Database
Rights, without regard to how the rights are labeled or
categorized. For purposes of this Public License, the rights
specified in Section 2(b)(1)-(2) are not Copyright and Similar
Rights.
e. Effective Technological Measures means those measures that, in the
absence of proper authority, may not be circumvented under laws
fulfilling obligations under Article 11 of the WIPO Copyright
Treaty adopted on December 20, 1996, and/or similar international
agreements.
f. Exceptions and Limitations means fair use, fair dealing, and/or
any other exception or limitation to Copyright and Similar Rights
that applies to Your use of the Licensed Material.
g. License Elements means the license attributes listed in the name
of a Creative Commons Public License. The License Elements of this
Public License are Attribution, NonCommercial, and ShareAlike.
h. Licensed Material means the artistic or literary work, database,
or other material to which the Licensor applied this Public
License.
i. Licensed Rights means the rights granted to You subject to the
terms and conditions of this Public License, which are limited to
all Copyright and Similar Rights that apply to Your use of the
Licensed Material and that the Licensor has authority to license.
j. Licensor means the individual(s) or entity(ies) granting rights
under this Public License.
k. NonCommercial means not primarily intended for or directed towards
commercial advantage or monetary compensation. For purposes of
this Public License, the exchange of the Licensed Material for
other material subject to Copyright and Similar Rights by digital
file-sharing or similar means is NonCommercial provided there is
no payment of monetary compensation in connection with the
exchange.
l. Share means to provide material to the public by any means or
process that requires permission under the Licensed Rights, such
as reproduction, public display, public performance, distribution,
dissemination, communication, or importation, and to make material
available to the public including in ways that members of the
public may access the material from a place and at a time
individually chosen by them.
m. Sui Generis Database Rights means rights other than copyright
resulting from Directive 96/9/EC of the European Parliament and of
the Council of 11 March 1996 on the legal protection of databases,
as amended and/or succeeded, as well as other essentially
equivalent rights anywhere in the world.
n. You means the individual or entity exercising the Licensed Rights
under this Public License. Your has a corresponding meaning.
Section 2 -- Scope.
a. License grant.
1. Subject to the terms and conditions of this Public License,
the Licensor hereby grants You a worldwide, royalty-free,
non-sublicensable, non-exclusive, irrevocable license to
exercise the Licensed Rights in the Licensed Material to:
a. reproduce and Share the Licensed Material, in whole or
in part, for NonCommercial purposes only; and
b. produce, reproduce, and Share Adapted Material for
NonCommercial purposes only.
2. Exceptions and Limitations. For the avoidance of doubt, where
Exceptions and Limitations apply to Your use, this Public
License does not apply, and You do not need to comply with
its terms and conditions.
3. Term. The term of this Public License is specified in Section
6(a).
4. Media and formats; technical modifications allowed. The
Licensor authorizes You to exercise the Licensed Rights in
all media and formats whether now known or hereafter created,
and to make technical modifications necessary to do so. The
Licensor waives and/or agrees not to assert any right or
authority to forbid You from making technical modifications
necessary to exercise the Licensed Rights, including
technical modifications necessary to circumvent Effective
Technological Measures. For purposes of this Public License,
simply making modifications authorized by this Section 2(a)
(4) never produces Adapted Material.
5. Downstream recipients.
a. Offer from the Licensor -- Licensed Material. Every
recipient of the Licensed Material automatically
receives an offer from the Licensor to exercise the
Licensed Rights under the terms and conditions of this
Public License.
b. Additional offer from the Licensor -- Adapted Material.
Every recipient of Adapted Material from You
automatically receives an offer from the Licensor to
exercise the Licensed Rights in the Adapted Material
under the conditions of the Adapter's License You apply.
c. No downstream restrictions. You may not offer or impose
any additional or different terms or conditions on, or
apply any Effective Technological Measures to, the
Licensed Material if doing so restricts exercise of the
Licensed Rights by any recipient of the Licensed
Material.
6. No endorsement. Nothing in this Public License constitutes or
may be construed as permission to assert or imply that You
are, or that Your use of the Licensed Material is, connected
with, or sponsored, endorsed, or granted official status by,
the Licensor or others designated to receive attribution as
provided in Section 3(a)(1)(A)(i).
b. Other rights.
1. Moral rights, such as the right of integrity, are not
licensed under this Public License, nor are publicity,
privacy, and/or other similar personality rights; however, to
the extent possible, the Licensor waives and/or agrees not to
assert any such rights held by the Licensor to the limited
extent necessary to allow You to exercise the Licensed
Rights, but not otherwise.
2. Patent and trademark rights are not licensed under this
Public License.
3. To the extent possible, the Licensor waives any right to
collect royalties from You for the exercise of the Licensed
Rights, whether directly or through a collecting society
under any voluntary or waivable statutory or compulsory
licensing scheme. In all other cases the Licensor expressly
reserves any right to collect such royalties, including when
the Licensed Material is used other than for NonCommercial
purposes.
Section 3 -- License Conditions.
Your exercise of the Licensed Rights is expressly made subject to the
following conditions.
a. Attribution.
1. If You Share the Licensed Material (including in modified
form), You must:
a. retain the following if it is supplied by the Licensor
with the Licensed Material:
i. identification of the creator(s) of the Licensed
Material and any others designated to receive
attribution, in any reasonable manner requested by
the Licensor (including by pseudonym if
designated);
ii. a copyright notice;
iii. a notice that refers to this Public License;
iv. a notice that refers to the disclaimer of
warranties;
v. a URI or hyperlink to the Licensed Material to the
extent reasonably practicable;
b. indicate if You modified the Licensed Material and
retain an indication of any previous modifications; and
c. indicate the Licensed Material is licensed under this
Public License, and include the text of, or the URI or
hyperlink to, this Public License.
2. You may satisfy the conditions in Section 3(a)(1) in any
reasonable manner based on the medium, means, and context in
which You Share the Licensed Material. For example, it may be
reasonable to satisfy the conditions by providing a URI or
hyperlink to a resource that includes the required
information.
3. If requested by the Licensor, You must remove any of the
information required by Section 3(a)(1)(A) to the extent
reasonably practicable.
b. ShareAlike.
In addition to the conditions in Section 3(a), if You Share
Adapted Material You produce, the following conditions also apply.
1. The Adapter's License You apply must be a Creative Commons
license with the same License Elements, this version or
later, or a BY-NC-SA Compatible License.
2. You must include the text of, or the URI or hyperlink to, the
Adapter's License You apply. You may satisfy this condition
in any reasonable manner based on the medium, means, and
context in which You Share Adapted Material.
3. You may not offer or impose any additional or different terms
or conditions on, or apply any Effective Technological
Measures to, Adapted Material that restrict exercise of the
rights granted under the Adapter's License You apply.
Section 4 -- Sui Generis Database Rights.
Where the Licensed Rights include Sui Generis Database Rights that
apply to Your use of the Licensed Material:
a. for the avoidance of doubt, Section 2(a)(1) grants You the right
to extract, reuse, reproduce, and Share all or a substantial
portion of the contents of the database for NonCommercial purposes
only;
b. if You include all or a substantial portion of the database
contents in a database in which You have Sui Generis Database
Rights, then the database in which You have Sui Generis Database
Rights (but not its individual contents) is Adapted Material,
including for purposes of Section 3(b); and
c. You must comply with the conditions in Section 3(a) if You Share
all or a substantial portion of the contents of the database.
For the avoidance of doubt, this Section 4 supplements and does not
replace Your obligations under this Public License where the Licensed
Rights include other Copyright and Similar Rights.
Section 5 -- Disclaimer of Warranties and Limitation of Liability.
a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.
b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.
c. The disclaimer of warranties and limitation of liability provided
above shall be interpreted in a manner that, to the extent
possible, most closely approximates an absolute disclaimer and
waiver of all liability.
Section 6 -- Term and Termination.
a. This Public License applies for the term of the Copyright and
Similar Rights licensed here. However, if You fail to comply with
this Public License, then Your rights under this Public License
terminate automatically.
b. Where Your right to use the Licensed Material has terminated under
Section 6(a), it reinstates:
1. automatically as of the date the violation is cured, provided
it is cured within 30 days of Your discovery of the
violation; or
2. upon express reinstatement by the Licensor.
For the avoidance of doubt, this Section 6(b) does not affect any
right the Licensor may have to seek remedies for Your violations
of this Public License.
c. For the avoidance of doubt, the Licensor may also offer the
Licensed Material under separate terms or conditions or stop
distributing the Licensed Material at any time; however, doing so
will not terminate this Public License.
d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
License.
Section 7 -- Other Terms and Conditions.
a. The Licensor shall not be bound by any additional or different
terms or conditions communicated by You unless expressly agreed.
b. Any arrangements, understandings, or agreements regarding the
Licensed Material not stated herein are separate from and
independent of the terms and conditions of this Public License.
Section 8 -- Interpretation.
a. For the avoidance of doubt, this Public License does not, and
shall not be interpreted to, reduce, limit, restrict, or impose
conditions on any use of the Licensed Material that could lawfully
be made without permission under this Public License.
b. To the extent possible, if any provision of this Public License is
deemed unenforceable, it shall be automatically reformed to the
minimum extent necessary to make it enforceable. If the provision
cannot be reformed, it shall be severed from this Public License
without affecting the enforceability of the remaining terms and
conditions.
c. No term or condition of this Public License will be waived and no
failure to comply consented to unless expressly agreed to by the
Licensor.
d. Nothing in this Public License constitutes or may be interpreted
as a limitation upon, or waiver of, any privileges and immunities
that apply to the Licensor or You, including from the legal
processes of any jurisdiction or authority.
=======================================================================
Creative Commons is not a party to its public
licenses. Notwithstanding, Creative Commons may elect to apply one of
its public licenses to material it publishes and in those instances
will be considered the “Licensor.” The text of the Creative Commons
public licenses is dedicated to the public domain under the CC0 Public
Domain Dedication. Except for the limited purpose of indicating that
material is shared under a Creative Commons public license or as
otherwise permitted by the Creative Commons policies published at
creativecommons.org/policies, Creative Commons does not authorize the
use of the trademark "Creative Commons" or any other trademark or logo
of Creative Commons without its prior written consent including,
without limitation, in connection with any unauthorized modifications
to any of its public licenses or any other arrangements,
understandings, or agreements concerning use of licensed material. For
the avoidance of doubt, this paragraph does not form part of the
public licenses.
Creative Commons may be contacted at creativecommons.org.
================================================
FILE: Makefile
================================================
.PHONY: toc
toc:
docker run --rm -it -v ${PWD}:/usr/src jorgeandrada/doctoc --github
$(shell tail -n +`grep -n '# \`go-internals\`' README.md | tr ':' ' ' | awk '{print $$1}'` README.md > /tmp/README2.md)
cp /tmp/README2.md README.md
================================================
FILE: README.md
================================================
# `go-internals`
`go-internals` is a work-in-progress book about the internals of the Go (1.10+) programming language.
---
## Table of Contents
- [Chapter I: A Primer on Go Assembly](./chapter1_assembly_primer/README.md)
- [Chapter II: Interfaces](./chapter2_interfaces/README.md)
- [Chapter III (soon!): The Garbage Collector](./chapter3_garbage_collector/README.md)
---
*Click [here](https://cmc.gitbook.io/go-internals/) for the GitBook version.*
## Goals
- **Concise**: The book aims to be as concise as possible, encouraging code and diagrams over lengthy prose.
- **Community-effort**: I myself am learning as I go through the writing of this book. I *will* make mistakes along the way. Hopefully the community can help in pointing out and correcting these mistakes.
- **Beyond the theory**: The book will not just cover the theory, but the actual implementation too. Assumptions will be proven or invalidated via experiments and measurements.
- **Up-to-Date**: The book will try to keep up-to-date with new Go versions being released.
- **Experienced audience**: The Go community has created *loads* of great introductory material for newcomers. Unfortunately we're still lacking good resources when it comes to the more advanced stuff. This books hopes to help solve this issue.
## Translations
- [Chinese](https://github.com/go-internals-cn/go-internals)
## Contributing
All kinds of contributions are very much welcome.
Don't hesitate to open an issue for e.g...:
- pointing out technical or english mistakes
- suggesting improvements and/or additions to existing chapters
- suggesting external links that might be of interest
- ..and pretty much anything else you can think of, really!
## Author
Clement Rey <<cr.rey.clement@gmail.com>> ([@teh_cmc](https://twitter.com/teh_cmc))
## License
Licensed under the [BY-NC-SA Creative Commons 4.0 International Public License](http://creativecommons.org/licenses/by-nc-sa/4.0/)
================================================
FILE: chapter1_assembly_primer/Makefile
================================================
GOOS=linux
GOARCH=amd64
SOURCES := $(wildcard *.go)
OBJECTS = $(SOURCES:.go=.o)
EXECUTABLES = $(OBJECTS:.o=.bin)
.SECONDARY: ${OBJECTS}
all: ${EXECUTABLES}
%.o: %.go
GOOS=${GOOS} GOARCH=${GOARCH} go tool compile $<
%.bin: %.o
GOOS=${GOOS} GOARCH=${GOARCH} go tool link -o $@ $<
clean:
rm -f ${OBJECTS}
rm -f ${EXECUTABLES}
================================================
FILE: chapter1_assembly_primer/README.md
================================================
<!-- Copyright © 2018 Clement Rey <cr.rey.clement@gmail.com>. -->
<!-- Licensed under the BY-NC-SA Creative Commons 4.0 International Public License. -->
```Bash
$ go version
go version go1.10 linux/amd64
```
# Chapter I: A Primer on Go Assembly
Developing some familiarity with Go's abstract assembly language is a must before we can start delving into the implementation of the runtime & standard library.
This quick guide should hopefully get you up-to-speed.
---
**Table of Contents**
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
- ["Pseudo-assembly"](#pseudo-assembly)
- [Decomposing a simple program](#decomposing-a-simple-program)
- [Dissecting `add`](#dissecting-add)
- [Dissecting `main`](#dissecting-main)
- [A word about goroutines, stacks and splits](#a-word-about-goroutines-stacks-and-splits)
- [Stacks](#stacks)
- [Splits](#splits)
- [Minus some subtleties](#minus-some-subtleties)
- [Conclusion](#conclusion)
- [Links](#links)
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
---
- *This chapter assumes some basic knowledge of any kind of assembler.*
- *If and when running into architecture-specific matters, always assume `linux/amd64`.*
- *We will always work with compiler optimizations **enabled**.*
- *Quoted text and/or comments always come from the official documentation and/or codebase, unless stated otherwise.*
## "Pseudo-assembly"
The Go compiler outputs an abstract, portable form of assembly that doesn't actually map to any real hardware. The Go assembler then uses this pseudo-assembly output in order to generate concrete, machine-specific instructions for the targeted hardware.
This extra layer has many benefits, the main one being how easy it makes porting Go to new architectures. For more information, have a look at Rob Pike's *The Design of the Go Assembler*, listed in the links at the end of this chapter.
> The most important thing to know about Go's assembler is that it is not a direct representation of the underlying machine. Some of the details map precisely to the machine, but some do not. This is because the compiler suite needs no assembler pass in the usual pipeline. Instead, the compiler operates on a kind of semi-abstract instruction set, and instruction selection occurs partly after code generation. The assembler works on the semi-abstract form, so when you see an instruction like MOV what the toolchain actually generates for that operation might not be a move instruction at all, perhaps a clear or load. Or it might correspond exactly to the machine instruction with that name. In general, machine-specific operations tend to appear as themselves, while more general concepts like memory move and subroutine call and return are more abstract. The details vary with architecture, and we apologize for the imprecision; the situation is not well-defined.
> The assembler program is a way to parse a description of that semi-abstract instruction set and turn it into instructions to be input to the linker.
## Decomposing a simple program
Consider the following Go code ([direct_topfunc_call.go](./direct_topfunc_call.go)):
```Go
//go:noinline
func add(a, b int32) (int32, bool) { return a + b, true }
func main() { add(10, 32) }
```
*(Note the `//go:noinline` compiler-directive here... Don't get bitten.)*
Let's compile this down to assembly:
```
$ GOOS=linux GOARCH=amd64 go tool compile -S direct_topfunc_call.go
```
```Assembly
0x0000 TEXT "".add(SB), NOSPLIT, $0-16
0x0000 FUNCDATA $0, gclocals·f207267fbf96a0178e8758c6e3e0ce28(SB)
0x0000 FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 MOVL "".b+12(SP), AX
0x0004 MOVL "".a+8(SP), CX
0x0008 ADDL CX, AX
0x000a MOVL AX, "".~r2+16(SP)
0x000e MOVB $1, "".~r3+20(SP)
0x0013 RET
0x0000 TEXT "".main(SB), $24-0
;; ...omitted stack-split prologue...
0x000f SUBQ $24, SP
0x0013 MOVQ BP, 16(SP)
0x0018 LEAQ 16(SP), BP
0x001d FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x001d FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x001d MOVQ $137438953482, AX
0x0027 MOVQ AX, (SP)
0x002b PCDATA $0, $0
0x002b CALL "".add(SB)
0x0030 MOVQ 16(SP), BP
0x0035 ADDQ $24, SP
0x0039 RET
;; ...omitted stack-split epilogue...
```
We'll dissect those 2 functions line-by-line in order to get a better understanding of what the compiler is doing.
### Dissecting `add`
```Assembly
0x0000 TEXT "".add(SB), NOSPLIT, $0-16
```
- `0x0000`: Offset of the current instruction, relative to the start of the function.
- `TEXT "".add`: The `TEXT` directive declares the `"".add` symbol as part of the `.text` section (i.e. runnable code) and indicates that the instructions that follow are the body of the function.
The empty string `""` will be replaced by the name of the current package at link-time: i.e., `"".add` will become `main.add` once linked into our final binary.
- `(SB)`: `SB` is the virtual register that holds the "static-base" pointer, i.e. the address of the beginning of the address-space of our program.
`"".add(SB)` declares that our symbol is located at some constant offset (computed by the linker) from the start of our address-space. Put differently, it has an absolute, direct address: it's a global function symbol.
Good ol' `objdump` will confirm all of that for us:
```
$ objdump -j .text -t direct_topfunc_call | grep 'main.add'
000000000044d980 g F .text 000000000000000f main.add
```
> All user-defined symbols are written as offsets to the pseudo-registers FP (arguments and locals) and SB (globals).
> The SB pseudo-register can be thought of as the origin of memory, so the symbol foo(SB) is the name foo as an address in memory.
- `NOSPLIT`: Indicates to the compiler that it should *not* insert the *stack-split* preamble, which checks whether the current stack needs to be grown.
In the case of our `add` function, the compiler has set the flag by itself: it is smart enough to figure that, since `add` has no local variables and no stack-frame of its own, it simply cannot outgrow the current stack; thus it'd be a complete waste of CPU cycles to run these checks at each call site.
> "NOSPLIT": Don't insert the preamble to check if the stack must be split. The frame for the routine, plus anything it calls, must fit in the spare space at the top of the stack segment. Used to protect routines such as the stack splitting code itself.
We'll have a quick word about goroutines and stack-splits at the end this chapter.
- `$0-16`: `$0` denotes the size in bytes of the stack-frame that will be allocated; while `$16` specifies the size of the arguments passed in by the caller.
> In the general case, the frame size is followed by an argument size, separated by a minus sign. (It's not a subtraction, just idiosyncratic syntax.) The frame size $24-8 states that the function has a 24-byte frame and is called with 8 bytes of argument, which live on the caller's frame. If NOSPLIT is not specified for the TEXT, the argument size must be provided. For assembly functions with Go prototypes, go vet will check that the argument size is correct.
```Assembly
0x0000 FUNCDATA $0, gclocals·f207267fbf96a0178e8758c6e3e0ce28(SB)
0x0000 FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
```
> The FUNCDATA and PCDATA directives contain information for use by the garbage collector; they are introduced by the compiler.
Don't worry about this for now; we'll come back to it when diving into garbage collection later in the book.
```Assembly
0x0000 MOVL "".b+12(SP), AX
0x0004 MOVL "".a+8(SP), CX
```
The Go calling convention mandates that every argument must be passed on the stack, using the pre-reserved space on the caller's stack-frame.
It is the caller's responsibility to grow (and shrink back) the stack appropriately so that arguments can be passed to the callee, and potential return-values passed back to the caller.
The Go compiler never generates instructions from the PUSH/POP family: the stack is grown or shrunk by respectively decrementing or incrementing the ~virtual~ hardware stack pointer `SP`.
*[UPDATE: We've discussed about this matter in [issue #21: about SP register](https://github.com/teh-cmc/go-internals/issues/21).]*
> The SP pseudo-register is a virtual stack pointer used to refer to frame-local variables and the arguments being prepared for function calls. It points to the top of the local stack frame, so references should use negative offsets in the range [−framesize, 0): x-8(SP), y-4(SP), and so on.
Although the official documentation states that "*All user-defined symbols are written as offsets to the pseudo-register FP (arguments and locals)*", this is only ever true for hand-written code.
Like most recent compilers, the Go tool suite always references argument and locals using offsets from the stack-pointer directly in the code it generates. This allows for the frame-pointer to be used as an extra general-purpose register on platform with fewer registers (e.g. x86).
Have a look at *Stack frame layout on x86-64* in the links at the end of this chapter if you enjoy this kind of nitty gritty details.
*[UPDATE: We've discussed about this matter in [issue #2: Frame pointer](https://github.com/teh-cmc/go-internals/issues/2).]*
`"".b+12(SP)` and `"".a+8(SP)` respectively refer to the addresses 12 bytes and 8 bytes below the top of the stack (remember: it grows downwards!).
`.a` and `.b` are arbitrary aliases given to the referred locations; although *they have absolutely no semantic meaning* whatsoever, they are mandatory when using relative addressing on virtual registers.
The documentation about the virtual frame-pointer has some to say about this:
> The FP pseudo-register is a virtual frame pointer used to refer to function arguments. The compilers maintain a virtual frame pointer and refer to the arguments on the stack as offsets from that pseudo-register. Thus 0(FP) is the first argument to the function, 8(FP) is the second (on a 64-bit machine), and so on. However, when referring to a function argument this way, it is necessary to place a name at the beginning, as in first_arg+0(FP) and second_arg+8(FP). (The meaning of the offset —offset from the frame pointer— distinct from its use with SB, where it is an offset from the symbol.) The assembler enforces this convention, rejecting plain 0(FP) and 8(FP). The actual name is semantically irrelevant but should be used to document the argument's name.
Finally, there are two important things to note here:
1. The first argument `a` is not located at `0(SP)`, but rather at `8(SP)`; that's because the caller stores its return-address in `0(SP)` via the `CALL` pseudo-instruction.
2. Arguments are passed in reverse-order; i.e. the first argument is the closest to the top of the stack.
```Assembly
0x0008 ADDL CX, AX
0x000a MOVL AX, "".~r2+16(SP)
0x000e MOVB $1, "".~r3+20(SP)
```
`ADDL` does the actual addition of the two **L**ong-words (i.e. 4-byte values) stored in `AX` and `CX`, then stores the final result in `AX`.
That result is then moved over to `"".~r2+16(SP)`, where the caller had previously reserved some stack space and expects to find its return values. Once again, `"".~r2` has no semantic meaning here.
To demonstrate how Go handles multiple return-values, we're also returning a constant `true` boolean value.
The mechanics at play are exactly the same as for our first return value; only the offset relative to `SP` changes.
```Assembly
0x0013 RET
```
A final `RET` pseudo-instruction tells the Go assembler to insert whatever instructions are required by the calling convention of the target platform in order to properly return from a subroutine call.
Most likely this will cause the code to pop off the return-address stored at `0(SP)` then jump back to it.
> The last instruction in a TEXT block must be some sort of jump, usually a RET (pseudo-)instruction.
> (If it's not, the linker will append a jump-to-itself instruction; there is no fallthrough in TEXTs.)
That's a lot of syntax and semantics to ingest all at once. Here's a quick inlined summary of what we've just covered:
```Assembly
;; Declare global function symbol "".add (actually main.add once linked)
;; Do not insert stack-split preamble
;; 0 bytes of stack-frame, 16 bytes of arguments passed in
;; func add(a, b int32) (int32, bool)
0x0000 TEXT "".add(SB), NOSPLIT, $0-16
;; ...omitted FUNCDATA stuff...
0x0000 MOVL "".b+12(SP), AX ;; move second Long-word (4B) argument from caller's stack-frame into AX
0x0004 MOVL "".a+8(SP), CX ;; move first Long-word (4B) argument from caller's stack-frame into CX
0x0008 ADDL CX, AX ;; compute AX=CX+AX
0x000a MOVL AX, "".~r2+16(SP) ;; move addition result (AX) into caller's stack-frame
0x000e MOVB $1, "".~r3+20(SP) ;; move `true` boolean (constant) into caller's stack-frame
0x0013 RET ;; jump to return address stored at 0(SP)
```
All in all, here's a visual representation of what the stack looks like when `main.add` has finished executing:
```
| +-------------------------+ <-- 32(SP)
| | |
G | | |
R | | |
O | | main.main's saved |
W | | frame-pointer (BP) |
S | |-------------------------| <-- 24(SP)
| | [alignment] |
D | | "".~r3 (bool) = 1/true | <-- 21(SP)
O | |-------------------------| <-- 20(SP)
W | | |
N | | "".~r2 (int32) = 42 |
W | |-------------------------| <-- 16(SP)
A | | |
R | | "".b (int32) = 32 |
D | |-------------------------| <-- 12(SP)
S | | |
| | "".a (int32) = 10 |
| |-------------------------| <-- 8(SP)
| | |
| | |
| | |
\ | / | return address to |
\|/ | main.main + 0x30 |
- +-------------------------+ <-- 0(SP) (TOP OF STACK)
(diagram made with https://textik.com)
```
<!-- https://textik.com/#af55d3485eaa56f2 -->
### Dissecting `main`
We'll spare you some unnecessary scrolling, here's a reminder of what our `main` function looks like:
```Assembly
0x0000 TEXT "".main(SB), $24-0
;; ...omitted stack-split prologue...
0x000f SUBQ $24, SP
0x0013 MOVQ BP, 16(SP)
0x0018 LEAQ 16(SP), BP
;; ...omitted FUNCDATA stuff...
0x001d MOVQ $137438953482, AX
0x0027 MOVQ AX, (SP)
;; ...omitted PCDATA stuff...
0x002b CALL "".add(SB)
0x0030 MOVQ 16(SP), BP
0x0035 ADDQ $24, SP
0x0039 RET
;; ...omitted stack-split epilogue...
```
```Assembly
0x0000 TEXT "".main(SB), $24-0
```
Nothing new here:
- `"".main` (`main.main` once linked) is a global function symbol in the `.text` section, whose address is some constant offset from the beginning of our address-space.
- It allocates a 24 bytes stack-frame and doesn't receive any argument nor does it return any value.
```Assembly
0x000f SUBQ $24, SP
0x0013 MOVQ BP, 16(SP)
0x0018 LEAQ 16(SP), BP
```
As we mentioned above, the Go calling convention mandates that every argument must be passed on the stack.
Our caller, `main`, grows its stack-frame by 24 bytes (*remember that the stack grows downwards, so `SUBQ` here actually makes the stack-frame bigger*) by decrementing the virtual stack-pointer.
Of those 24 bytes:
- 8 bytes (`16(SP)`-`24(SP)`) are used to store the current value of the frame-pointer `BP` (*the real one!*) to allow for stack-unwinding and facilitate debugging
- 1+3 bytes (`12(SP)`-`16(SP)`) are reserved for the second return value (`bool`) plus 3 bytes of necessary alignment on `amd64`
- 4 bytes (`8(SP)`-`12(SP)`) are reserved for the first return value (`int32`)
- 4 bytes (`4(SP)`-`8(SP)`) are reserved for the value of argument `b (int32)`
- 4 bytes (`0(SP)`-`4(SP)`) are reserved for the value of argument `a (int32)`
Finally, following the growth of the stack, `LEAQ` computes the new address of the frame-pointer and stores it in `BP`.
```Assembly
0x001d MOVQ $137438953482, AX
0x0027 MOVQ AX, (SP)
```
The caller pushes the arguments for the callee as a **Q**uad word (i.e. an 8-byte value) at the top of the stack that it has just grown.
Although it might look like random garbage at first, `137438953482` actually corresponds to the `10` and `32` 4-byte values concatenated into one 8-byte value:
```
$ echo 'obase=2;137438953482' | bc
10000000000000000000000000000000001010
\____/\______________________________/
32 10
```
```Assembly
0x002b CALL "".add(SB)
```
We `CALL` our `add` function as an offset relative to the static-base pointer: i.e. this is a straightforward jump to a direct address.
Note that `CALL` also pushes the return-address (8-byte value) at the top of the stack; so every references to `SP` made from within our `add` function end up being offsetted by 8 bytes!
E.g. `"".a` is not at `0(SP)` anymore, but at `8(SP)`.
```Assembly
0x0030 MOVQ 16(SP), BP
0x0035 ADDQ $24, SP
0x0039 RET
```
Finally, we:
1. Unwind the frame-pointer by one stack-frame (i.e. we "go down" one level)
2. Shrink the stack by 24 bytes to reclaim the stack space we had previously allocated
3. Ask the Go assembler to insert subroutine-return related stuff
## A word about goroutines, stacks and splits
Now is not the time nor place to delve into goroutines' internals (*..that comes later*), but as we start looking at assembly dumps more and more, instructions related to stack management will rapidly become a very familiar sight.
We should be able to quickly recognize these patterns, and, while we're at it, understand the general idea of what they do and why do they do it.
### Stacks
Since the number of goroutines in a Go program is non-deterministic, and can go up to several millions in practice, the runtime must take the conservative route when allocating stack space for goroutines to avoid eating up all of the available memory.
As such, every new goroutine is given an initial tiny 2kB stack by the runtime (said stack is actually allocated on the heap behind the scenes).
As a goroutine runs along doing its job, it might end up outgrowing its contrived, initial stack-space (i.e. stack-overflow).
To prevent this from happening, the runtime makes sure that when a goroutine is running out of stack, a new, bigger stack with two times the size of the old one gets allocated, and that the content of the original stack gets copied over to the new one.
This process is known as a *stack-split* and effectively makes goroutine stacks dynamically-sized.
### Splits
For stack-splitting to work, the compiler inserts a few instructions at the beginning and end of every function that could potentially overflow its stack.
As we've seen earlier in this chapter, and to avoid unnecessary overhead, functions that cannot possibly outgrow their stack are marked as `NOSPLIT` as a hint for the compiler not to insert these checks.
Let's look at our main function from earlier, this time without omitting the stack-split preamble:
```Assembly
0x0000 TEXT "".main(SB), $24-0
;; stack-split prologue
0x0000 MOVQ (TLS), CX
0x0009 CMPQ SP, 16(CX)
0x000d JLS 58
0x000f SUBQ $24, SP
0x0013 MOVQ BP, 16(SP)
0x0018 LEAQ 16(SP), BP
;; ...omitted FUNCDATA stuff...
0x001d MOVQ $137438953482, AX
0x0027 MOVQ AX, (SP)
;; ...omitted PCDATA stuff...
0x002b CALL "".add(SB)
0x0030 MOVQ 16(SP), BP
0x0035 ADDQ $24, SP
0x0039 RET
;; stack-split epilogue
0x003a NOP
;; ...omitted PCDATA stuff...
0x003a CALL runtime.morestack_noctxt(SB)
0x003f JMP 0
```
As you can see, the stack-split preamble is divided into a prologue and an epilogue:
- The prologue checks whether the goroutine is running out of space and, if it's the case, jumps to the epilogue.
- The epilogue, on the other hand, triggers the stack-growth machinery and then jumps back to the prologue.
This creates a feedback loop that goes on for as long as a large enough stack hasn't been allocated for our starved goroutine.
**Prologue**
```Assembly
0x0000 MOVQ (TLS), CX ;; store current *g in CX
0x0009 CMPQ SP, 16(CX) ;; compare SP and g.stackguard0
0x000d JLS 58 ;; jumps to 0x3a if SP <= g.stackguard0
```
`TLS` is a virtual register maintained by the runtime that holds a pointer to the current `g`, i.e. the data-structure that keeps track of all the state of a goroutine.
Looking at the definition of `g` from the source code of the runtime:
```Go
type g struct {
stack stack // 16 bytes
// 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.
stackguard0 uintptr
stackguard1 uintptr
// ...omitted dozens of fields...
}
```
We can see that `16(CX)` corresponds to `g.stackguard0`, which is the threshold value maintained by the runtime that, when compared to the stack-pointer, indicates whether or not a goroutine is about to run out of space.
The prologue thus checks if the current `SP` value is less than or equal to the `stackguard0` threshold (that is, it's bigger), then jumps to the epilogue if it happens to be the case.
**Epilogue**
```Assembly
0x003a NOP
0x003a CALL runtime.morestack_noctxt(SB)
0x003f JMP 0
```
The body of the epilogue is pretty straightforward: it calls into the runtime, which will do the actual work of growing the stack, then jumps back to the first instruction of the function (i.e. to the prologue).
The `NOP` instruction just before the `CALL` exists so that the prologue doesn't jump directly onto a `CALL` instruction. On some platforms, doing so can lead to very dark places; it's a common pratice to set-up a noop instruction right before the actual call and land on this `NOP` instead.
*[UPDATE: We've discussed about this matter in [issue #4: Clarify "nop before call" paragraph](https://github.com/teh-cmc/go-internals/issues/4).]*
### Minus some subtleties
We've merely covered the tip of the iceberg here.
The inner mechanics of stack-growth have many more subtleties that we haven't even mentioned here: the whole process is quite a complex machinery overall, and will require a chapter of its own.
We'll come back to these matters in time.
## Conclusion
This quick introduction to Go's assembler should give you enough material to start toying around.
As we dig deeper and deeper into Go's internals for the rest of this book, Go assembly will be one of our most relied-on tool to understand what goes on behind the scenes and connect the, at first sight, not-always-so-obvious dots.
If you have any questions or suggestions, don't hesitate to open an issue with the `chapter1:` prefix!
## Links
- [[Official] A Quick Guide to Go's Assembler](https://golang.org/doc/asm)
- [[Official] Go Compiler Directives](https://golang.org/cmd/compile/#hdr-Compiler_Directives)
- [[Official] The design of the Go Assembler](https://www.youtube.com/watch?v=KINIAgRpkDA)
- [[Official] Contiguous stacks Design Document](https://docs.google.com/document/d/1wAaf1rYoM4S4gtnPh0zOlGzWtrZFQ5suE8qr2sD8uWQ/pub)
- [[Official] The `_StackMin` constant](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/stack.go#L70-L71)
- [[Discussion] Issue #2: *Frame pointer*](https://github.com/teh-cmc/go-internals/issues/2)
- [[Discussion] Issue #4: *Clarify "nop before call" paragraph*](https://github.com/teh-cmc/go-internals/issues/4)
- [A Foray Into Go Assembly Programming](https://blog.sgmansfield.com/2017/04/a-foray-into-go-assembly-programming/)
- [Dropping Down Go Functions in Assembly](https://www.youtube.com/watch?v=9jpnFmJr2PE)
- [What is the purpose of the EBP frame pointer register?](https://stackoverflow.com/questions/579262/what-is-the-purpose-of-the-ebp-frame-pointer-register)
- [Stack frame layout on x86-64](https://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64)
- [How Stacks are Handled in Go](https://blog.cloudflare.com/how-stacks-are-handled-in-go/)
- [Why stack grows down](https://gist.github.com/cpq/8598782)
================================================
FILE: chapter1_assembly_primer/direct_topfunc_call.go
================================================
package main
//go:noinline
func add(a, b int32) (int32, bool) { return a + b, true }
func main() { add(10, 32) }
================================================
FILE: chapter2_interfaces/Makefile
================================================
GOOS=linux
GOARCH=amd64
SOURCES := $(wildcard *.go)
OBJECTS = $(SOURCES:.go=.o)
EXECUTABLES = $(OBJECTS:.o=.bin)
.SECONDARY: ${OBJECTS}
all: ${EXECUTABLES}
%.o: %.go
GOOS=${GOOS} GOARCH=${GOARCH} go tool compile $<
%.bin: %.o
GOOS=${GOOS} GOARCH=${GOARCH} go tool link -o $@ $<
clean:
rm -f ${OBJECTS}
rm -f ${EXECUTABLES}
================================================
FILE: chapter2_interfaces/README.md
================================================
<!-- Copyright © 2018 Clement Rey <cr.rey.clement@gmail.com>. -->
<!-- Licensed under the BY-NC-SA Creative Commons 4.0 International Public License. -->
```Bash
$ go version
go version go1.10 linux/amd64
```
# Chapter II: Interfaces
This chapter covers the inner workings of Go's interfaces.
Specifically, we'll look at:
- How functions & methods are called at run time.
- How interfaces are built and what they're made of.
- How, when and at what cost does dynamic dispatch work.
- How the empty interface & other special cases differ from their peers.
- How interface composition works.
- How and at what cost do type assertions work.
As we dig deeper and deeper, we'll also poke at miscellaneous low-level concerns, such as some implementation details of modern CPUs as well as various optimizations techniques used by the Go compiler.
---
**Table of Contents**
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
- [Function and method calls](#function-and-method-calls)
- [Overview of direct calls](#overview-of-direct-calls)
- [Implicit dereferencing](#implicit-dereferencing)
- [Anatomy of an interface](#anatomy-of-an-interface)
- [Overview of the datastructures](#overview-of-the-datastructures)
- [Creating an interface](#creating-an-interface)
- [Reconstructing an `itab` from an executable](#reconstructing-an-itab-from-an-executable)
- [Dynamic dispatch](#dynamic-dispatch)
- [Indirect method call on interface](#indirect-method-call-on-interface)
- [Overhead](#overhead)
- [The theory: quick refresher on modern CPUs](#the-theory-quick-refresher-on-modern-cpus)
- [The practice: benchmarks](#the-practice-benchmarks)
- [Special cases & compiler tricks](#special-cases--compiler-tricks)
- [The empty interface](#the-empty-interface)
- [Interface holding a scalar type](#interface-holding-a-scalar-type)
- [A word about zero-values](#a-word-about-zero-values)
- [A tangent about zero-size variables](#a-tangent-about-zero-size-variables)
- [Interface composition](#interface-composition)
- [Assertions](#assertions)
- [Type assertions](#type-assertions)
- [Type-switches](#type-switches)
- [Conclusion](#conclusion)
- [Links](#links)
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
---
- *This chapter assumes you're familiar with Go's assembler ([chapter I](../chapter1_assembly_primer/README.md)).*
- *If and when running into architecture-specific matters, always assume `linux/amd64`.*
- *We will always work with compiler optimizations **enabled**.*
- *Quoted text and/or comments always come from the official documentation (including Russ Cox "Function Calls" design document) and/or codebase, unless stated otherwise.*
## Function and method calls
As pointed out by Russ Cox in his design document about function calls (listed at the end of this chapter), Go has..:
..4 different kinds of functions..:
> - top-level func
> - method with value receiver
> - method with pointer receiver
> - func literal
..and 5 different kinds of calls:
> - direct call of top-level func (`func TopLevel(x int) {}`)
> - direct call of method with value receiver (`func (Value) M(int) {}`)
> - direct call of method with pointer receiver (`func (*Pointer) M(int) {}`)
> - indirect call of method on interface (`type Interface interface { M(int) }`)
> - indirect call of func value (`var literal = func(x int) {}`)
Mixed together, these make up for 10 possible combinations of function and call types:
> - direct call of top-level func /
> - direct call of method with value receiver /
> - direct call of method with pointer receiver /
> - indirect call of method on interface / containing value with value method
> - indirect call of method on interface / containing pointer with value method
> - indirect call of method on interface / containing pointer with pointer method
> - indirect call of func value / set to top-level func
> - indirect call of func value / set to value method
> - indirect call of func value / set to pointer method
> - indirect call of func value / set to func literal
>
> (A slash separates what is known at compile time from what is only found out at run time.)
We'll first take a few minutes to review the three kinds of direct calls, then we'll shift our focus towards interfaces and indirect method calls for the rest of this chapter.
We won't cover function literals in this chapter, as doing so would first require us to become familiar with the mechanics of closures.. which we'll inevitably do, in due time.
### Overview of direct calls
Consider the following code ([direct_calls.go](./direct_calls.go)):
```Go
//go:noinline
func Add(a, b int32) int32 { return a + b }
type Adder struct{ id int32 }
//go:noinline
func (adder *Adder) AddPtr(a, b int32) int32 { return a + b }
//go:noinline
func (adder Adder) AddVal(a, b int32) int32 { return a + b }
func main() {
Add(10, 32) // direct call of top-level function
adder := Adder{id: 6754}
adder.AddPtr(10, 32) // direct call of method with pointer receiver
adder.AddVal(10, 32) // direct call of method with value receiver
(&adder).AddVal(10, 32) // implicit dereferencing
}
```
Let's have a quick look at the code generated for each of those 4 calls.
**Direct call of a top-level function**
Looking at the assembly output for `Add(10, 32)`:
```Assembly
0x0000 TEXT "".main(SB), $40-0
;; ...omitted everything but the actual function call...
0x0021 MOVQ $137438953482, AX
0x002b MOVQ AX, (SP)
0x002f CALL "".Add(SB)
;; ...omitted everything but the actual function call...
```
We see that, as we already knew from chapter I, this translates into a direct jump to a global function symbol in the `.text` section, with the arguments and return values stored on the caller's stack-frame.
It's as straightforward as it gets.
Russ Cox wraps it up as such in his document:
> Direct call of top-level func:
> A direct call of a top-level func passes all arguments on the stack, expecting results to occupy the successive stack positions.
**Direct call of a method with pointer receiver**
First things first, the receiver is initialized via `adder := Adder{id: 6754}`:
```Assembly
0x0034 MOVL $6754, "".adder+28(SP)
```
*(The extra-space on our stack-frame was pre-allocated as part of the frame-pointer preamble, which we haven't shown here for conciseness.)*
Then comes the actual method call to `adder.AddPtr(10, 32)`:
```Assembly
0x0057 LEAQ "".adder+28(SP), AX ;; move &adder to..
0x005c MOVQ AX, (SP) ;; ..the top of the stack (argument #1)
0x0060 MOVQ $137438953482, AX ;; move (32,10) to..
0x006a MOVQ AX, 8(SP) ;; ..the top of the stack (arguments #3 & #2)
0x006f CALL "".(*Adder).AddPtr(SB)
```
Looking at the assembly output, we can clearly see that a call to a method (whether it has a value or pointer receiver) is almost identical to a function call, the only difference being that the receiver is passed as first argument.
In this case, we do so by loading the effective address (`LEAQ`) of `"".adder+28(SP)` at the top of the frame, so that argument #1 becomes `&adder` (if you're a bit confused regarding the semantics of `LEA` vs. `MOV`, you may want to have a look at the links at the end of this chapter for some pointers).
Note how the compiler encodes the type of the receiver and whether it's a value or pointer directly into the name of the symbol: `"".(*Adder).AddPtr`.
> Direct call of method:
> In order to use the same generated code for both an indirect call of a func value and for a direct call, the code generated for a method (both value and pointer receivers) is chosen to have the same calling convention as a top-level function with the receiver as a leading argument.
**Direct call of a method with value receiver**
As we'd expect, using a value receiver yields very similar code as above.
Consider `adder.AddVal(10, 32)`:
```Assembly
0x003c MOVQ $42949679714, AX ;; move (10,6754) to..
0x0046 MOVQ AX, (SP) ;; ..the top of the stack (arguments #2 & #1)
0x004a MOVL $32, 8(SP) ;; move 32 to the top of the stack (argument #3)
0x0052 CALL "".Adder.AddVal(SB)
```
Looks like something a bit trickier is going on here, though: the generated assembly isn't even referencing `"".adder+28(SP)` anywhere, even though that is where our receiver currently resides.
So what's really going on here? Well, since the receiver is a value, and since the compiler is able to statically infer that value, it doesn't bother with copying the existing value from its current location (`28(SP)`): instead, it simply creates a new, identical `Adder` value directly on the stack, and merges this operation with the creation of the second argument to save one more instruction in the process.
Once again, notice how the symbol name of the method explicitly denotes that it expects a value receiver.
### Implicit dereferencing
There's one final call that we haven't looked at yet: `(&adder).AddVal(10, 32)`.
In that case, we're using a pointer variable to call a method that instead expects a value receiver. Somehow, Go automagically dereferences our pointer and manages to make the call. How so?
How the compiler handles this kind of situation depends on whether or not the receiver being pointed to has escaped to the heap or not.
**Case A: The receiver is on the stack**
If the receiver is still on the stack and its size is sufficiently small that it can be copied in a few instructions, as is the case here, the compiler simply copies its value over to the top of the stack then does a straightforward method call to `"".Adder.AddVal` (i.e. the one with a value receiver).
`(&adder).AddVal(10, 32)` thus looks like this in this situation:
```Assembly
0x0074 MOVL "".adder+28(SP), AX ;; move (i.e. copy) adder (note the MOV instead of a LEA) to..
0x0078 MOVL AX, (SP) ;; ..the top of the stack (argument #1)
0x007b MOVQ $137438953482, AX ;; move (32,10) to..
0x0085 MOVQ AX, 4(SP) ;; ..the top of the stack (arguments #3 & #2)
0x008a CALL "".Adder.AddVal(SB)
```
Boring (although efficient). Let's move on to case B.
**Case B: The receiver is on the heap**
If the receiver has escaped to the heap then the compiler has to take a cleverer route: it generates a new method (with a pointer receiver, this time) that wraps `"".Adder.AddVal`, and replaces the original call to `"".Adder.AddVal` (the wrappee) with a call to `"".(*Adder).AddVal` (the wrapper).
The wrapper's sole mission, then, is to make sure that the receiver gets properly dereferenced before being passed to the wrappee, and that any arguments and return values involved are properly copied back and forth between the caller and the wrappee.
(*NOTE: In assembly outputs, these wrapper methods are marked as `<autogenerated>`.*)
Here's an annotated listing of the generated wrapper that should hopefully clear things up a bit:
```Assembly
0x0000 TEXT "".(*Adder).AddVal(SB), DUPOK|WRAPPER, $32-24
;; ...omitted preambles...
0x0026 MOVQ ""..this+40(SP), AX ;; check whether the receiver..
0x002b TESTQ AX, AX ;; ..is nil
0x002e JEQ 92 ;; if it is, jump to 0x005c (panic)
0x0030 MOVL (AX), AX ;; dereference pointer receiver..
0x0032 MOVL AX, (SP) ;; ..and move (i.e. copy) the resulting value to argument #1
;; forward (copy) arguments #2 & #3 then call the wrappee
0x0035 MOVL "".a+48(SP), AX
0x0039 MOVL AX, 4(SP)
0x003d MOVL "".b+52(SP), AX
0x0041 MOVL AX, 8(SP)
0x0045 CALL "".Adder.AddVal(SB) ;; call the wrapped method
;; copy return value from wrapped method then return
0x004a MOVL 16(SP), AX
0x004e MOVL AX, "".~r2+56(SP)
;; ...omitted frame-pointer stuff...
0x005b RET
;; throw a panic with a detailed error
0x005c CALL runtime.panicwrap(SB)
;; ...omitted epilogues...
```
Obviously, this kind of wrapper can induce quite a bit of overhead considering all the copying that needs to be done in order to pass the arguments back and forth; especially if the wrappee is just a few instructions.
Fortunately, in practice, the compiler would have inlined the wrappee directly into the wrapper to amortize these costs (when feasible, at least).
Note the `WRAPPER` directive in the definition of the symbol, which indicates that this method shouldn't appear in backtraces (so as not to confuse the end-user), nor should it be able to recover from panics that might be thrown by the wrappee.
> WRAPPER: This is a wrapper function and should not count as disabling recover.
The `runtime.panicwrap` function, which throws a panic if the wrapper's receiver is `nil`, is pretty self-explanatory; here's its complete listing for reference ([src/runtime/error.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/error.go#L132-L157)):
```Go
// panicwrap generates a panic for a call to a wrapped value method
// with a nil pointer receiver.
//
// It is called from the generated wrapper code.
func panicwrap() {
pc := getcallerpc()
name := funcname(findfunc(pc))
// name is something like "main.(*T).F".
// We want to extract pkg ("main"), typ ("T"), and meth ("F").
// Do it by finding the parens.
i := stringsIndexByte(name, '(')
if i < 0 {
throw("panicwrap: no ( in " + name)
}
pkg := name[:i-1]
if i+2 >= len(name) || name[i-1:i+2] != ".(*" {
throw("panicwrap: unexpected string after package name: " + name)
}
name = name[i+2:]
i = stringsIndexByte(name, ')')
if i < 0 {
throw("panicwrap: no ) in " + name)
}
if i+2 >= len(name) || name[i:i+2] != ")." {
throw("panicwrap: unexpected string after type name: " + name)
}
typ := name[:i]
meth := name[i+2:]
panic(plainError("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer"))
}
```
That's all for function and method calls, we'll now focus on the main course: interfaces.
## Anatomy of an interface
### Overview of the datastructures
Before we can understand how they work, we first need to build a mental model of the datastructures that make up interfaces and how they're laid out in memory.
To that end, we'll have a quick peek into the runtime package to see what an interface actually looks like from the standpoint of the Go implementation.
**The `iface` structure**
`iface` is the root type that represents an interface within the runtime ([src/runtime/runtime2.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/runtime2.go#L143-L146)).
Its definition goes like this:
```Go
type iface struct { // 16 bytes on a 64bit arch
tab *itab
data unsafe.Pointer
}
```
An interface is thus a very simple structure that maintains 2 pointers:
- `tab` holds the address of an `itab` object, which embeds the datastructures that describe both the type of the interface as well as the type of the data it points to.
- `data` is a raw (i.e. `unsafe`) pointer to the value held by the interface.
While extremely simple, this definition already gives us some valuable information: since interfaces can only hold pointers, *any concrete value that we wrap into an interface will have to have its address taken*.
More often than not, this will result in a heap allocation as the compiler takes the conservative route and forces the receiver to escape.
This holds true even for scalar types!
We can prove that with a few lines of code ([escape.go](./escape.go)):
```Go
type Addifier interface{ Add(a, b int32) int32 }
type Adder struct{ name string }
//go:noinline
func (adder Adder) Add(a, b int32) int32 { return a + b }
func main() {
adder := Adder{name: "myAdder"}
adder.Add(10, 32) // doesn't escape
Addifier(adder).Add(10, 32) // escapes
}
```
```Bash
$ GOOS=linux GOARCH=amd64 go tool compile -m escape.go
escape.go:13:10: Addifier(adder) escapes to heap
# ...
```
One could even visualize the resulting heap allocation using a simple benchmark ([escape_test.go](./escape_test.go)):
```Go
func BenchmarkDirect(b *testing.B) {
adder := Adder{id: 6754}
for i := 0; i < b.N; i++ {
adder.Add(10, 32)
}
}
func BenchmarkInterface(b *testing.B) {
adder := Adder{id: 6754}
for i := 0; i < b.N; i++ {
Addifier(adder).Add(10, 32)
}
}
```
```Bash
$ GOOS=linux GOARCH=amd64 go tool compile -m escape_test.go
# ...
escape_test.go:22:11: Addifier(adder) escapes to heap
# ...
```
```Bash
$ GOOS=linux GOARCH=amd64 go test -bench=. -benchmem ./escape_test.go
BenchmarkDirect-8 2000000000 1.60 ns/op 0 B/op 0 allocs/op
BenchmarkInterface-8 100000000 15.0 ns/op 4 B/op 1 allocs/op
```
We can clearly see how each time we create a new `Addifier` interface and initialize it with our `adder` variable, a heap allocation of `sizeof(Adder)` actually takes place.
Later in this chapter, we'll see how even simple scalar types can lead to heap allocations when used with interfaces.
Let's turn our attention towards the next datastructure: `itab`.
**The `itab` structure**
`itab` is defined thusly ([src/runtime/runtime2.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/runtime2.go#L648-L658)):
```Go
type itab struct { // 40 bytes on a 64bit arch
inter *interfacetype
_type *_type
hash uint32 // copy of _type.hash. Used for type switches.
_ [4]byte
fun [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
}
```
An `itab` is the heart & brain of an interface.
First, it embeds a `_type`, which is the internal representation of any Go type within the runtime.
A `_type` describes every facets of a type: its name, its characteristics (e.g. size, alignment...), and to some extent, even how it behaves (e.g. comparison, hashing...)!
In this instance, the `_type` field describes the type of the value held by the interface, i.e. the value that the `data` pointer points to.
Second, we find a pointer to an `interfacetype`, which is merely a wrapper around `_type` with some extra information that are specific to interfaces.
As you'd expect, the `inter` field describes the type of the interface itself.
Finally, the `fun` array holds the function pointers that make up the virtual/dispatch table of the interface.
Notice the comment that says `// variable sized`, meaning that the size with which this array is declared is *irrelevant*.
We'll see later in this chapter that the compiler is responsible for allocating the memory that backs this array, and does so independently of the size indicated here. Likewise, the runtime always accesses this array using raw pointers, thus bounds-checking does not apply here.
**The `_type` structure**
As we said above, the `_type` structure gives a complete description of a Go type.
It's defined as such ([src/runtime/type.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/type.go#L25-L43)):
```Go
type _type struct { // 48 bytes on a 64bit arch
size uintptr
ptrdata uintptr // size of memory prefix holding all pointers
hash uint32
tflag tflag
align uint8
fieldalign uint8
kind uint8
alg *typeAlg
// gcdata stores the GC type data for the garbage collector.
// If the KindGCProg bit is set in kind, gcdata is a GC program.
// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
gcdata *byte
str nameOff
ptrToThis typeOff
}
```
Thankfully, most of these fields are quite self-explanatory.
The `nameOff` & `typeOff` types are `int32` offsets into the metadata embedded into the final executable by the linker. This metadata is loaded into `runtime.moduledata` structures at run time ([src/runtime/symtab.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/symtab.go#L352-L393)), which should look fairly similar if you've ever had to look at the content of an ELF file.
The runtime provide helpers that implement the necessary logic for following these offsets through the `moduledata` structures, such as e.g. `resolveNameOff` ([src/runtime/type.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/type.go#L168-L196)) and `resolveTypeOff` ([src/runtime/type.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/type.go#L202-L236)):
```Go
func resolveNameOff(ptrInModule unsafe.Pointer, off nameOff) name {}
func resolveTypeOff(ptrInModule unsafe.Pointer, off typeOff) *_type {}
```
I.e., assuming `t` is a `_type`, calling `resolveTypeOff(t, t.ptrToThis)` returns a copy of `t`.
**The `interfacetype` structure**
Finally, here's the `interfacetype` structure ([src/runtime/type.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/type.go#L342-L346)):
```Go
type interfacetype struct { // 80 bytes on a 64bit arch
typ _type
pkgpath name
mhdr []imethod
}
type imethod struct {
name nameOff
ityp typeOff
}
```
As mentioned, an `interfacetype` is just a wrapper around a `_type` with some extra interface-specific metadata added on top.
In the current implementation, this metadata is mostly composed of a list of offsets that points to the respective names and types of the methods exposed by the interface (`[]imethod`).
**Conclusion**
Here's an overview of what an `iface` looks like when represented with all of its sub-types inlined; this hopefully should help connect all the dots:
```Go
type iface struct { // `iface`
tab *struct { // `itab`
inter *struct { // `interfacetype`
typ struct { // `_type`
size uintptr
ptrdata uintptr
hash uint32
tflag tflag
align uint8
fieldalign uint8
kind uint8
alg *typeAlg
gcdata *byte
str nameOff
ptrToThis typeOff
}
pkgpath name
mhdr []struct { // `imethod`
name nameOff
ityp typeOff
}
}
_type *struct { // `_type`
size uintptr
ptrdata uintptr
hash uint32
tflag tflag
align uint8
fieldalign uint8
kind uint8
alg *typeAlg
gcdata *byte
str nameOff
ptrToThis typeOff
}
hash uint32
_ [4]byte
fun [1]uintptr
}
data unsafe.Pointer
}
```
This section glossed over the different data-types that make up an interface to help us to start building a mental model of the various cogs involved in the overall machinery, and how they all work with each other.
In the next section, we'll learn how these datastructures actually get computed.
### Creating an interface
Now that we've had a quick look at all the datastructures involved, we'll focus on how they actually get allocated and initiliazed.
Consider the following program ([iface.go](./iface.go)):
```Go
type Mather interface {
Add(a, b int32) int32
Sub(a, b int64) int64
}
type Adder struct{ id int32 }
//go:noinline
func (adder Adder) Add(a, b int32) int32 { return a + b }
//go:noinline
func (adder Adder) Sub(a, b int64) int64 { return a - b }
func main() {
m := Mather(Adder{id: 6754})
// This call just makes sure that the interface is actually used.
// Without this call, the linker would see that the interface defined above
// is in fact never used, and thus would optimize it out of the final
// executable.
m.Add(10, 32)
}
```
*NOTE: For the remainder of this chapter, we will denote an interface `I` that holds a type `T` as `<I,T>`. E.g. `Mather(Adder{id: 6754})` instantiates an `iface<Mather, Adder>`.*
Let's zoom in on the instantiation of `iface<Mather, Adder>`:
```Go
m := Mather(Adder{id: 6754})
```
This single line of Go code actually sets off quite a bit of machinery, as the assembly listing generated by the compiler can attest:
```Assembly
;; part 1: allocate the receiver
0x001d MOVL $6754, ""..autotmp_1+36(SP)
;; part 2: set up the itab
0x0025 LEAQ go.itab."".Adder,"".Mather(SB), AX
0x002c MOVQ AX, (SP)
;; part 3: set up the data
0x0030 LEAQ ""..autotmp_1+36(SP), AX
0x0035 MOVQ AX, 8(SP)
0x003a CALL runtime.convT2I32(SB)
0x003f MOVQ 16(SP), AX
0x0044 MOVQ 24(SP), CX
```
As you can see, we've splitted the output into three logical parts.
**Part 1: Allocate the receiver**
```Assembly
0x001d MOVL $6754, ""..autotmp_1+36(SP)
```
A constant decimal value of `6754`, corresponding to the ID of our `Adder`, is stored at the beginning of the current stack-frame.
It's stored there so that the compiler will later be able to reference it by its address; we'll see why in part 3.
**Part 2: Set up the itab**
```Assembly
0x0025 LEAQ go.itab."".Adder,"".Mather(SB), AX
0x002c MOVQ AX, (SP)
```
It looks like the compiler has already created the necessary `itab` for representing our `iface<Mather, Adder>` interface, and made it available to us via a global symbol: `go.itab."".Adder,"".Mather`.
We're in the process of building an `iface<Mather, Adder>` interface and, in order to do so, we're loading the effective address of this global `go.itab."".Adder,"".Mather` symbol at the top of the current stack-frame.
Once again, we'll see why in part 3.
Semantically, this gives us something along the lines of the following pseudo-code:
```Go
tab := getSymAddr(`go.itab.main.Adder,main.Mather`).(*itab)
```
That's half of our interface right there!
Now, while we're at it, let's have a deeper look at that `go.itab."".Adder,"".Mather` symbol.
As usual, the `-S` flag of the compiler can tell us a lot:
```
$ GOOS=linux GOARCH=amd64 go tool compile -S iface.go | grep -A 7 '^go.itab."".Adder,"".Mather'
go.itab."".Adder,"".Mather SRODATA dupok size=40
0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0010 8a 3d 5f 61 00 00 00 00 00 00 00 00 00 00 00 00 .=_a............
0x0020 00 00 00 00 00 00 00 00 ........
rel 0+8 t=1 type."".Mather+0
rel 8+8 t=1 type."".Adder+0
rel 24+8 t=1 "".(*Adder).Add+0
rel 32+8 t=1 "".(*Adder).Sub+0
```
Neat. Let's analyze this piece by piece.
The first piece declares the symbol and its attributes:
```
go.itab."".Adder,"".Mather SRODATA dupok size=40
```
As usual, since we're looking directly at the intermediate object file generated by the compiler (i.e. the linker hasn't run yet), symbol names are still missing package names. Nothing new on that front.
Other than that, what we've got here is a 40-byte global object symbol that will be stored in the `.rodata` section of our binary.
Note the `dupok` directive, which tells the linker that it is legal for this symbol to appear multiple times at link-time: the linker will have to arbitrarily choose one of them over the others.
What makes the Go authors think that this symbol might end up duplicated, I'm not sure. Feel free to file an issue if you know more.
*[UPDATE: We've discussed about this matter in [issue #7: How you can get duplicated go.itab interface definitions](https://github.com/teh-cmc/go-internals/issues/7).]*
The second piece is a hexdump of the 40 bytes of data associated with the symbol. I.e., it's a serialized representation of an `itab` structure:
```
0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0010 8a 3d 5f 61 00 00 00 00 00 00 00 00 00 00 00 00 .=_a............
0x0020 00 00 00 00 00 00 00 00 ........
```
As you can see, most of this data is just a bunch of zeros at this point. The linker will take care of filling them up, as we'll see in a minute.
Notice how, among all these zeros, 4 bytes actually have been set though, at offset `0x10+4`.
If we take a look back at the declaration of the `itab` structure and annotate the respective offsets of its fields:
```Go
type itab struct { // 40 bytes on a 64bit arch
inter *interfacetype // offset 0x00 ($00)
_type *_type // offset 0x08 ($08)
hash uint32 // offset 0x10 ($16)
_ [4]byte // offset 0x14 ($20)
fun [1]uintptr // offset 0x18 ($24)
// offset 0x20 ($32)
}
```
We see that offset `0x10+4` matches the `hash uint32` field: i.e., the hash value that corresponds to our `main.Adder` type is already right there in our object file.
The third and final piece lists a bunch of relocation directives for the linker:
```
rel 0+8 t=1 type."".Mather+0
rel 8+8 t=1 type."".Adder+0
rel 24+8 t=1 "".(*Adder).Add+0
rel 32+8 t=1 "".(*Adder).Sub+0
```
`rel 0+8 t=1 type."".Mather+0` tells the linker to fill up the first 8 bytes (`0+8`) of the contents with the address of the global object symbol `type."".Mather`.
`rel 8+8 t=1 type."".Adder+0` then fills the next 8 bytes with the address of `type."".Adder`, and so on and so forth.
Once the linker has done its job and followed all of these directives, our 40-byte serialized `itab` will be complete.
Overall, we're now looking at something akin to the following pseudo-code:
```Go
tab := getSymAddr(`go.itab.main.Adder,main.Mather`).(*itab)
// NOTE: The linker strips the `type.` prefix from these symbols when building
// the executable, so the final symbol names in the .rodata section of the
// binary will actually be `main.Mather` and `main.Adder` rather than
// `type.main.Mather` and `type.main.Adder`.
// Don't get tripped up by this when toying around with objdump.
tab.inter = getSymAddr(`type.main.Mather`).(*interfacetype)
tab._type = getSymAddr(`type.main.Adder`).(*_type)
tab.fun[0] = getSymAddr(`main.(*Adder).Add`).(uintptr)
tab.fun[1] = getSymAddr(`main.(*Adder).Sub`).(uintptr)
```
We've got ourselves a ready-to-use `itab`, now if we just had some data to along with it, that'd make for a nice, complete interface.
**Part 3: Set up the data**
```Assembly
0x0030 LEAQ ""..autotmp_1+36(SP), AX
0x0035 MOVQ AX, 8(SP)
0x003a CALL runtime.convT2I32(SB)
0x003f MOVQ 16(SP), AX
0x0044 MOVQ 24(SP), CX
```
Remember from part 1 that the top of the stack `(SP)` currently holds the address of `go.itab."".Adder,"".Mather` (argument #1).
Also remember from part 2 that we had stored a `$6754` decimal constant in `""..autotmp_1+36(SP)`: we now load the effective address of this constant just below the top of the stack-frame, at 8(SP) (argument #2).
These two pointers are the two arguments that we pass into `runtime.convT2I32`, which will apply the final touches of glue to create and return our complete interface.
Let's have a closer look at it ([src/runtime/iface.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/iface.go#L433-L451)):
```Go
func convT2I32(tab *itab, elem unsafe.Pointer) (i iface) {
t := tab._type
/* ...omitted debug stuff... */
var x unsafe.Pointer
if *(*uint32)(elem) == 0 {
x = unsafe.Pointer(&zeroVal[0])
} else {
x = mallocgc(4, t, false)
*(*uint32)(x) = *(*uint32)(elem)
}
i.tab = tab
i.data = x
return
}
```
So `runtime.convT2I32` does 4 things:
1. It creates a new `iface` structure `i` (to be pedantic, its caller creates it.. same difference).
2. It assigns the `itab` pointer we just gave it to `i.tab`.
3. It **allocates a new object of type `i.tab._type` on the heap**, then copy the value pointed to by the second argument `elem` into that new object.
4. It returns the final interface.
This process is quite straightforward overall, although the 3rd step does involve some tricky implementation details in this specific case, which are caused by the fact that our `Adder` type is effectively a scalar type.
We'll look at the interactions of scalar types and interfaces in more details in the section about [the special cases of interfaces](#interface-holding-a-scalar-type).
Conceptually, we've now accomplished the following (pseudo-code):
```Go
tab := getSymAddr(`go.itab.main.Adder,main.Mather`).(*itab)
elem := getSymAddr(`""..autotmp_1+36(SP)`).(*int32)
i := runtime.convTI32(tab, unsafe.Pointer(elem))
assert(i.tab == tab)
assert(*(*int32)(i.data) == 6754) // same value..
assert((*int32)(i.data) != elem) // ..but different (al)locations!
```
To summarize all that just went down, here's a complete, annotated version of the assembly code for all 3 parts:
```Assembly
0x001d MOVL $6754, ""..autotmp_1+36(SP) ;; create an addressable $6754 value at 36(SP)
0x0025 LEAQ go.itab."".Adder,"".Mather(SB), AX ;; set up go.itab."".Adder,"".Mather..
0x002c MOVQ AX, (SP) ;; ..as first argument (tab *itab)
0x0030 LEAQ ""..autotmp_1+36(SP), AX ;; set up &36(SP)..
0x0035 MOVQ AX, 8(SP) ;; ..as second argument (elem unsafe.Pointer)
0x003a CALL runtime.convT2I32(SB) ;; call convT2I32(go.itab."".Adder,"".Mather, &$6754)
0x003f MOVQ 16(SP), AX ;; AX now holds i.tab (go.itab."".Adder,"".Mather)
0x0044 MOVQ 24(SP), CX ;; CX now holds i.data (&$6754, somewhere on the heap)
```
Keep in mind that all of this started with just one single line: `m := Mather(Adder{id: 6754})`.
We finally got ourselves a complete, working interface.
### Reconstructing an `itab` from an executable
In the previous section, we dumped the contents of `go.itab."".Adder,"".Mather` directly from the object files generated by the compiler and ended up looking at what was mostly a blob of zeros (except for the `hash` value):
```
$ GOOS=linux GOARCH=amd64 go tool compile -S iface.go | grep -A 3 '^go.itab."".Adder,"".Mather'
go.itab."".Adder,"".Mather SRODATA dupok size=40
0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0010 8a 3d 5f 61 00 00 00 00 00 00 00 00 00 00 00 00 .=_a............
0x0020 00 00 00 00 00 00 00 00 ........
```
To get a better picture of how the data is laid out into the final executable produced by the linker, we'll walk through the generated ELF file and manually reconstruct the bytes that make up the `itab` of our `iface<Mather, Adder>`.
Hopefully, this'll enable us to observe what our `itab` looks like once the linker has done its job.
First things first, let's build the `iface` binary: `GOOS=linux GOARCH=amd64 go build -o iface.bin iface.go`.
**Step 1: Find `.rodata`**
Let's print the section headers in search of `.rodata`, `readelf` can help with that:
```Bash
$ readelf -St -W iface.bin
There are 22 section headers, starting at offset 0x190:
Section Headers:
[Nr] Name
Type Address Off Size ES Lk Inf Al
Flags
[ 0]
NULL 0000000000000000 000000 000000 00 0 0 0
[0000000000000000]:
[ 1] .text
PROGBITS 0000000000401000 001000 04b3cf 00 0 0 16
[0000000000000006]: ALLOC, EXEC
[ 2] .rodata
PROGBITS 000000000044d000 04d000 028ac4 00 0 0 32
[0000000000000002]: ALLOC
## ...omitted rest of output...
```
What we really need here is the (decimal) offset of the section, so let's apply some pipe-foo:
```Bash
$ readelf -St -W iface.bin | \
grep -A 1 .rodata | \
tail -n +2 | \
awk '{print "ibase=16;"toupper($3)}' | \
bc
315392
```
Which means that `fseek`-ing 315392 bytes into our binary should place us right at the start of the `.rodata` section.
Now what we need to do is map this file location to a virtual-memory address.
**Step 2: Find the virtual-memory address (VMA) of `.rodata`**
The VMA is the virtual address at which the section will be mapped once the binary has been loaded in memory by the OS. I.e., this is the address that we'll use to reference a symbol at runtime.
The reason we care about the VMA in this case is that we cannot directly ask `readelf` or `objdump` for the offset of a specific symbol (AFAIK). What we can do, on the other hand, is ask for the VMA of a specific symbol.
Coupled with some simple maths, we should be able to build a mapping between VMAs and offsets and finally find the offsets of the symbols that we're looking for.
Finding the VMA of `.rodata` is no different than finding its offset, it's just a different column is all:
```Bash
$ readelf -St -W iface.bin | \
grep -A 1 .rodata | \
tail -n +2 | \
awk '{print "ibase=16;"toupper($2)}' | \
bc
4509696
```
So here's what we know so far: the `.rodata` section is located at offset `$315392` (= `0x04d000`) into the ELF file, which will be mapped at virtual address `$4509696` (= `0x44d000`) at run time.
Now we need the VMA as well as the size of the symbol we're looking for:
- Its VMA will (indirectly) allow us to locate it within the executable.
- Its size will tell us how much data to extract once we've found the correct offset.
**Step 3: Find the VMA & size of `go.itab.main.Adder,main.Mather`**
`objdump` has those available for us.
First, find the symbol:
```Bash
$ objdump -t -j .rodata iface.bin | grep "go.itab.main.Adder,main.Mather"
0000000000475140 g O .rodata 0000000000000028 go.itab.main.Adder,main.Mather
```
Then, get its VMA in decimal form:
```Bash
$ objdump -t -j .rodata iface.bin | \
grep "go.itab.main.Adder,main.Mather" | \
awk '{print "ibase=16;"toupper($1)}' | \
bc
4673856
```
And finally, get its size in decimal form:
```Bash
$ objdump -t -j .rodata iface.bin | \
grep "go.itab.main.Adder,main.Mather" | \
awk '{print "ibase=16;"toupper($5)}' | \
bc
40
```
So `go.itab.main.Adder,main.Mather` will be mapped at virtual address `$4673856` (= `0x475140`) at run time, and has a size of 40 bytes (which we already knew, as it's the size of an `itab` structure).
**Step 4: Find & extract `go.itab.main.Adder,main.Mather`**
We now have all the elements we need in order to locate `go.itab.main.Adder,main.Mather` within our binary.
Here's a reminder of what we know so far:
```
.rodata offset: 0x04d000 == $315392
.rodata VMA: 0x44d000 == $4509696
go.itab.main.Adder,main.Mather VMA: 0x475140 == $4673856
go.itab.main.Adder,main.Mather size: 0x24 = $40
```
If `$315392` (`.rodata`'s offset) maps to $4509696 (`.rodata`'s VMA) and `go.itab.main.Adder,main.Mather`'s VMA is `$4673856`, then `go.itab.main.Adder,main.Mather`'s offset within the executable is:
`sym.offset = sym.vma - section.vma + section.offset = $4673856 - $4509696 + $315392 = $479552`.
Now that we know both the offset and size of the data, we can take out good ol' `dd` and extract the raw bytes straight out of the executable:
```Bash
$ dd if=iface.bin of=/dev/stdout bs=1 count=40 skip=479552 2>/dev/null | hexdump
0000000 bd20 0045 0000 0000 ed40 0045 0000 0000
0000010 3d8a 615f 0000 0000 c2d0 0044 0000 0000
0000020 c350 0044 0000 0000
0000028
```
This certainly does look like a clear-cut victory.. but is it, really? Maybe we've just dumped 40 totally random, unrelated bytes? Who knows?
There's at least one way to be sure: let's compare the type hash found in our binary dump (at offset `0x10+4` -> `0x615f3d8a`) with the one loaded by the runtime ([iface_type_hash.go](./iface_type_hash.go)):
```Go
// simplified definitions of runtime's iface & itab types
type iface struct {
tab *itab
data unsafe.Pointer
}
type itab struct {
inter uintptr
_type uintptr
hash uint32
_ [4]byte
fun [1]uintptr
}
func main() {
m := Mather(Adder{id: 6754})
iface := (*iface)(unsafe.Pointer(&m))
fmt.Printf("iface.tab.hash = %#x\n", iface.tab.hash) // 0x615f3d8a
}
```
It's a match! `fmt.Printf("iface.tab.hash = %#x\n", iface.tab.hash)` gives us `0x615f3d8a`, which corresponds to the value that we've extracted from the contents of the ELF file.
**Conclusion**
We've reconstructed the complete `itab` for our `iface<Mather, Adder>` interface; it's all there in the executable, just waiting to be used, and already contains all the information that the runtime will need to make the interface behave as we expect.
Of course, since an `itab` is mostly composed of a bunch of pointers to other datastructures, we'd have to follow the virtual addresses present in the contents that we've extracted via `dd` in order to reconstruct the complete picture.
Speaking of pointers, we can now have a clear view of the virtual-table for `iface<Mather, Adder>`; here's an annotated version of the contents of `go.itab.main.Adder,main.Mather`:
```Bash
$ dd if=iface.bin of=/dev/stdout bs=1 count=40 skip=479552 2>/dev/null | hexdump
0000000 bd20 0045 0000 0000 ed40 0045 0000 0000
0000010 3d8a 615f 0000 0000 c2d0 0044 0000 0000
# ^^^^^^^^^^^^^^^^^^^
# offset 0x18+8: itab.fun[0]
0000020 c350 0044 0000 0000
# ^^^^^^^^^^^^^^^^^^^
# offset 0x20+8: itab.fun[1]
0000028
```
```Bash
$ objdump -t -j .text iface.bin | grep 000000000044c2d0
000000000044c2d0 g F .text 0000000000000079 main.(*Adder).Add
```
```Bash
$ objdump -t -j .text iface.bin | grep 000000000044c350
000000000044c350 g F .text 000000000000007f main.(*Adder).Sub
```
Without surprise, the virtual table for `iface<Mather, Adder>` holds two method pointers: `main.(*Adder).add` and `main.(*Adder).sub`.
Well, actually, this *is* a bit surprising: we've never defined these two methods to have pointer receivers.
The compiler has generated these wrapper methods on our behalf (as we've described in the ["Implicit dereferencing" section](#implicit-dereferencing)) because it knows that we're going to need them: since an interface can only hold pointers, and since our `Adder` implementation of said interface only provides methods with value-receivers, we'll have to go through a wrapper at some point if we're going to call either of these methods via the virtual table of the interface.
This should already give you a pretty good idea of how dynamic dispatch is handled at run time; which is what we will look at in the next section.
**Bonus**
I've hacked up a generic bash script that you can use to dump the contents of any symbol in any section of an ELF file ([dump_sym.sh](./dump_sym.sh)):
```Bash
# ./dump_sym.sh bin_path section_name sym_name
$ ./dump_sym.sh iface.bin .rodata go.itab.main.Adder,main.Mather
.rodata file-offset: 315392
.rodata VMA: 4509696
go.itab.main.Adder,main.Mather VMA: 4673856
go.itab.main.Adder,main.Mather SIZE: 40
0000000 bd20 0045 0000 0000 ed40 0045 0000 0000
0000010 3d8a 615f 0000 0000 c2d0 0044 0000 0000
0000020 c350 0044 0000 0000
0000028
```
I'd imagine there must exist an easier way to do what this script does, maybe some arcane flags or an obscure gem hidden inside the `binutils` distribution.. who knows.
If you've got some hints, don't hesitate to say so in the issues.
## Dynamic dispatch
In this section we'll finally cover the main feature of interfaces: dynamic dispatch.
Specifically, we'll look at how dynamic dispatch works under the hood, and how much we got to pay for it.
### Indirect method call on interface
Let's have a look back at our code from earlier ([iface.go](./iface.go)):
```Go
type Mather interface {
Add(a, b int32) int32
Sub(a, b int64) int64
}
type Adder struct{ id int32 }
//go:noinline
func (adder Adder) Add(a, b int32) int32 { return a + b }
//go:noinline
func (adder Adder) Sub(a, b int64) int64 { return a - b }
func main() {
m := Mather(Adder{id: 6754})
m.Add(10, 32)
}
```
We've already had a deeper look into most of what happens in this piece of code: how the `iface<Mather, Adder>` interface gets created, how it's laid out in the final exectutable, and how it ends up being loaded by the runtime.
There's only one thing left for us to look at, and that is the actual indirect method call that follows: `m.Add(10, 32)`.
To refresh our memory, we'll zoom in on both the creation of the interface as well as on the method call itself:
```Go
m := Mather(Adder{id: 6754})
m.Add(10, 32)
```
Thankfully, we already have a fully annotated version of the assembly generated by the instantiation done on the first line (`m := Mather(Adder{id: 6754})`):
```Assembly
;; m := Mather(Adder{id: 6754})
0x001d MOVL $6754, ""..autotmp_1+36(SP) ;; create an addressable $6754 value at 36(SP)
0x0025 LEAQ go.itab."".Adder,"".Mather(SB), AX ;; set up go.itab."".Adder,"".Mather..
0x002c MOVQ AX, (SP) ;; ..as first argument (tab *itab)
0x0030 LEAQ ""..autotmp_1+36(SP), AX ;; set up &36(SP)..
0x0035 MOVQ AX, 8(SP) ;; ..as second argument (elem unsafe.Pointer)
0x003a CALL runtime.convT2I32(SB) ;; runtime.convT2I32(go.itab."".Adder,"".Mather, &$6754)
0x003f MOVQ 16(SP), AX ;; AX now holds i.tab (go.itab."".Adder,"".Mather)
0x0044 MOVQ 24(SP), CX ;; CX now holds i.data (&$6754, somewhere on the heap)
```
And now, here's the assembly listing for the indirect method call that follows (`m.Add(10, 32)`):
```Assembly
;; m.Add(10, 32)
0x0049 MOVQ 24(AX), AX
0x004d MOVQ $137438953482, DX
0x0057 MOVQ DX, 8(SP)
0x005c MOVQ CX, (SP)
0x0060 CALL AX
```
With the knowledge accumulated from the previous sections, these few instructions should be straightforward to understand.
```Assembly
0x0049 MOVQ 24(AX), AX
```
Once `runtime.convT2I32` has returned, `AX` holds `i.tab`, which as we know is a pointer to an `itab`; and more specifically a pointer to `go.itab."".Adder,"".Mather` in this case.
By dereferencing `AX` and offsetting 24 bytes forward, we reach `i.tab.fun`, which corresponds to the first entry of the virtual table.
Here's a reminder of what the offset table for `itab` looks like:
```Go
type itab struct { // 32 bytes on a 64bit arch
inter *interfacetype // offset 0x00 ($00)
_type *_type // offset 0x08 ($08)
hash uint32 // offset 0x10 ($16)
_ [4]byte // offset 0x14 ($20)
fun [1]uintptr // offset 0x18 ($24)
// offset 0x20 ($32)
}
```
As we've seen in the previous section where we've reconstructed the final `itab` directly from the executable, `iface.tab.fun[0]` is a pointer to `main.(*Adder).add`, which is the compiler-generated wrapper-method that wraps our original value-receiver `main.Adder.add` method.
```Assembly
0x004d MOVQ $137438953482, DX
0x0057 MOVQ DX, 8(SP)
```
We store `10` and `32` at the top of the stack, as arguments #2 & #3.
```Assembly
0x005c MOVQ CX, (SP)
0x0060 CALL AX
```
Once `runtime.convT2I32` has returned, `CX` holds `i.data`, which is a pointer to our `Adder` instance.
We move this pointer to the top of stack, as argument #1, to satisfy the calling convention: the receiver for a method should always be passed as the first argument.
Finally, with our stack all set up, we can do the actual call.
We'll close this section with a complete annotated assembly listing of the entire process:
```Assembly
;; m := Mather(Adder{id: 6754})
0x001d MOVL $6754, ""..autotmp_1+36(SP) ;; create an addressable $6754 value at 36(SP)
0x0025 LEAQ go.itab."".Adder,"".Mather(SB), AX ;; set up go.itab."".Adder,"".Mather..
0x002c MOVQ AX, (SP) ;; ..as first argument (tab *itab)
0x0030 LEAQ ""..autotmp_1+36(SP), AX ;; set up &36(SP)..
0x0035 MOVQ AX, 8(SP) ;; ..as second argument (elem unsafe.Pointer)
0x003a CALL runtime.convT2I32(SB) ;; runtime.convT2I32(go.itab."".Adder,"".Mather, &$6754)
0x003f MOVQ 16(SP), AX ;; AX now holds i.tab (go.itab."".Adder,"".Mather)
0x0044 MOVQ 24(SP), CX ;; CX now holds i.data (&$6754, somewhere on the heap)
;; m.Add(10, 32)
0x0049 MOVQ 24(AX), AX ;; AX now holds (*iface.tab)+0x18, i.e. iface.tab.fun[0]
0x004d MOVQ $137438953482, DX ;; move (32,10) to..
0x0057 MOVQ DX, 8(SP) ;; ..the top of the stack (arguments #3 & #2)
0x005c MOVQ CX, (SP) ;; CX, which holds &$6754 (i.e., our receiver), gets moved to
;; ..the top of stack (argument #1 -> receiver)
0x0060 CALL AX ;; you know the drill
```
We now have a clear picture of the entire machinery required for interfaces and virtual method calls to work.
In the next section, we'll measure the actual cost of this machinery, in theory as well as in practice.
### Overhead
As we've seen, the implementation of interfaces delegates most of the work on both the compiler and the linker. From a performance standpoint, this is obviously very good news: we effectively want to relieve the runtime from as much work as possible.
There do exist some specific cases where instantiating an interface may also require the runtime to get to work (e.g. the `runtime.convT2*` family of functions), though they are not so prevalent in practice.
We'll learn more about these edge cases in the [section dedicated to the special cases of interfaces](#special-cases--compiler-tricks). In the meantime, we'll concentrate purely on the overhead of virtual method calls and ignore the one-time costs related to instantiation.
Once an interface has been properly instantiated, calling methods on it is nothing more than going through one more layer of indirection compared to the usual statically dispatched call (i.e. dereferencing `itab.fun` at the desired index).
As such, one would imagine this process to be virtually free.. and one would be kind of right, but not quite: the theory is a bit tricky, and the reality even trickier still.
#### The theory: quick refresher on modern CPUs
The extra indirection inherent to virtual calls is, in and of itself, effectively free *for as long as it is somewhat predictable from the standpoint of the CPU*.
Modern CPUs are very aggressive beasts: they cache aggressively, they aggressively pre-fetch both instructions & data, they pre-execute code aggressively, they even reorder and parallelize it as they see fit.
All of this extra work is done whether we want it or not, hence we should always strive not to get in the way of the CPU's efforts to be extra smart, so all of these precious cycles don't go needlessly wasted.
This is where virtual method calls can quickly become a problem.
In the case of a statically dispatched call, the CPU has foreknowledge of the upcoming branch in the program and pre-fetches the necessary instructions accordingly. This makes up for a smooth, transparent transition from one branch of the program to the other as far as performance is concerned.
With dynamic dispatch, on the other hand, the CPU cannot know in advance where the program is heading: it all depends on computations whose results are, by definition, not known until run time. To counter-balance this, the CPU applies various algorithms and heuristics in order to guess where the program is going to branch next (i.e. "branch prediction").
If the processor guesses correctly, we can expect a dynamic branch to be almost as efficient as a static one, since the instructions of the landing site have already been pre-fetched into the processor's caches anyway.
If it gets things wrong, though, things can get a bit rough: first, of course, we'll have to pay for the extra indirection plus the corresponding (slow) load from main memory (i.e. the CPU is effectively stalled) to load the right instructions into the L1i cache. Even worse, we'll have to pay for the price of the CPU backtracking in its own mistakes and flushing its instruction pipeline following the branch misprediction.
Another important downside of dynamic dispatch is that it makes inlining impossible by definition: one simply cannot inline what they don't know is coming.
All in all, it should, at least in theory, be very possible to end up with massive differences in performance between a direct call to an inlined function F, and a call to that same function that couldn't be inlined and had to go through some extra layers of indirection, and maybe even got hit by a branch misprediction on its way.
That's mostly it for the theory.
When it comes to modern hardware, though, one should always be wary of the theory.
Let's measure this stuff.
#### The practice: benchmarks
First things first, some information about the CPU we're running on:
```Bash
$ lscpu | sed -nr '/Model name/ s/.*:\s*(.* @ .*)/\1/p'
Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
```
We'll define the interface used for our benchmarks as such ([iface_bench_test.go](./iface_bench_test.go)):
```Go
type identifier interface {
idInline() int32
idNoInline() int32
}
type id32 struct{ id int32 }
// NOTE: Use pointer receivers so we don't measure the extra overhead incurred by
// autogenerated wrappers as part of our results.
func (id *id32) idInline() int32 { return id.id }
//go:noinline
func (id *id32) idNoInline() int32 { return id.id }
```
**Benchmark suite A: single instance, many calls, inlined & non-inlined**
For our first two benchmarks, we'll try calling a non-inlined method inside a busy-loop, on both an `*Adder` value and a `iface<Mather, *Adder>` interface:
```Go
var escapeMePlease *id32
// escapeToHeap makes sure that `id` escapes to the heap.
//
// In simple situations such as some of the benchmarks present in this file,
// the compiler is able to statically infer the underlying type of the
// interface (or rather the type of the data it points to, to be pedantic) and
// ends up replacing what should have been a dynamic method call by a
// static call.
// This anti-optimization prevents this extra cleverness.
//
//go:noinline
func escapeToHeap(id *id32) identifier {
escapeMePlease = id
return escapeMePlease
}
var myID int32
func BenchmarkMethodCall_direct(b *testing.B) {
b.Run("single/noinline", func(b *testing.B) {
m := escapeToHeap(&id32{id: 6754}).(*id32)
for i := 0; i < b.N; i++ {
// CALL "".(*id32).idNoInline(SB)
// MOVL 8(SP), AX
// MOVQ "".&myID+40(SP), CX
// MOVL AX, (CX)
myID = m.idNoInline()
}
})
}
func BenchmarkMethodCall_interface(b *testing.B) {
b.Run("single/noinline", func(b *testing.B) {
m := escapeToHeap(&id32{id: 6754})
for i := 0; i < b.N; i++ {
// MOVQ 32(AX), CX
// MOVQ "".m.data+40(SP), DX
// MOVQ DX, (SP)
// CALL CX
// MOVL 8(SP), AX
// MOVQ "".&myID+48(SP), CX
// MOVL AX, (CX)
myID = m.idNoInline()
}
})
}
```
We expect both benchmarks to run A) extremely fast and B) at almost the same speeds.
Given the tightness of the loop, we can expect both benchmarks to have their data (receiver & vtable) and instructions (`"".(*id32).idNoInline`) already be present in the L1d/L1i caches of the CPU for each iteration of the loop. I.e., performance should be purely CPU-bound.
`BenchmarkMethodCall_interface` should run a bit slower (on the nanosecond scale) though, as it has to deal with the overhead of finding & copying the right pointer from the virtual table (which is already in the L1 cache, though).
Since the `CALL CX` instruction has a strong dependency on the output of these few extra instructions required to consult the vtable, the processor has no choice but to execute all of this extra logic as a sequential stream, leaving any chance of instruction-level parallelization on the table.
This is ultimately the main reason why we would expect the "interface" version to run a bit slower.
We end up with the following results for the "direct" version:
```Bash
$ go test -run=NONE -o iface_bench_test.bin iface_bench_test.go && \
perf stat --cpu=1 \
taskset 2 \
./iface_bench_test.bin -test.cpu=1 -test.benchtime=1s -test.count=3 \
-test.bench='BenchmarkMethodCall_direct/single/noinline'
BenchmarkMethodCall_direct/single/noinline 2000000000 1.81 ns/op
BenchmarkMethodCall_direct/single/noinline 2000000000 1.80 ns/op
BenchmarkMethodCall_direct/single/noinline 2000000000 1.80 ns/op
Performance counter stats for 'CPU(s) 1':
11702.303843 cpu-clock (msec) # 1.000 CPUs utilized
2,481 context-switches # 0.212 K/sec
1 cpu-migrations # 0.000 K/sec
7,349 page-faults # 0.628 K/sec
43,726,491,825 cycles # 3.737 GHz
110,979,100,648 instructions # 2.54 insn per cycle
19,646,440,556 branches # 1678.852 M/sec
566,424 branch-misses # 0.00% of all branches
11.702332281 seconds time elapsed
```
And here's for the "interface" version:
```Bash
$ go test -run=NONE -o iface_bench_test.bin iface_bench_test.go && \
perf stat --cpu=1 \
taskset 2 \
./iface_bench_test.bin -test.cpu=1 -test.benchtime=1s -test.count=3 \
-test.bench='BenchmarkMethodCall_interface/single/noinline'
BenchmarkMethodCall_interface/single/noinline 2000000000 1.95 ns/op
BenchmarkMethodCall_interface/single/noinline 2000000000 1.96 ns/op
BenchmarkMethodCall_interface/single/noinline 2000000000 1.96 ns/op
Performance counter stats for 'CPU(s) 1':
12709.383862 cpu-clock (msec) # 1.000 CPUs utilized
3,003 context-switches # 0.236 K/sec
1 cpu-migrations # 0.000 K/sec
10,524 page-faults # 0.828 K/sec
47,301,533,147 cycles # 3.722 GHz
124,467,105,161 instructions # 2.63 insn per cycle
19,878,711,448 branches # 1564.097 M/sec
761,899 branch-misses # 0.00% of all branches
12.709412950 seconds time elapsed
```
The results match our expectations: the "interface" version is indeed a bit slower, with approximately 0.15 extra nanoseconds per iteration, or a ~8% slowdown.
8% might sound like a noticeable difference at first, but we have to keep in mind that A) these are nanosecond-scale measurements and B) the method being called does so little work that it magnifies even more the overhead of the call.
Looking at the number of instructions per benchmark, we see that the interface-based version has had to execute for ~14 billion more instructions compared to the "direct" version (`110,979,100,648` vs. `124,467,105,161`), even though both benchmarks were run for `6,000,000,000` (`2,000,000,000\*3`) iterations.
As we've mentioned before, the CPU cannot parallelize these extra instructions due to the `CALL` depending on them, which gets reflected quite clearly in the instruction-per-cycle ratio: both benchmarks end up with a similar IPC ratio (`2.54` vs. `2.63`) even though the "interface" version has much more work to do overall.
This lack of parallelism piles up to an extra ~3.5 billion CPU cycles for the "interface" version, which is where those extra 0.15ns that we've measured are actually spent.
Now what happens when we let the compiler inline the method call?
```Go
var myID int32
func BenchmarkMethodCall_direct(b *testing.B) {
b.Run("single/inline", func(b *testing.B) {
m := escapeToHeap(&id32{id: 6754}).(*id32)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// MOVL (DX), SI
// MOVL SI, (CX)
myID = m.idInline()
}
})
}
func BenchmarkMethodCall_interface(b *testing.B) {
b.Run("single/inline", func(b *testing.B) {
m := escapeToHeap(&id32{id: 6754})
b.ResetTimer()
for i := 0; i < b.N; i++ {
// MOVQ 32(AX), CX
// MOVQ "".m.data+40(SP), DX
// MOVQ DX, (SP)
// CALL CX
// MOVL 8(SP), AX
// MOVQ "".&myID+48(SP), CX
// MOVL AX, (CX)
myID = m.idNoInline()
}
})
}
```
Two things jump out at us:
- `BenchmarkMethodCall_direct`: Thanks to inlining, the call has been reduced to a simple pair of memory moves.
- `BenchmarkMethodCall_interface`: Due to dynamic dispatch, the compiler has been unable to inline the call, thus the generated assembly ends up being exactly the same as before.
We won't even bother running `BenchmarkMethodCall_interface` since the code hasn't changed a bit.
Let's have a quick look at the "direct" version though:
```Bash
$ go test -run=NONE -o iface_bench_test.bin iface_bench_test.go && \
perf stat --cpu=1 \
taskset 2 \
./iface_bench_test.bin -test.cpu=1 -test.benchtime=1s -test.count=3 \
-test.bench='BenchmarkMethodCall_direct/single/inline'
BenchmarkMethodCall_direct/single/inline 2000000000 0.35 ns/op
BenchmarkMethodCall_direct/single/inline 2000000000 0.34 ns/op
BenchmarkMethodCall_direct/single/inline 2000000000 0.34 ns/op
Performance counter stats for 'CPU(s) 1':
2464.353001 cpu-clock (msec) # 1.000 CPUs utilized
629 context-switches # 0.255 K/sec
1 cpu-migrations # 0.000 K/sec
7,322 page-faults # 0.003 M/sec
9,026,867,915 cycles # 3.663 GHz
41,580,825,875 instructions # 4.61 insn per cycle
7,027,066,264 branches # 2851.485 M/sec
1,134,955 branch-misses # 0.02% of all branches
2.464386341 seconds time elapsed
```
As expected, this runs ridiculously fast now that the overhead of the call is gone.
With ~0.34ns per op for the "direct" inlined version, the "interface" version is now ~475% slower, quite a steep drop from the ~8% difference that we've measured earlier with inlining disabled.
Notice how, with the branching inherent to the method call now gone, the CPU is able to parallelize and speculatively execute the remaining instructions much more efficiently, reaching an IPC ratio of 4.61.
**Benchmark suite B: many instances, many non-inlined calls, small/big/pseudo-random iterations**
For this second benchmark suite, we'll look at a more real-world-like situation in which an iterator goes through a slice of objects that all expose a common method and calls it for each object.
To better mimic reality, we'll disable inlining, as most methods called this way in a real program would most likely by sufficiently complex not to be inlined by the compiler (YMMV; a good counter-example of this is the `sort.Interface` interface from the standard library).
We'll define 3 similar benchmarks that just differ in the way they access this slice of objects; the goal being to simulate decreasing levels of cache friendliness:
1. In the first case, the iterator walks the array in order, calls the method, then gets incremented by the size of one object for each iteration.
1. In the second case, the iterator still walks the slice in order, but this time gets incremented by a value that's larger than the size of a single cache-line.
1. Finally, in the third case, the iterator will pseudo-randomly steps through the slice.
In all three cases, we'll make sure that the array is big enough not to fit entirely in any of the processor's caches in order to simulate (not-so-accurately) a very busy server that's putting a lot of pressure of both its CPU caches and main memory.
Here's a quick recap of the processor's attributes, we'll design the benchmarks accordingly:
```Bash
$ lscpu | sed -nr '/Model name/ s/.*:\s*(.* @ .*)/\1/p'
Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
$ lscpu | grep cache
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 6144K
$ getconf LEVEL1_DCACHE_LINESIZE
64
$ getconf LEVEL1_ICACHE_LINESIZE
64
$ find /sys/devices/system/cpu/cpu0/cache/index{1,2,3} -name "shared_cpu_list" -exec cat {} \;
# (annotations are mine)
0,4 # L1 (hyperthreading)
0,4 # L2 (hyperthreading)
0-7 # L3 (shared + hyperthreading)
```
Here's what the benchmark suite looks like for the "direct" version (the benchmarks marked as `baseline` compute the cost of retrieving the receiver in isolation, so that we can subtract that cost from the final measurements):
```Go
const _maxSize = 2097152 // 2^21
const _maxSizeModMask = _maxSize - 1 // avoids a mod (%) in the hot path
var _randIndexes = [_maxSize]int{}
func init() {
rand.Seed(42)
for i := range _randIndexes {
_randIndexes[i] = rand.Intn(_maxSize)
}
}
func BenchmarkMethodCall_direct(b *testing.B) {
adders := make([]*id32, _maxSize)
for i := range adders {
adders[i] = &id32{id: int32(i)}
}
runtime.GC()
var myID int32
b.Run("many/noinline/small_incr", func(b *testing.B) {
var m *id32
b.Run("baseline", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = adders[i&_maxSizeModMask]
}
})
b.Run("call", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = adders[i&_maxSizeModMask]
myID = m.idNoInline()
}
})
})
b.Run("many/noinline/big_incr", func(b *testing.B) {
var m *id32
b.Run("baseline", func(b *testing.B) {
j := 0
for i := 0; i < b.N; i++ {
m = adders[j&_maxSizeModMask]
j += 32
}
})
b.Run("call", func(b *testing.B) {
j := 0
for i := 0; i < b.N; i++ {
m = adders[j&_maxSizeModMask]
myID = m.idNoInline()
j += 32
}
})
})
b.Run("many/noinline/random_incr", func(b *testing.B) {
var m *id32
b.Run("baseline", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = adders[_randIndexes[i&_maxSizeModMask]]
}
})
b.Run("call", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = adders[_randIndexes[i&_maxSizeModMask]]
myID = m.idNoInline()
}
})
})
}
```
The benchmark suite for the "interface" version is identical, except that the array is initialized with interface values instead of pointers to the concrete type, as one would expect:
```Go
func BenchmarkMethodCall_interface(b *testing.B) {
adders := make([]identifier, _maxSize)
for i := range adders {
adders[i] = identifier(&id32{id: int32(i)})
}
runtime.GC()
/* ... */
}
```
For the "direct" suite, we get the following results:
```Bash
$ go test -run=NONE -o iface_bench_test.bin iface_bench_test.go && \
benchstat <(
taskset 2 ./iface_bench_test.bin -test.cpu=1 -test.benchtime=1s -test.count=3 \
-test.bench='BenchmarkMethodCall_direct/many/noinline')
name time/op
MethodCall_direct/many/noinline/small_incr/baseline 0.99ns ± 3%
MethodCall_direct/many/noinline/small_incr/call 2.32ns ± 1% # 2.32 - 0.99 = 1.33ns
MethodCall_direct/many/noinline/big_incr/baseline 5.86ns ± 0%
MethodCall_direct/many/noinline/big_incr/call 17.1ns ± 1% # 17.1 - 5.86 = 11.24ns
MethodCall_direct/many/noinline/random_incr/baseline 8.80ns ± 0%
MethodCall_direct/many/noinline/random_incr/call 30.8ns ± 0% # 30.8 - 8.8 = 22ns
```
There really are no surprises here:
1. `small_incr`: By being *extremely* cache-friendly, we obtain results similar to the previous benchmark that looped over a single instance.
2. `big_incr`: By forcing the CPU to fetch a new cache-line at every iteration, we do see a noticeable bump in latencies, which is completely unrelated to the cost of doing the call, though: ~6ns are attributable to the baseline while the rest is a combination of the cost of dereferencing the receiver in order to get to its `id` field and copying around the return value.
3. `random_incr`: Same remarks as with `big_incr`, except that the bump in latencies is even more pronounced due to A) the pseudo-random accesses and B) the cost of retrieving the next index from the pre-computed array of indexes (which triggers cache misses in and of itself).
As logic would dictate, thrashing the CPU d-caches doesn't seem to influence the latency of the actual direct method call (inlined or not) by any mean, although it does make everything that surrounds it slower.
What about dynamic dispatch?
```Bash
$ go test -run=NONE -o iface_bench_test.bin iface_bench_test.go && \
benchstat <(
taskset 2 ./iface_bench_test.bin -test.cpu=1 -test.benchtime=1s -test.count=3 \
-test.bench='BenchmarkMethodCall_interface/many/inline')
name time/op
MethodCall_interface/many/noinline/small_incr/baseline 1.38ns ± 0%
MethodCall_interface/many/noinline/small_incr/call 3.48ns ± 0% # 3.48 - 1.38 = 2.1ns
MethodCall_interface/many/noinline/big_incr/baseline 6.86ns ± 0%
MethodCall_interface/many/noinline/big_incr/call 19.6ns ± 1% # 19.6 - 6.86 = 12.74ns
MethodCall_interface/many/noinline/random_incr/baseline 11.0ns ± 0%
MethodCall_interface/many/noinline/random_incr/call 34.7ns ± 0% # 34.7 - 11.0 = 23.7ns
```
The results are extremely similar, albeit a tiny bit slower overall simply due to the fact that we're copying two quad-words (i.e. both fields of an `identifier` interface) out of the slice at each iteration instead of one (a pointer to `id32`).
The reason this runs almost as fast as its "direct" counterpart is that, since all the interfaces in the slice share a common `itab` (i.e. they're all `iface<Mather, Adder>` interfaces), their associated virtual table never leaves the L1d cache and so fetching the right method pointer at each iteration is virtually free.
Likewise, the instructions that make up the body of the `main.(*id32).idNoInline` method never leave the L1i cache.
One might think that, in practice, a slice of interfaces would encompass many different underlying types (and thus vtables), which would result in thrashing of both the L1i and L1d caches due to the varying vtables pushing each other out.
While that holds true in theory, these kinds of thoughts tend to be the result of years of experience using older OOP languages such as C++ that (used to, at least) encourage the use of deeply-nested hierarchies of inherited classes and virtual calls as their main tool of abstraction.
With big enough hierarchies, the number of associated vtables could sometimes get large enough to thrash the CPU caches when iterating over a datastructure holding various implementations of a virtual class (think e.g. of a GUI framework where everything is a `Widget` stored in a graph-like datastructure); especially so that, in C++ at least, virtual classes tend to specify quite complex behaviors, sometimes with dozen of methods, resulting in quite big vtables and even more pressure on the L1d cache.
Go, on the other hand, has very different idioms: OOP has been completely thrown out of the window, the type system flattened, and interfaces are most often used to describe minimal, constrained behaviors (a few methods at most an average, helped by the fact that interfaces are implicitly satisfied) instead of being used as an abstraction on top of a more complex, layered type hierarchy.
In practice, in Go, I've found it's very rare to have to iterate over a set of interfaces that carry many different underlying types. YMMV, of course.
For the curious-minded, here's what the results of the "direct" version would have looked like with inlining enabled:
```Bash
name time/op
MethodCall_direct/many/inline/small_incr 0.97ns ± 1% # 0.97ns
MethodCall_direct/many/inline/big_incr/baseline 5.96ns ± 1%
MethodCall_direct/many/inline/big_incr/call 11.9ns ± 1% # 11.9 - 5.96 = 5.94ns
MethodCall_direct/many/inline/random_incr/baseline 9.20ns ± 1%
MethodCall_direct/many/inline/random_incr/call 16.9ns ± 1% # 16.9 - 9.2 = 7.7ns
```
Which would have made the "direct" version around 2 to 3 times faster than the "interface" version in cases where the compiler would have been able to inline the call.
Then again, as we've mentioned earlier, the limited capabilities of the current compiler with regards to inlining mean that, in practice, these kind of wins would rarely be seen. And of course, there often are times when you really don't have a choice but to resort to virtual calls anyway.
**Conclusion**
Effectively measuring the latency of a virtual call turned out to be quite a complex endeavor, as most of it is the direct consequence of many intertwined side-effects that result from the very complex implementation details of modern hardware.
*In Go*, thanks to the idioms encouraged by the design of the language, and taking into account the (current) limitations of the compiler with regards to inlining, one could effectively consider dynamic dispatch as virtually free.
Still, when in doubt, one should always measure their hot paths and look at the relevant performance counters to assert with certainty whether dynamic dispatch ends up being an issue or not.
*(NOTE: We will look at the inlining capabilities of the compiler in a later chapter of this book.*)
## Special cases & compiler tricks
This section will review some of the most common special cases that we encounter every day when dealing with interfaces.
By now you should have a pretty clear idea of how interfaces work, so we'll try and aim for conciseness here.
### The empty interface
The datastructure for the empty interface is what you'd intuitively think it would be: an `iface` without an `itab`.
There are two reasons for that:
1. Since the empty interface has no methods, everything related to dynamic dispatch can safely be dropped from the datastructure.
1. With the virtual table gone, the type of the empty interface itself, not to be confused with the type of the data it holds, is always the same (i.e. we talk about *the* empty interface rather than *an* empty interface).
*NOTE: Similar to the notation we used for `iface`, we'll denote the empty interface holding a type T as `eface<T>`*
`eface` is the root type that represents the empty interface within the runtime ([src/runtime/runtime2.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/runtime2.go#L148-L151)).
Its definition goes like this:
```Go
type eface struct { // 16 bytes on a 64bit arch
_type *_type
data unsafe.Pointer
}
```
Where `_type` holds the type information of the value pointed to by `data`.
As expected, the `itab` has been dropped entirely.
While the empty interface could just reuse the `iface` datastructure (it is a superset of `eface` after all), the runtime chooses to distinguish the two for two main reasons: space efficiency and code clarity.
### Interface holding a scalar type
Earlier in this chapter ([#Anatomy of an Interface](#overview-of-the-datastructures)), we've mentioned that even storing a simple scalar type such as an integer into an interface will result in a heap allocation.
It's time we see why, and how.
Consider these two benchmarks ([eface_scalar_test.go](./eface_scalar_test.go)):
```Go
func BenchmarkEfaceScalar(b *testing.B) {
var Uint uint32
b.Run("uint32", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Uint = uint32(i)
}
})
var Eface interface{}
b.Run("eface32", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Eface = uint32(i)
}
})
}
```
```Bash
$ go test -benchmem -bench=. ./eface_scalar_test.go
BenchmarkEfaceScalar/uint32-8 2000000000 0.54 ns/op 0 B/op 0 allocs/op
BenchmarkEfaceScalar/eface32-8 100000000 12.3 ns/op 4 B/op 1 allocs/op
```
1. That's a 2-orders-of-magnitude difference in performance for a simple assignment operation, and
1. we can see that the second benchmark has to allocate 4 extra bytes at each iteration.
Clearly, some hidden heavy machinery is being set off in the second case: we need to have a look at the generated assembly.
For the first benchmark, the compiler generates exactly what you'd expect it to with regard to the assignment operation:
```Assembly
;; Uint = uint32(i)
0x000d MOVL DX, (AX)
```
In the second benchmark, though, things get far more complex:
```Assembly
;; Eface = uint32(i)
0x0050 MOVL CX, ""..autotmp_3+36(SP)
0x0054 LEAQ type.uint32(SB), AX
0x005b MOVQ AX, (SP)
0x005f LEAQ ""..autotmp_3+36(SP), DX
0x0064 MOVQ DX, 8(SP)
0x0069 CALL runtime.convT2E32(SB)
0x006e MOVQ 24(SP), AX
0x0073 MOVQ 16(SP), CX
0x0078 MOVQ "".&Eface+48(SP), DX
0x007d MOVQ CX, (DX)
0x0080 MOVL runtime.writeBarrier(SB), CX
0x0086 LEAQ 8(DX), DI
0x008a TESTL CX, CX
0x008c JNE 148
0x008e MOVQ AX, 8(DX)
0x0092 JMP 46
0x0094 CALL runtime.gcWriteBarrier(SB)
0x0099 JMP 46
```
This is *just* the assignment, not the complete benchmark!
We'll have to study this code piece by piece.
**Step 1: Create the interface**
```Assembly
0x0050 MOVL CX, ""..autotmp_3+36(SP)
0x0054 LEAQ type.uint32(SB), AX
0x005b MOVQ AX, (SP)
0x005f LEAQ ""..autotmp_3+36(SP), DX
0x0064 MOVQ DX, 8(SP)
0x0069 CALL runtime.convT2E32(SB)
0x006e MOVQ 24(SP), AX
0x0073 MOVQ 16(SP), CX
```
This first piece of the listing instantiates the empty interface `eface<uint32>` that we will later assign to `Eface`.
We've already studied similar code in the section about creating interfaces ([#Creating an interface](#creating-an-interface)), except that this code was calling `runtime.convT2I32` instead of `runtime.convT2E32` here; nonetheless, this should look very familiar.
It turns out that `runtime.convT2I32` and `runtime.convT2E32` are part of a larger family of functions whose job is to instanciate either a specific interface or the empty interface from a scalar value (or a string or slice, as special cases).
This family is composed of 10 symbols, one for each combination of `(eface/iface, 16/32/64/string/slice)`:
```Go
// empty interface from scalar value
func convT2E16(t *_type, elem unsafe.Pointer) (e eface) {}
func convT2E32(t *_type, elem unsafe.Pointer) (e eface) {}
func convT2E64(t *_type, elem unsafe.Pointer) (e eface) {}
func convT2Estring(t *_type, elem unsafe.Pointer) (e eface) {}
func convT2Eslice(t *_type, elem unsafe.Pointer) (e eface) {}
// interface from scalar value
func convT2I16(tab *itab, elem unsafe.Pointer) (i iface) {}
func convT2I32(tab *itab, elem unsafe.Pointer) (i iface) {}
func convT2I64(tab *itab, elem unsafe.Pointer) (i iface) {}
func convT2Istring(tab *itab, elem unsafe.Pointer) (i iface) {}
func convT2Islice(tab *itab, elem unsafe.Pointer) (i iface) {}
```
(*You'll notice that there is no `convT2E8` nor `convT2I8` function; this is due to a compiler optimization that we'll take a look at at the end of this section.*)
All of these functions do almost the exact same thing, they only differ in the type of their return value (`iface` vs. `eface`) and the size of the memory that they allocate on the heap.
Let's take a look at e.g. `runtime.convT2E32` more closely ([src/runtime/iface.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/iface.go#L308-L325)):
```Go
func convT2E32(t *_type, elem unsafe.Pointer) (e eface) {
/* ...omitted debug stuff... */
var x unsafe.Pointer
if *(*uint32)(elem) == 0 {
x = unsafe.Pointer(&zeroVal[0])
} else {
x = mallocgc(4, t, false)
*(*uint32)(x) = *(*uint32)(elem)
}
e._type = t
e.data = x
return
}
```
The function initializes the `_type` field of the `eface` structure "passed" in by the caller (remember: return values are allocated by the caller on its own stack-frame) with the `_type` given as first parameter.
For the `data` field of the `eface`, it all depends on the value of the second parameter `elem`:
- If `elem` is zero, `e.data` is initialized to point to `runtime.zeroVal`, which is a special global variable defined by the runtime that represents the zero value. We'll discuss a bit more about this special variable in the next section.
- If `elem` is non-zero, the function allocates 4 bytes on the heap (`x = mallocgc(4, t, false)`), initializes the contents of those 4 bytes with the value pointed to by `elem` (`*(*uint32)(x) = *(*uint32)(elem)`), then stick the resulting pointer into `e.data`.
In this case, `e._type` holds the address of `type.uint32` (`LEAQ type.uint32(SB), AX`), which is implemented by the standard library and whose address will only be known when linking against said stdlib:
```Bash
$ go tool nm eface_scalar_test.o | grep 'type\.uint32'
U type.uint32
```
(`U` denotes that the symbol is not defined in this object file, and will (hopefully) be provided by another object at link-time (i.e. the standard library in this case).)
**Step 2: Assign the result (part 1)**
```Assembly
0x0078 MOVQ "".&Eface+48(SP), DX
0x007d MOVQ CX, (DX) ;; Eface._type = ret._type
```
The result of `runtime.convT2E32` gets assigned to our `Eface` variable.. or does it?
Actually, for now, only the `_type` field of the returned value is being assigned to `Eface._type`, the `data` field cannot be copied over just yet.
**Step 3: Assign the result (part 2) or ask the garbage collector to**
```Assembly
0x0080 MOVL runtime.writeBarrier(SB), CX
0x0086 LEAQ 8(DX), DI ;; Eface.data = ret.data (indirectly via runtime.gcWriteBarrier)
0x008a TESTL CX, CX
0x008c JNE 148
0x008e MOVQ AX, 8(DX) ;; Eface.data = ret.data (direct)
0x0092 JMP 46
0x0094 CALL runtime.gcWriteBarrier(SB)
0x0099 JMP 46
```
The apparent complexity of this last piece is a side-effect of assigning the `data` pointer of the returned `eface` to `Eface.data`: since we're manipulating the memory graph of our program (i.e. which part of memory holds references to which part of memory), we may have to notify the garbage collector of this change, just in case a garbage collection were to be currently running in the background.
This is known as a write barrier, and is a direct consequence of Go's *concurrent* garbage collector.
Don't worry if this sounds a bit vague for now; the next chapter of this book will offer a thorough review of garbage collection in Go.
For now, it's enough to remember that when we see some assembly code calling into `runtime.gcWriteBarrier`, it has to do with pointer manipulation and notifying the garbage collector.
All in all, this final piece of code can do one of two things:
- If the write-barrier is currently inactive, it assigns `ret.data` to `Eface.data` (`MOVQ AX, 8(DX)`).
- If the write-barrier is active, it politely asks the garbage-collector to do the assignment on our behalf
(`LEAQ 8(DX), DI` + `CALL runtime.gcWriteBarrier(SB)`).
(*Once again, try not to worry too much about this for now.*)
Voila, we've got a complete interface holding a simple scalar type (`uint32`).
**Conclusion**
While sticking a scalar value into an interface is not something that happens that often in practice, it can be a costly operation for various reasons, and as such it's important to be aware of the machinery behind it.
Speaking of cost, we've mentioned that the compiler implements various tricks to avoid allocating in some specific situations; we'll close this section with a quick look at 3 of those tricks.
**Interface trick 1: Byte-sized values**
Consider this benchmark that instanciates an `eface<uint8>` ([eface_scalar_test.go](./eface_scalar_test.go)):
```Go
func BenchmarkEfaceScalar(b *testing.B) {
b.Run("eface8", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// LEAQ type.uint8(SB), BX
// MOVQ BX, (CX)
// MOVBLZX AL, SI
// LEAQ runtime.staticbytes(SB), R8
// ADDQ R8, SI
// MOVL runtime.writeBarrier(SB), R9
// LEAQ 8(CX), DI
// TESTL R9, R9
// JNE 100
// MOVQ SI, 8(CX)
// JMP 40
// MOVQ AX, R9
// MOVQ SI, AX
// CALL runtime.gcWriteBarrier(SB)
// MOVQ R9, AX
// JMP 40
Eface = uint8(i)
}
})
}
```
```Bash
$ go test -benchmem -bench=BenchmarkEfaceScalar/eface8 ./eface_scalar_test.go
BenchmarkEfaceScalar/eface8-8 2000000000 0.88 ns/op 0 B/op 0 allocs/op
```
We notice that in the case of a byte-sized value, the compiler avoids the call to `runtime.convT2E`/`runtime.convT2I` and the associated heap allocation, and instead re-uses the address of a global variable exposed by the runtime that already holds the 1-byte value we're looking for: `LEAQ runtime.staticbytes(SB), R8`.
`runtime.staticbytes` can be found in [src/runtime/iface.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/iface.go#L619-L653) and looks like this:
```Go
// staticbytes is used to avoid convT2E for byte-sized values.
var staticbytes = [...]byte{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
}
```
Using the right offset into this array, the compiler can effectively avoid an extra heap allocation and still reference any value representable as a single byte.
Something feels wrong here, though.. can you tell?
The generated code still embeds all the machinery related to the write-barrier, even though the pointer we're manipulating holds the address of a global variable whose lifetime is the same as the entire program's anyway.
I.e. `runtime.staticbytes` can never be garbage collected, no matter which part of memory holds a reference to it or not, so we shouldn't have to pay for the overhead of a write-barrier in this case.
**Interface trick 2: Static inference**
Consider this benchmark that instanciates an `eface<uint64>` from a value known at compile time ([eface_scalar_test.go](./eface_scalar_test.go)):
```Go
func BenchmarkEfaceScalar(b *testing.B) {
b.Run("eface-static", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// LEAQ type.uint64(SB), BX
// MOVQ BX, (CX)
// MOVL runtime.writeBarrier(SB), SI
// LEAQ 8(CX), DI
// TESTL SI, SI
// JNE 92
// LEAQ "".statictmp_0(SB), SI
// MOVQ SI, 8(CX)
// JMP 40
// MOVQ AX, SI
// LEAQ "".statictmp_0(SB), AX
// CALL runtime.gcWriteBarrier(SB)
// MOVQ SI, AX
// LEAQ "".statictmp_0(SB), SI
// JMP 40
Eface = uint64(42)
}
})
}
```
```Bash
$ go test -benchmem -bench=BenchmarkEfaceScalar/eface-static ./eface_scalar_test.go
BenchmarkEfaceScalar/eface-static-8 2000000000 0.81 ns/op 0 B/op 0 allocs/op
```
We can see from the generated assembly that the compiler completely optimizes out the call to `runtime.convT2E64`, and instead directly constructs the empty interface by loading the address of an autogenerated global variable that already holds the value we're looking for: `LEAQ "".statictmp_0(SB), SI` (note the `(SB)` part, indicating a global variable).
We can better visualize what's going on using the script that we've hacked up earlier: `dump_sym.sh`.
```Bash
$ GOOS=linux GOARCH=amd64 go tool compile eface_scalar_test.go
$ GOOS=linux GOARCH=amd64 go tool link -o eface_scalar_test.bin eface_scalar_test.o
$ ./dump_sym.sh eface_scalar_test.bin .rodata main.statictmp_0
.rodata file-offset: 655360
.rodata VMA: 4849664
main.statictmp_0 VMA: 5145768
main.statictmp_0 SIZE: 8
0000000 002a 0000 0000 0000
0000008
```
As expected, `main.statictmp_0` is a 8-byte variable whose value is `0x000000000000002a`, i.e. `$42`.
**Interface trick 3: Zero-values**
For this final trick, consider the following benchmark that instanciates an `eface<uint32>` from a zero-value ([eface_scalar_test.go](./eface_scalar_test.go)):
```Go
func BenchmarkEfaceScalar(b *testing.B) {
b.Run("eface-zeroval", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// MOVL $0, ""..autotmp_3+36(SP)
// LEAQ type.uint32(SB), AX
// MOVQ AX, (SP)
// LEAQ ""..autotmp_3+36(SP), CX
// MOVQ CX, 8(SP)
// CALL runtime.convT2E32(SB)
// MOVQ 16(SP), AX
// MOVQ 24(SP), CX
// MOVQ "".&Eface+48(SP), DX
// MOVQ AX, (DX)
// MOVL runtime.writeBarrier(SB), AX
// LEAQ 8(DX), DI
// TESTL AX, AX
// JNE 152
// MOVQ CX, 8(DX)
// JMP 46
// MOVQ CX, AX
// CALL runtime.gcWriteBarrier(SB)
// JMP 46
Eface = uint32(i - i) // outsmart the compiler (avoid static inference)
}
})
}
```
```Bash
$ go test -benchmem -bench=BenchmarkEfaceScalar/eface-zero ./eface_scalar_test.go
BenchmarkEfaceScalar/eface-zeroval-8 500000000 3.14 ns/op 0 B/op 0 allocs/op
```
First, notice how we make use of `uint32(i - i)` instead of `uint32(0)` to prevent the compiler from falling back to optimization #2 (static inference).
(*Sure, we could just have declared a global zero variable and the compiler would had been forced to take the conservative route too.. but then again, we're trying to have some fun here. Don't be that guy.*)
The generated code now looks exactly like the normal, allocating case.. and still, it doesn't allocate. What's going on?
As we've mentioned earlier back when we were dissecting `runtime.convT2E32`, the allocation here can be optimized out using a trick similar to #1 (byte-sized values): when some code needs to reference a variable that holds a zero-value, the compiler simply gives it the address of a global variable exposed by the runtime whose value is always zero.
Similarly to `runtime.staticbytes`, we can find this variable in the runtime code ([src/runtime/hashmap.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/hashmap.go#L1248-L1249)):
```Go
const maxZero = 1024 // must match value in ../cmd/compile/internal/gc/walk.go
var zeroVal [maxZero]byte
```
This ends our little tour of optimizations.
We'll close this section with a summary of all the benchmarks that we've just looked at:
```Bash
$ go test -benchmem -bench=. ./eface_scalar_test.go
BenchmarkEfaceScalar/uint32-8 2000000000 0.54 ns/op 0 B/op 0 allocs/op
BenchmarkEfaceScalar/eface32-8 100000000 12.3 ns/op 4 B/op 1 allocs/op
BenchmarkEfaceScalar/eface8-8 2000000000 0.88 ns/op 0 B/op 0 allocs/op
BenchmarkEfaceScalar/eface-zeroval-8 500000000 3.14 ns/op 0 B/op 0 allocs/op
BenchmarkEfaceScalar/eface-static-8 2000000000 0.81 ns/op 0 B/op 0 allocs/op
```
### A word about zero-values
As we've just seen, the `runtime.convT2*` family of functions avoids a heap allocation when the data to be held by the resulting interface happens to reference a zero-value.
This optimization is not specific to interfaces and is actually part of a broader effort by the Go runtime to make sure that, when in need of a pointer to a zero-value, unnecessary allocations are avoided by taking the address of a special, always-zero variable exposed by the runtime.
We can confirm this with a simple program ([zeroval.go](./zeroval.go)):
```Go
//go:linkname zeroVal runtime.zeroVal
var zeroVal uintptr
type eface struct{ _type, data unsafe.Pointer }
func main() {
x := 42
var i interface{} = x - x // outsmart the compiler (avoid static inference)
fmt.Printf("zeroVal = %p\n", &zeroVal)
fmt.Printf(" i = %p\n", ((*eface)(unsafe.Pointer(&i))).data)
}
```
```Bash
$ go run zeroval.go
zeroVal = 0x5458e0
i = 0x5458e0
```
As expected.
Note the `//go:linkname` directive which allows us to reference an external symbol:
> The //go:linkname directive instructs the compiler to use “importpath.name” as the object file symbol name for the variable or function declared as “localname” in the source code. Because this directive can subvert the type system and package modularity, it is only enabled in files that have imported "unsafe".
### A tangent about zero-size variables
In a similar vein as zero-values, a very common trick in Go programs is to rely on the fact that instanciating an object of size 0 (such as `struct{}{}`) doesn't result in an allocation.
The official Go specification (linked at the end of this chapter) ends on a note that explains this:
> A struct or array type has size zero if it contains no fields (or elements, respectively) that have a size greater than zero.
> Two distinct zero-size variables may have the same address in memory.
The "may" in "may have the same address in memory" implies that the compiler doesn't guarantee this fact to be true, although it has always been and continues to be the case in the current implementation of the official Go compiler (`gc`).
As usual, we can confirm this with a simple program ([zerobase.go](./zerobase.go)):
```Go
func main() {
var s struct{}
var a [42]struct{}
fmt.Printf("s = % p\n", &s)
fmt.Printf("a = % p\n", &a)
}
```
```Bash
$ go run zerobase.go
s = 0x546fa8
a = 0x546fa8
```
If we'd like to know what hides behind this address, we can simply have a peek inside the binary:
```Bash
$ go build -o zerobase.bin zerobase.go && objdump -t zerobase.bin | grep 546fa8
0000000000546fa8 g O .noptrbss 0000000000000008 runtime.zerobase
```
Then it's just a matter of finding this `runtime.zerobase` variable within the runtime source code ([src/runtime/malloc.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/malloc.go#L516-L517)):
```Go
// base address for all 0-byte allocations
var zerobase uintptr
```
And if we'd rather be really, really sure indeed:
```Go
//go:linkname zerobase runtime.zerobase
var zerobase uintptr
func main() {
var s struct{}
var a [42]struct{}
fmt.Printf("zerobase = %p\n", &zerobase)
fmt.Printf(" s = %p\n", &s)
fmt.Printf(" a = %p\n", &a)
}
```
```Bash
$ go run zerobase.go
zerobase = 0x546fa8
s = 0x546fa8
a = 0x546fa8
```
## Interface composition
There really is nothing special about interface composition, it merely is syntastic sugar exposed by the compiler.
Consider the following program ([compound_interface.go](./compound_interface.go)):
```Go
type Adder interface{ Add(a, b int32) int32 }
type Subber interface{ Sub(a, b int32) int32 }
type Mather interface {
Adder
Subber
}
type Calculator struct{ id int32 }
func (c *Calculator) Add(a, b int32) int32 { return a + b }
func (c *Calculator) Sub(a, b int32) int32 { return a - b }
func main() {
calc := Calculator{id: 6754}
var m Mather = &calc
m.Sub(10, 32)
}
```
As usual, the compiler generates the corresponding `itab` for `iface<Mather, *Calculator>`:
```Bash
$ GOOS=linux GOARCH=amd64 go tool compile -S compound_interface.go | \
grep -A 7 '^go.itab.\*"".Calculator,"".Mather'
go.itab.*"".Calculator,"".Mather SRODATA dupok size=40
0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0010 5e 33 ca c8 00 00 00 00 00 00 00 00 00 00 00 00 ^3..............
0x0020 00 00 00 00 00 00 00 00 ........
rel 0+8 t=1 type."".Mather+0
rel 8+8 t=1 type.*"".Calculator+0
rel 24+8 t=1 "".(*Calculator).Add+0
rel 32+8 t=1 "".(*Calculator).Sub+0
```
We can see from the relocation directives that the virtual table generated by the compiler holds both the methods of `Adder` as well as those belonging to `Subber`, as we'd expect:
```
rel 24+8 t=1 "".(*Calculator).Add+0
rel 32+8 t=1 "".(*Calculator).Sub+0
```
Like we said, there's no secret sauce when it comes to interface composition.
On an unrelated note, this little program demonstrates something that we had not seen up until now: since the generated `itab` is specifically tailored to *a pointer to* a `Constructor`, as opposed to a concrete value, this fact gets reflected both in its symbol-name (`go.itab.*"".Calculator,"".Mather`) as well as in the `_type` that it embeds (`type.*"".Calculator`).
This is consistent with the semantics used for naming method symbols, like we've seen earlier at the beginning of this chapter.
## Assertions
We'll close this chapter by looking at type assertions, both from an implementation and a cost standpoint.
### Type assertions
Consider this short program ([eface_to_type.go](./eface_to_type.go)):
```Go
var j uint32
var Eface interface{} // outsmart compiler (avoid static inference)
func assertion() {
i := uint64(42)
Eface = i
j = Eface.(uint32)
}
```
Here's the annotated assembly listing for `j = Eface.(uint32)`:
```Assembly
0x0065 00101 MOVQ "".Eface(SB), AX ;; AX = Eface._type
0x006c 00108 MOVQ "".Eface+8(SB), CX ;; CX = Eface.data
0x0073 00115 LEAQ type.uint32(SB), DX ;; DX = type.uint32
0x007a 00122 CMPQ AX, DX ;; Eface._type == type.uint32 ?
0x007d 00125 JNE 162 ;; no? panic our way outta here
0x007f 00127 MOVL (CX), AX ;; AX = *Eface.data
0x0081 00129 MOVL AX, "".j(SB) ;; j = AX = *Eface.data
;; exit
0x0087 00135 MOVQ 40(SP), BP
0x008c 00140 ADDQ $48, SP
0x0090 00144 RET
;; panic: interface conversion: <iface> is <have>, not <want>
0x00a2 00162 MOVQ AX, (SP) ;; have: Eface._type
0x00a6 00166 MOVQ DX, 8(SP) ;; want: type.uint32
0x00ab 00171 LEAQ type.interface {}(SB), AX ;; AX = type.interface{} (eface)
0x00b2 00178 MOVQ AX, 16(SP) ;; iface: AX
0x00b7 00183 CALL runtime.panicdottypeE(SB) ;; func panicdottypeE(have, want, iface *_type)
0x00bc 00188 UNDEF
0x00be 00190 NOP
```
Nothing surprising in there: the code compares the address held by `Eface._type` with the address of `type.uint32`, which, as we've seen before, is the global symbol exposed by the standard library that holds the content of the `_type` structure which describes an `uint32`.
If the `_type` pointers match, then all is good and we're free to assign `*Eface.data` to `j`; otherwise, we call `runtime.panicdottypeE` to throw a panic message that precisely describes the mismatch.
`runtime.panicdottypeE` is a _very_ simple function that does no more than you'd expect ([src/runtime/iface.go](https://github.com/golang/go/blob/bf86aec25972f3a100c3aa58a6abcbcc35bdea49/src/runtime/iface.go#L235-L245)):
```Go
// panicdottypeE is called when doing an e.(T) conversion and the conversion fails.
// have = the dynamic type we have.
// want = the static type we're trying to convert to.
// iface = the static type we're converting from.
func panicdottypeE(have, want, iface *_type) {
haveString := ""
if have != nil {
haveString = have.string()
}
panic(&TypeAssertionError{iface.string(), haveString, want.string(), ""})
}
```
**What about performance?**
Well, let's see what we've got here: a bunch of `MOV`s from main memory, a *very* predictable branch and, last but not least, a pointer dereference (`j = *Eface.data`) (which is only there because we've initialized our interface with a concrete value in the first place, otherwise we could just have copied the `Eface.data` pointer directly).
It's not even worth micro-benchmarking this, really.
Similarly to the overhead of dynamic dispatch that we've measured earlier, this is in and of itself, in theory, almost free. How much it'll really cost you in practice will most likely be a matter of how your code-path is designed with regard to cache-friendliness & al.
A simple micro-benchmark would probably be too skewed to tell us anything useful here, anyway.
All in all, we end up with the same old advice as usual: measure for your specific use case, check your processor's performance counters, and assert whether or not this has a visible impact on your hot path.
It might. It might not. It most likely doesn't.
### Type-switches
Type-switches are a bit trickier, of course. Consider the following code ([eface_to_type.go](./eface_to_type.go)):
```Go
var j uint32
var Eface interface{} // outsmart compiler (avoid static inference)
func typeSwitch() {
i := uint32(42)
Eface = i
switch v := Eface.(type) {
case uint16:
j = uint32(v)
case uint32:
j = v
}
}
```
This quite simple type-switch statement translates into the following assembly (annotated):
```Assembly
;; switch v := Eface.(type)
0x0065 00101 MOVQ "".Eface(SB), AX ;; AX = Eface._type
0x006c 00108 MOVQ "".Eface+8(SB), CX ;; CX = Eface.data
0x0073 00115 TESTQ AX, AX ;; Eface._type == nil ?
0x0076 00118 JEQ 153 ;; yes? exit the switch
0x0078 00120 MOVL 16(AX), DX ;; DX = Eface.type._hash
;; case uint32
0x007b 00123 CMPL DX, $-800397251 ;; Eface.type._hash == type.uint32.hash ?
0x0081 00129 JNE 163 ;; no? go to next case (uint16)
0x0083 00131 LEAQ type.uint32(SB), BX ;; BX = type.uint32
0x008a 00138 CMPQ BX, AX ;; type.uint32 == Eface._type ? (hash collision?)
0x008d 00141 JNE 206 ;; no? clear BX and go to next case (uint16)
0x008f 00143 MOVL (CX), BX ;; BX = *Eface.data
0x0091 00145 JNE 163 ;; landsite for indirect jump starting at 0x00d3
0x0093 00147 MOVL BX, "".j(SB) ;; j = BX = *Eface.data
;; exit
0x0099 00153 MOVQ 40(SP), BP
0x009e 00158 ADDQ $48, SP
0x00a2 00162 RET
;; case uint16
0x00a3 00163 CMPL DX, $-269349216 ;; Eface.type._hash == type.uint16.hash ?
0x00a9 00169 JNE 153 ;; no? exit the switch
0x00ab 00171 LEAQ type.uint16(SB), DX ;; DX = type.uint16
0x00b2 00178 CMPQ DX, AX ;; type.uint16 == Eface._type ? (hash collision?)
0x00b5 00181 JNE 199 ;; no? clear AX and exit the switch
0x00b7 00183 MOVWLZX (CX), AX ;; AX = uint16(*Eface.data)
0x00ba 00186 JNE 153 ;; landsite for indirect jump starting at 0x00cc
0x00bc 00188 MOVWLZX AX, AX ;; AX = uint16(AX) (redundant)
0x00bf 00191 MOVL AX, "".j(SB) ;; j = AX = *Eface.data
0x00c5 00197 JMP 153 ;; we're done, exit the switch
;; indirect jump table
0x00c7 00199 MOVL $0, AX ;; AX = $0
0x00cc 00204 JMP 186 ;; indirect jump to 153 (exit)
0x00ce 00206 MOVL $0, BX ;; BX = $0
0x00d3 00211 JMP 145 ;; indirect jump to 163 (case uint16)
```
Once again, if you meticulously step through the generated code and carefully read the corresponding annotations, you'll find that there's no dark magic in there.
The control flow might look a bit convoluted at first, as it jumps back and forth a lot, but other than it's a pretty faithful rendition of the original Go code.
There are quite a few interesting things to note, though.
**Note 1: Layout**
First, notice the high-level layout of the generated code, which matches pretty closely the original switch statement:
1. We find an initial block of instructions that loads the `_type` of the variable we're interested in, and checks for `nil` pointers, just in case.
1. Then, we get N logical blocks that each correspond to one of the cases described in the original switch statement.
1. And finally, one last block defines a kind of indirect jump table that allows the control flow to jump from one case to the next while making sure to properly reset dirty registers on the way.
While obvious in hindsight, that second point is pretty important, as it implies that the number of instructions generated by a type-switch statement is purely a factor of the number of cases that it describes.
In practice, this could lead to surprising performance issues as, for example, a massive type-switch statement with plenty of cases could generate a ton of instructions and end up thrashing the L1i cache if used on the wrong path.
Another interesting fact regarding the layout of our simple switch-statement above is the order in which the cases are set up in the generated code. In our original Go code, `case uint16` came first, followed by `case uint32`. In the assembly generated by the compiler, though, their orders have been reversed, with `case uint32` now being first and `case uint16` coming in second.
That this reordering is a net win for us in this particular case is nothing but mere luck, AFAICT. In fact, if you take the time to experiment a bit with type-switches, especially ones with more than two cases, you'll find that the compiler always shuffles the cases using some kind of deterministic heuristics.
What those heuristics are, I don't know (but as always, I'd love to if you do).
**Note 2: O(n)**
Second, notice how the control flow blindly jumps from one case to the next, until it either lands on one that evaluates to true or finally reaches the end of the switch statement.
Once again, while obvious when one actually stops to think about it ("how else could it work?"), this is easy to overlook when reasoning at a higher-level. In practice, this means that the cost of evaluating a type-switch statement grows linearly with its number of cases: it's `O(n)`.
Likewise, evaluating a type-switch statement with N cases effectively has the same time-complexity as evaluating N type-assertions. As we've said, there's no magic here.
It's easy to confirm this with a bunch of benchmarks ([eface_to_type_test.go](./eface_to_type_test.go)):
```Go
var j uint32
var eface interface{} = uint32(42)
func BenchmarkEfaceToType(b *testing.B) {
b.Run("switch-small", func(b *testing.B) {
for i := 0; i < b.N; i++ {
switch v := eface.(type) {
case int8:
j = uint32(v)
case int16:
j = uint32(v)
default:
j = v.(uint32)
}
}
})
b.Run("switch-big", func(b *testing.B) {
for i := 0; i < b.N; i++ {
switch v := eface.(type) {
case int8:
j = uint32(v)
case int16:
j = uint32(v)
case int32:
j = uint32(v)
case int64:
j = uint32(v)
case uint8:
j = uint32(v)
case uint16:
j = uint32(v)
case uint64:
j = uint32(v)
default:
j = v.(uint32)
}
}
})
}
```
```Bash
benchstat <(go test -benchtime=1s -bench=. -count=3 ./eface_to_type_test.go)
name time/op
EfaceToType/switch-small-8 1.91ns ± 2%
EfaceToType/switch-big-8 3.52ns ± 1%
```
With all its extra cases, the second type-switch does take almost twice as long per iteration indeed.
As an interesting exercise for the reader, try adding a `case uint32` in either one of the benchmarks above (anywhere), you'll see their performances improve drastically:
```Bash
benchstat <(go test -benchtime=1s -bench=. -count=3 ./eface_to_type_test.go)
name time/op
EfaceToType/switch-small-8 1.63ns ± 1%
EfaceToType/switch-big-8 2.17ns ± 1%
```
Using all the tools and knowledge that we've gathered during this chapter, you should be able to explain the rationale behind the numbers. Have fun!
**Note 3: Type hashes & pointer comparisons**
Finally, notice how the type comparisons in each cases are always done in two phases:
1. The types' hashes (`_type.hash`) are compared, and then
2. if they match, the respective memory-addresses of each `_type` pointers are compared directly.
Since each `_type` structure is generated once by the compiler and stored in a global variable in the `.rodata` section, we are guaranteed that each type gets assigned a unique address for the lifetime of the program.
In that context, it makes sense to do this extra pointer comparison in order to make sure that the successful match wasn't simply the result of a hash collision.. but then this raises an obvious question: why not just compare the pointers directly in the first place, and drop the notion of type hashes altogether? Especially when simple type assertions, as we've seen earlier, don't use type hashes at all.
The answer is I don't have the slightest clue, and certainly would love some enlightment on this. As always, feel free to open an issue if you know more.
Speaking of type hashes, how is it that we know that `$-800397251` corresponds to `type.uint32.hash` and `$-269349216` to `type.uint16.hash`, you might wonder? The hard way, of course ([eface_type_hash.go](./eface_type_hash.go)):
```Go
// simplified definitions of runtime's eface & _type types
type eface struct {
_type *_type
data unsafe.Pointer
}
type _type struct {
size uintptr
ptrdata uintptr
hash uint32
/* omitted lotta fields */
}
var Eface interface{}
func main() {
Eface = uint32(42)
fmt.Printf("eface<uint32>._type.hash = %d\n",
int32((*eface)(unsafe.Pointer(&Eface))._type.hash))
Eface = uint16(42)
fmt.Printf("eface<uint16>._type.hash = %d\n",
int32((*eface)(unsafe.Pointer(&Eface))._type.hash))
}
```
```
$ go run eface_type_hash.go
eface<uint32>._type.hash = -800397251
eface<uint16>._type.hash = -269349216
```
## Conclusion
That's it for interfaces.
I hope this chapter has given you most of the answers you were looking for when it comes to interfaces and their innards. Most importantly, it should have provided you with all the necessary tools and skills required to dig further whenever you'd need to.
If you have any questions or suggestions, don't hesitate to open an issue with the `chapter2:` prefix!
## Links
- [[Official] Go 1.1 Function Calls](https://docs.google.com/document/d/1bMwCey-gmqZVTpRax-ESeVuZGmjwbocYs1iHplK-cjo/pub)
- [[Official] The Go Programming Language Specification](https://golang.org/ref/spec)
- [The Gold linker by Ian Lance Taylor](https://lwn.net/Articles/276782/)
- [ELF: a linux executable walkthrough](https://i.imgur.com/EL7lT1i.png)
- [VMA vs LMA?](https://www.embeddedrelated.com/showthread/comp.arch.embedded/77071-1.php)
- [In C++ why and how are virtual functions slower?](https://softwareengineering.stackexchange.com/questions/191637/in-c-why-and-how-are-virtual-functions-slower)
- [The cost of dynamic (virtual calls) vs. static (CRTP) dispatch in C++](https://eli.thegreenplace.net/2013/12/05/the-cost-of-dynamic-virtual-calls-vs-static-crtp-dispatch-in-c)
- [Why is it faster to process a sorted array than an unsorted array?](https://stackoverflow.com/a/11227902)
- [Is accessing data in the heap faster than from the stack?](https://stackoverflow.com/a/24057744)
- [CPU cache](https://en.wikipedia.org/wiki/CPU_cache)
- [CppCon 2014: Mike Acton "Data-Oriented Design and C++"](https://www.youtube.com/watch?v=rX0ItVEVjHc)
- [CppCon 2017: Chandler Carruth "Going Nowhere Faster"](https://www.youtube.com/watch?v=2EWejmkKlxs)
- [What is the difference between MOV and LEA?](https://stackoverflow.com/a/1699778)
- [Issue #24631 (golang/go): *testing: don't truncate allocs/op*](https://github.com/golang/go/issues/24631)
================================================
FILE: chapter2_interfaces/compound_interface.go
================================================
package main
type Adder interface{ Add(a, b int32) int32 }
type Subber interface{ Sub(a, b int32) int32 }
type Mather interface {
Adder
Subber
}
type Calculator struct{ id int32 }
func (c *Calculator) Add(a, b int32) int32 { return a + b }
func (c *Calculator) Sub(a, b int32) int32 { return a - b }
func main() {
calc := Calculator{id: 6754}
var m Mather = &calc
m.Sub(10, 32)
}
================================================
FILE: chapter2_interfaces/direct_calls.go
================================================
package main
//go:noinline
func Add(a, b int32) int32 { return a + b }
type Adder struct{ id int32 }
//go:noinline
func (adder *Adder) AddPtr(a, b int32) int32 { return a + b }
//go:noinline
func (adder Adder) AddVal(a, b int32) int32 { return a + b }
func main() {
Add(10, 32) // direct call of top-level function
adder := Adder{id: 6754}
adder.AddPtr(10, 32) // direct call of method with pointer receiver
adder.AddVal(10, 32) // direct call of method with value receiver
(&adder).AddVal(10, 32) // implicit dereferencing
}
================================================
FILE: chapter2_interfaces/dump_sym.sh
================================================
#!/usr/bin/env bash
BIN="$1"
test "$BIN"
SECTION="$2"
test "$SECTION"
SYM="$3"
test "$SYM"
section_offset=$(
readelf -St -W "$BIN" | \
grep -A 1 "$SECTION" | \
tail -n +2 | \
awk '{print toupper($3)}'
)
section_offset_dec=$(echo "ibase=16;$section_offset" | bc)
echo "$SECTION file-offset: $section_offset_dec"
section_vma=$(
readelf -St -W "$BIN" | \
grep -A 1 "$SECTION" | \
tail -n +2 | \
awk '{print toupper($2)}'
)
section_vma_dec=$(echo "ibase=16;$section_vma" | bc)
echo "$SECTION VMA: $section_vma_dec"
sym_vma=$(objdump -t -j "$SECTION" "$BIN" | grep "$SYM" | awk '{print toupper($1)}')
sym_vma_dec=$(echo "ibase=16;$sym_vma" | bc)
echo "$SYM VMA: $sym_vma_dec"
sym_size=$(objdump -t -j "$SECTION" "$BIN" | grep "$SYM" | awk '{print toupper($5)}')
sym_size_dec=$(echo "ibase=16;$sym_size" | bc)
echo -e "$SYM SIZE: $sym_size_dec\n"
sym_offset=$(( $sym_vma_dec - $section_vma_dec + $section_offset_dec ))
dd if="$BIN" of=/dev/stdout bs=1 count=$sym_size_dec skip="$sym_offset" 2>/dev/null | hexdump
================================================
FILE: chapter2_interfaces/eface_scalar_test.go
================================================
package main
import (
"testing"
)
func BenchmarkEfaceScalar(b *testing.B) {
var Uint uint32
b.Run("uint32", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// MOVL DX, (AX)
Uint = uint32(i)
}
})
var Eface interface{}
b.Run("eface32", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// MOVL CX, ""..autotmp_3+36(SP)
// LEAQ type.uint32(SB), AX
// MOVQ AX, (SP)
// LEAQ ""..autotmp_3+36(SP), DX
// MOVQ DX, 8(SP)
// CALL runtime.convT2E32(SB)
// MOVQ 24(SP), AX
// MOVQ 16(SP), CX
// MOVQ "".&Eface+48(SP), DX
// MOVQ CX, (DX)
// MOVL runtime.writeBarrier(SB), CX
// LEAQ 8(DX), DI
// TESTL CX, CX
// JNE 148
// MOVQ AX, 8(DX)
// JMP 46
// CALL runtime.gcWriteBarrier(SB)
// JMP 46
Eface = uint32(i)
}
})
b.Run("eface8", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// LEAQ type.uint8(SB), BX
// MOVQ BX, (CX)
// MOVBLZX AL, SI
// LEAQ runtime.staticbytes(SB), R8
// ADDQ R8, SI
// MOVL runtime.writeBarrier(SB), R9
// LEAQ 8(CX), DI
// TESTL R9, R9
// JNE 100
// MOVQ SI, 8(CX)
// JMP 40
// MOVQ AX, R9
// MOVQ SI, AX
// CALL runtime.gcWriteBarrier(SB)
// MOVQ R9, AX
// JMP 40
Eface = uint8(i)
}
})
b.Run("eface-zeroval", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// MOVL $0, ""..autotmp_3+36(SP)
// LEAQ type.uint32(SB), AX
// MOVQ AX, (SP)
// LEAQ ""..autotmp_3+36(SP), CX
// MOVQ CX, 8(SP)
// CALL runtime.convT2E32(SB)
// MOVQ 16(SP), AX
// MOVQ 24(SP), CX
// MOVQ "".&Eface+48(SP), DX
// MOVQ AX, (DX)
// MOVL runtime.writeBarrier(SB), AX
// LEAQ 8(DX), DI
// TESTL AX, AX
// JNE 152
// MOVQ CX, 8(DX)
// JMP 46
// MOVQ CX, AX
// CALL runtime.gcWriteBarrier(SB)
// JMP 46
Eface = uint32(i - i) // outsmart the compiler
}
})
b.Run("eface-static", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// LEAQ type.uint64(SB), BX
// MOVQ BX, (CX)
// MOVL runtime.writeBarrier(SB), SI
// LEAQ 8(CX), DI
// TESTL SI, SI
// JNE 92
// LEAQ "".statictmp_0(SB), SI
// MOVQ SI, 8(CX)
// JMP 40
// MOVQ AX, SI
// LEAQ "".statictmp_0(SB), AX
// CALL runtime.gcWriteBarrier(SB)
// MOVQ SI, AX
// LEAQ "".statictmp_0(SB), SI
// JMP 40
Eface = uint64(42)
}
})
}
func main() {
// So that we can easily compile this and retrieve `main.statictmp_0`
// from the final executable.
BenchmarkEfaceScalar(&testing.B{})
}
================================================
FILE: chapter2_interfaces/eface_to_type.go
================================================
package main
var j uint32
var Eface interface{} // outsmart compiler (avoid static inference)
func assertion() {
i := uint32(42)
Eface = i
// 0x0065 00101 MOVQ "".Eface(SB), AX ;; AX = Eface._type
// 0x006c 00108 MOVQ "".Eface+8(SB), CX ;; CX = Eface.data
// 0x0073 00115 LEAQ type.uint32(SB), DX ;; DX = type.uint32
// 0x007a 00122 CMPQ AX, DX ;; Eface._type == type.uint32 ?
// 0x007d 00125 JNE 162 ;; no? panic our way outta here
// 0x007f 00127 MOVL (CX), AX ;; AX = *Eface.data
// 0x0081 00129 MOVL AX, "".j(SB) ;; j = AX = *Eface.data
// ;; exit
// 0x0087 00135 MOVQ 40(SP), BP
// 0x008c 00140 ADDQ $48, SP
// 0x0090 00144 RET
// ;; panic: interface conversion: <iface> is <have>, not <want>
// 0x00a2 00162 MOVQ AX, (SP) ;; have: Eface._type
// 0x00a6 00166 MOVQ DX, 8(SP) ;; want: type.uint32
// 0x00ab 00171 LEAQ type.interface {}(SB), AX ;; AX = type.interface{} (eface)
// 0x00b2 00178 MOVQ AX, 16(SP) ;; iface: AX
// 0x00b7 00183 CALL runtime.panicdottypeE(SB) ;; func panicdottypeE(have, want, iface *_type)
// 0x00bc 00188 UNDEF
// 0x00be 00190 NOP
j = Eface.(uint32)
}
func typeSwitch() {
i := uint32(42)
Eface = i
// ;; switch v := Eface.(type)
// 0x0065 00101 MOVQ "".Eface(SB), AX ;; AX = Eface._type
// 0x006c 00108 MOVQ "".Eface+8(SB), CX ;; CX = Eface.data
// 0x0073 00115 TESTQ AX, AX ;; Eface._type == nil ?
// 0x0076 00118 JEQ 153 ;; yes? exit the switch
// 0x0078 00120 MOVL 16(AX), DX ;; DX = Eface.type._hash
// ;; case uint32
// 0x007b 00123 CMPL DX, $-800397251 ;; Eface.type._hash == type.uint32.hash ?
// 0x0081 00129 JNE 163 ;; no? go to next case (uint16)
// 0x0083 00131 LEAQ type.uint32(SB), BX ;; BX = type.uint32
// 0x008a 00138 CMPQ BX, AX ;; type.uint32 == Eface._type ? (HASH COLLISION?)
// 0x008d 00141 JNE 206 ;; no? clear BX and go to next case (uint16)
// 0x008f 00143 MOVL (CX), BX ;; BX = *Eface.data
// 0x0091 00145 JNE 163 ;; landsite for indirect jump starting at 0x00d3
// 0x0093 00147 MOVL BX, "".j(SB) ;; j = BX = *Eface.data
// ;; exit
// 0x0099 00153 MOVQ 40(SP), BP
// 0x009e 00158 ADDQ $48, SP
// 0x00a2 00162 RET
// ;; case uint16
// 0x00a3 00163 CMPL DX, $-269349216 ;; Eface.type._hash == type.uint16.hash ?
// 0x00a9 00169 JNE 153 ;; no? exit the switch
// 0x00ab 00171 LEAQ type.uint16(SB), DX ;; DX = type.uint16
// 0x00b2 00178 CMPQ DX, AX ;; type.uint16 == Eface._type ? (HASH COLLISION?)
// 0x00b5 00181 JNE 199 ;; no? clear AX and exit the switch
// 0x00b7 00183 MOVWLZX (CX), AX ;; AX = uint16(*Eface.data)
// 0x00ba 00186 JNE 153 ;; landsite for indirect jump starting at 0x00cc
// 0x00bc 00188 MOVWLZX AX, AX ;; AX = uint16(AX) (redundant)
// 0x00bf 00191 MOVL AX, "".j(SB) ;; j = AX = *Eface.data
// 0x00c5 00197 JMP 153 ;; we're done, exit the switch
// ;; indirect jump table
// 0x00c7 00199 MOVL $0, AX ;; AX = $0
// 0x00cc 00204 JMP 186 ;; indirect jump to 153 (exit)
// 0x00ce 00206 MOVL $0, BX ;; BX = $0
// 0x00d3 00211 JMP 145 ;; indirect jump to 163 (case uint16)
switch v := Eface.(type) {
case uint16:
j = uint32(v)
case uint32:
j = v
}
}
func main() {
assertion()
typeSwitch()
}
================================================
FILE: chapter2_interfaces/eface_to_type_test.go
================================================
package main
import "testing"
var j uint32
var eface interface{} = uint32(42)
func BenchmarkEfaceToType(b *testing.B) {
b.Run("switch-small", func(b *testing.B) {
for i := 0; i < b.N; i++ {
switch v := eface.(type) {
case int8:
j = uint32(v)
case uint32:
j = uint32(v)
case int16:
j = uint32(v)
default:
j = v.(uint32)
}
}
})
b.Run("switch-big", func(b *testing.B) {
for i := 0; i < b.N; i++ {
switch v := eface.(type) {
case int8:
j = uint32(v)
case int16:
j = uint32(v)
case int32:
j = uint32(v)
case uint32:
j = uint32(v)
case int64:
j = uint32(v)
case uint8:
j = uint32(v)
case uint16:
j = uint32(v)
case uint64:
j = uint32(v)
default:
j = v.(uint32)
}
}
})
}
func main() {}
================================================
FILE: chapter2_interfaces/eface_type_hash.go
================================================
package main
import (
"fmt"
"unsafe"
)
// simplified definitions of runtime's eface & _type types
type eface struct {
_type *_type
data unsafe.Pointer
}
type _type struct {
size uintptr
ptrdata uintptr
hash uint32
/* omitted lotta fields */
}
// -----------------------------------------------------------------------------
var Eface interface{}
func main() {
Eface = uint32(42)
fmt.Printf("eface<uint32>._type.hash = %d\n",
int32((*eface)(unsafe.Pointer(&Eface))._type.hash))
Eface = uint16(42)
fmt.Printf("eface<uint16>._type.hash = %d\n",
int32((*eface)(unsafe.Pointer(&Eface))._type.hash))
}
================================================
FILE: chapter2_interfaces/escape.go
================================================
package main
type Addifier interface{ Add(a, b int32) int32 }
type Adder struct{ id int32 }
//go:noinline
func (adder Adder) Add(a, b int32) int32 { return a + b }
func main() {
adder := Adder{id: 6754}
adder.Add(10, 32)
Addifier(adder).Add(10, 32)
}
================================================
FILE: chapter2_interfaces/escape_test.go
================================================
package main
import "testing"
type Addifier interface{ Add(a, b int32) int32 }
type Adder struct{ id int32 }
//go:noinline
func (adder Adder) Add(a, b int32) int32 { return a + b }
func BenchmarkDirect(b *testing.B) {
adder := Adder{id: 6754}
for i := 0; i < b.N; i++ {
adder.Add(10, 32)
}
}
func BenchmarkInterface(b *testing.B) {
adder := Adder{id: 6754}
for i := 0; i < b.N; i++ {
Addifier(adder).Add(10, 32)
}
}
func main() {}
================================================
FILE: chapter2_interfaces/iface.go
================================================
package main
type Mather interface {
Add(a, b int32) int32
Sub(a, b int64) int64
}
type Adder struct{ id int32 }
//go:noinline
func (adder Adder) Add(a, b int32) int32 { return a + b }
//go:noinline
func (adder Adder) Sub(a, b int64) int64 { return a - b }
func main() {
m := Mather(Adder{id: 6754})
// This call just makes sure that the interface is actually used.
// Without this call, the linker would see that the interface defined above
// is in fact never used, and thus would optimize it out of the final
// executable.
m.Add(10, 32)
}
================================================
FILE: chapter2_interfaces/iface_bench_test.go
================================================
package main
import (
"math/rand"
"runtime"
"testing"
)
// -----------------------------------------------------------------------------
type identifier interface {
idInline() int32
idNoInline() int32
}
type id32 struct{ id int32 }
// NOTE: Use pointer receivers so we don't measure the extra overhead incurred by
// autogenerated wrappers as part of our results.
func (id *id32) idInline() int32 { return id.id }
//go:noinline
func (id *id32) idNoInline() int32 { return id.id }
// -----------------------------------------------------------------------------
const _maxSize = 2097152 // 2^21
const _maxSizeModMask = _maxSize - 1 // avoids a mod (%) in the hot path
var _randIndexes = [_maxSize]int{}
func init() {
rand.Seed(42)
for i := range _randIndexes {
_randIndexes[i] = rand.Intn(_maxSize)
}
}
var escapeMePlease *id32
// escapeToHeap makes sure that `id` escapes to the heap.
//
// In simple situations such as some of the benchmarks present in this file,
// the compiler is able to statically infer the underlying type of the
// interface (or rather the type of the data it points to, to be pedantic) and
// ends up replacing what should have been a dynamic method call by a
// static call.
// This anti-optimization prevents this extra cleverness.
//
//go:noinline
func escapeToHeap(id *id32) identifier {
escapeMePlease = id
return escapeMePlease
}
func BenchmarkMethodCall_direct(b *testing.B) {
adders := make([]*id32, _maxSize)
for i := range adders {
adders[i] = escapeToHeap(&id32{id: int32(i)}).(*id32)
}
runtime.GC()
var myID int32
b.Run("single/noinline", func(b *testing.B) {
m := escapeToHeap(&id32{id: 6754}).(*id32)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// CALL "".(*id32).idNoInline(SB)
// MOVL 8(SP), AX
// MOVQ "".&myID+40(SP), CX
// MOVL AX, (CX)
myID = m.idNoInline()
}
})
b.Run("single/inline", func(b *testing.B) {
m := escapeToHeap(&id32{id: 6754}).(*id32)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// MOVL (DX), SI
// MOVL SI, (CX)
myID = m.idInline()
}
})
b.Run("many/noinline/small_incr", func(b *testing.B) {
var m *id32
b.Run("baseline", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = adders[i&_maxSizeModMask]
}
})
b.Run("call", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = adders[i&_maxSizeModMask]
myID = m.idNoInline()
}
})
})
b.Run("many/noinline/big_incr", func(b *testing.B) {
var m *id32
b.Run("baseline", func(b *testing.B) {
j := 0
for i := 0; i < b.N; i++ {
m = adders[j&_maxSizeModMask]
j += 32
}
})
b.Run("call", func(b *testing.B) {
j := 0
for i := 0; i < b.N; i++ {
m = adders[j&_maxSizeModMask]
myID = m.idNoInline()
j += 32
}
})
})
b.Run("many/noinline/random_incr", func(b *testing.B) {
var m *id32
b.Run("baseline", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = adders[_randIndexes[i&_maxSizeModMask]]
}
})
b.Run("call", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = adders[_randIndexes[i&_maxSizeModMask]]
myID = m.idNoInline()
}
})
})
}
func BenchmarkMethodCall_interface(b *testing.B) {
adders := make([]identifier, _maxSize)
for i := range adders {
adders[i] = escapeToHeap(&id32{id: int32(i)})
}
runtime.GC()
var myID int32
b.Run("single/noinline", func(b *testing.B) {
m := escapeToHeap(&id32{id: 6754})
b.ResetTimer()
for i := 0; i < b.N; i++ {
// MOVQ 32(AX), CX
// MOVQ "".m.data+40(SP), DX
// MOVQ DX, (SP)
// CALL CX
// MOVL 8(SP), AX
// MOVQ "".&myID+48(SP), CX
// MOVL AX, (CX)
myID = m.idNoInline()
}
})
b.Run("single/inline", func(b *testing.B) {
m := escapeToHeap(&id32{id: 6754})
b.ResetTimer()
for i := 0; i < b.N; i++ {
// MOVQ 24(AX), CX
// MOVQ "".m.data+40(SP), DX
// MOVQ DX, (SP)
// CALL CX
// MOVL 8(SP), AX
// MOVQ "".&myID+48(SP), CX
// MOVL AX, (CX)
myID = m.idInline()
}
})
b.Run("many/noinline/small_incr", func(b *testing.B) {
var m identifier
b.Run("baseline", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = adders[i&_maxSizeModMask]
}
})
b.Run("call", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = adders[i&_maxSizeModMask]
myID = m.idNoInline()
}
})
})
b.Run("many/noinline/big_incr", func(b *testing.B) {
var m identifier
b.Run("baseline", func(b *testing.B) {
j := 0
for i := 0; i < b.N; i++ {
m = adders[j&_maxSizeModMask]
j += 32
}
})
b.Run("call", func(b *testing.B) {
j := 0
for i := 0; i < b.N; i++ {
m = adders[j&_maxSizeModMask]
myID = m.idNoInline()
j += 32
}
})
})
b.Run("many/noinline/random_incr", func(b *testing.B) {
var m identifier
b.Run("baseline", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = adders[_randIndexes[i&_maxSizeModMask]]
}
})
b.Run("call", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = adders[_randIndexes[i&_maxSizeModMask]]
myID = m.idNoInline()
}
})
})
}
func main() {}
================================================
FILE: chapter2_interfaces/iface_type_hash.go
================================================
package main
import (
"fmt"
"unsafe"
)
type Mather interface {
Add(a, b int32) int32
Sub(a, b int64) int64
}
type Adder struct{ id int32 }
//go:noinline
func (adder Adder) Add(a, b int32) int32 { return a + b }
//go:noinline
func (adder Adder) Sub(a, b int64) int64 { return a - b }
func main() {
m := Mather(Adder{id: 6754})
iface := (*iface)(unsafe.Pointer(&m))
fmt.Printf("iface.tab.hash = %#x\n", iface.tab.hash)
}
// simplified definitions of runtime's iface & itab types
type iface struct {
tab *itab
data unsafe.Pointer
}
type itab struct {
inter uintptr
_type uintptr
hash uint32
_ [4]byte
fun [1]uintptr
}
================================================
FILE: chapter2_interfaces/zerobase.go
================================================
package main
import (
"fmt"
_ "unsafe"
)
//go:linkname zerobase runtime.zerobase
var zerobase uintptr
func main() {
var s struct{}
var a [42]struct{}
fmt.Printf("zerobase = %p\n", &zerobase)
fmt.Printf(" s = %p\n", &s)
fmt.Printf(" a = %p\n", &a)
}
================================================
FILE: chapter2_interfaces/zeroval.go
================================================
package main
import (
"fmt"
"unsafe"
)
//go:linkname zeroVal runtime.zeroVal
var zeroVal uintptr
type eface struct{ _type, data unsafe.Pointer }
func main() {
x := 42
var i interface{} = x - x // outsmart the compiler (avoid static inference)
fmt.Printf("zeroVal = %p\n", &zeroVal)
fmt.Printf(" i = %p\n", ((*eface)(unsafe.Pointer(&i))).data)
}
================================================
FILE: chapter3_garbage_collector/README.md
================================================
<!-- Copyright © 2018 Clement Rey <cr.rey.clement@gmail.com>. -->
<!-- Licensed under the BY-NC-SA Creative Commons 4.0 International Public License. -->
Soon!
gitextract_5lpvqa3t/
├── .gitignore
├── LICENSE
├── Makefile
├── README.md
├── chapter1_assembly_primer/
│ ├── Makefile
│ ├── README.md
│ └── direct_topfunc_call.go
├── chapter2_interfaces/
│ ├── Makefile
│ ├── README.md
│ ├── compound_interface.go
│ ├── direct_calls.go
│ ├── dump_sym.sh
│ ├── eface_scalar_test.go
│ ├── eface_to_type.go
│ ├── eface_to_type_test.go
│ ├── eface_type_hash.go
│ ├── escape.go
│ ├── escape_test.go
│ ├── iface.go
│ ├── iface_bench_test.go
│ ├── iface_type_hash.go
│ ├── zerobase.go
│ └── zeroval.go
└── chapter3_garbage_collector/
└── README.md
SYMBOL INDEX (60 symbols across 14 files)
FILE: chapter1_assembly_primer/direct_topfunc_call.go
function add (line 4) | func add(a, b int32) (int32, bool) { return a + b, true }
function main (line 6) | func main() { add(10, 32) }
FILE: chapter2_interfaces/compound_interface.go
type Adder (line 3) | type Adder interface
type Subber (line 4) | type Subber interface
type Mather (line 6) | type Mather interface
type Calculator (line 11) | type Calculator struct
method Add (line 13) | func (c *Calculator) Add(a, b int32) int32 { return a + b }
method Sub (line 14) | func (c *Calculator) Sub(a, b int32) int32 { return a - b }
function main (line 16) | func main() {
FILE: chapter2_interfaces/direct_calls.go
function Add (line 4) | func Add(a, b int32) int32 { return a + b }
type Adder (line 6) | type Adder struct
method AddPtr (line 9) | func (adder *Adder) AddPtr(a, b int32) int32 { return a + b }
method AddVal (line 12) | func (adder Adder) AddVal(a, b int32) int32 { return a + b }
function main (line 14) | func main() {
FILE: chapter2_interfaces/eface_scalar_test.go
function BenchmarkEfaceScalar (line 7) | func BenchmarkEfaceScalar(b *testing.B) {
function main (line 106) | func main() {
FILE: chapter2_interfaces/eface_to_type.go
function assertion (line 6) | func assertion() {
function typeSwitch (line 32) | func typeSwitch() {
function main (line 79) | func main() {
FILE: chapter2_interfaces/eface_to_type_test.go
function BenchmarkEfaceToType (line 8) | func BenchmarkEfaceToType(b *testing.B) {
function main (line 49) | func main() {}
FILE: chapter2_interfaces/eface_type_hash.go
type eface (line 9) | type eface struct
type _type (line 13) | type _type struct
function main (line 24) | func main() {
FILE: chapter2_interfaces/escape.go
type Addifier (line 3) | type Addifier interface
type Adder (line 5) | type Adder struct
method Add (line 8) | func (adder Adder) Add(a, b int32) int32 { return a + b }
function main (line 10) | func main() {
FILE: chapter2_interfaces/escape_test.go
type Addifier (line 5) | type Addifier interface
type Adder (line 7) | type Adder struct
method Add (line 10) | func (adder Adder) Add(a, b int32) int32 { return a + b }
function BenchmarkDirect (line 12) | func BenchmarkDirect(b *testing.B) {
function BenchmarkInterface (line 19) | func BenchmarkInterface(b *testing.B) {
function main (line 26) | func main() {}
FILE: chapter2_interfaces/iface.go
type Mather (line 3) | type Mather interface
type Adder (line 8) | type Adder struct
method Add (line 11) | func (adder Adder) Add(a, b int32) int32 { return a + b }
method Sub (line 14) | func (adder Adder) Sub(a, b int64) int64 { return a - b }
function main (line 16) | func main() {
FILE: chapter2_interfaces/iface_bench_test.go
type identifier (line 11) | type identifier interface
type id32 (line 16) | type id32 struct
method idInline (line 21) | func (id *id32) idInline() int32 { return id.id }
method idNoInline (line 24) | func (id *id32) idNoInline() int32 { return id.id }
constant _maxSize (line 28) | _maxSize = 2097152
constant _maxSizeModMask (line 29) | _maxSizeModMask = _maxSize - 1
function init (line 33) | func init() {
function escapeToHeap (line 52) | func escapeToHeap(id *id32) identifier {
function BenchmarkMethodCall_direct (line 57) | func BenchmarkMethodCall_direct(b *testing.B) {
function BenchmarkMethodCall_interface (line 135) | func BenchmarkMethodCall_interface(b *testing.B) {
function main (line 221) | func main() {}
FILE: chapter2_interfaces/iface_type_hash.go
type Mather (line 8) | type Mather interface
type Adder (line 13) | type Adder struct
method Add (line 16) | func (adder Adder) Add(a, b int32) int32 { return a + b }
method Sub (line 19) | func (adder Adder) Sub(a, b int64) int64 { return a - b }
function main (line 21) | func main() {
type iface (line 31) | type iface struct
type itab (line 35) | type itab struct
FILE: chapter2_interfaces/zerobase.go
function main (line 11) | func main() {
FILE: chapter2_interfaces/zeroval.go
type eface (line 11) | type eface struct
function main (line 13) | func main() {
Condensed preview — 24 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (187K chars).
[
{
"path": ".gitignore",
"chars": 306,
"preview": "# Compiled Object files, Static and Dynamic libs (Shared Objects)\n*.o\n*.a\n*.so\n\n*.gor\n\n# Folders\n_obj\n_test\n\n# Architect"
},
{
"path": "LICENSE",
"chars": 20840,
"preview": "Attribution-NonCommercial-ShareAlike 4.0 International\n\n================================================================"
},
{
"path": "Makefile",
"chars": 238,
"preview": ".PHONY: toc\n\ntoc:\n\tdocker run --rm -it -v ${PWD}:/usr/src jorgeandrada/doctoc --github\n\t$(shell tail -n +`grep -n '# \\`g"
},
{
"path": "README.md",
"chars": 1949,
"preview": "# `go-internals`\n\n`go-internals` is a work-in-progress book about the internals of the Go (1.10+) programming language.\n"
},
{
"path": "chapter1_assembly_primer/Makefile",
"chars": 333,
"preview": "GOOS=linux\nGOARCH=amd64\n\nSOURCES := $(wildcard *.go)\nOBJECTS = $(SOURCES:.go=.o)\nEXECUTABLES = $(OBJECTS:.o=.bin)\n\n.SECO"
},
{
"path": "chapter1_assembly_primer/README.md",
"chars": 24879,
"preview": "<!-- Copyright © 2018 Clement Rey <cr.rey.clement@gmail.com>. -->\n<!-- Licensed under the BY-NC-SA Creative Commons 4.0 "
},
{
"path": "chapter1_assembly_primer/direct_topfunc_call.go",
"chars": 115,
"preview": "package main\n\n//go:noinline\nfunc add(a, b int32) (int32, bool) { return a + b, true }\n\nfunc main() { add(10, 32) }\n"
},
{
"path": "chapter2_interfaces/Makefile",
"chars": 333,
"preview": "GOOS=linux\nGOARCH=amd64\n\nSOURCES := $(wildcard *.go)\nOBJECTS = $(SOURCES:.go=.o)\nEXECUTABLES = $(OBJECTS:.o=.bin)\n\n.SECO"
},
{
"path": "chapter2_interfaces/README.md",
"chars": 113293,
"preview": "<!-- Copyright © 2018 Clement Rey <cr.rey.clement@gmail.com>. -->\n<!-- Licensed under the BY-NC-SA Creative Commons 4.0 "
},
{
"path": "chapter2_interfaces/compound_interface.go",
"chars": 390,
"preview": "package main\n\ntype Adder interface{ Add(a, b int32) int32 }\ntype Subber interface{ Sub(a, b int32) int32 }\n\ntype Mather "
},
{
"path": "chapter2_interfaces/direct_calls.go",
"chars": 538,
"preview": "package main\n\n//go:noinline\nfunc Add(a, b int32) int32 { return a + b }\n\ntype Adder struct{ id int32 }\n\n//go:noinline\nfu"
},
{
"path": "chapter2_interfaces/dump_sym.sh",
"chars": 1028,
"preview": "#!/usr/bin/env bash\n\nBIN=\"$1\"\ntest \"$BIN\"\nSECTION=\"$2\"\ntest \"$SECTION\"\nSYM=\"$3\"\ntest \"$SYM\"\n\nsection_offset=$(\n readelf"
},
{
"path": "chapter2_interfaces/eface_scalar_test.go",
"chars": 2573,
"preview": "package main\n\nimport (\n\t\"testing\"\n)\n\nfunc BenchmarkEfaceScalar(b *testing.B) {\n\tvar Uint uint32\n\tb.Run(\"uint32\", func(b "
},
{
"path": "chapter2_interfaces/eface_to_type.go",
"chars": 3677,
"preview": "package main\n\nvar j uint32\nvar Eface interface{} // outsmart compiler (avoid static inference)\n\nfunc assertion() {\n\ti :="
},
{
"path": "chapter2_interfaces/eface_to_type_test.go",
"chars": 798,
"preview": "package main\n\nimport \"testing\"\n\nvar j uint32\nvar eface interface{} = uint32(42)\n\nfunc BenchmarkEfaceToType(b *testing.B)"
},
{
"path": "chapter2_interfaces/eface_type_hash.go",
"chars": 625,
"preview": "package main\n\nimport (\n\t\"fmt\"\n\t\"unsafe\"\n)\n\n// simplified definitions of runtime's eface & _type types\ntype eface struct "
},
{
"path": "chapter2_interfaces/escape.go",
"chars": 258,
"preview": "package main\n\ntype Addifier interface{ Add(a, b int32) int32 }\n\ntype Adder struct{ id int32 }\n\n//go:noinline\nfunc (adder"
},
{
"path": "chapter2_interfaces/escape_test.go",
"chars": 448,
"preview": "package main\n\nimport \"testing\"\n\ntype Addifier interface{ Add(a, b int32) int32 }\n\ntype Adder struct{ id int32 }\n\n//go:no"
},
{
"path": "chapter2_interfaces/iface.go",
"chars": 557,
"preview": "package main\n\ntype Mather interface {\n\tAdd(a, b int32) int32\n\tSub(a, b int64) int64\n}\n\ntype Adder struct{ id int32 }\n\n//"
},
{
"path": "chapter2_interfaces/iface_bench_test.go",
"chars": 5062,
"preview": "package main\n\nimport (\n\t\"math/rand\"\n\t\"runtime\"\n\t\"testing\"\n)\n\n// --------------------------------------------------------"
},
{
"path": "chapter2_interfaces/iface_type_hash.go",
"chars": 647,
"preview": "package main\n\nimport (\n\t\"fmt\"\n\t\"unsafe\"\n)\n\ntype Mather interface {\n\tAdd(a, b int32) int32\n\tSub(a, b int64) int64\n}\n\ntype"
},
{
"path": "chapter2_interfaces/zerobase.go",
"chars": 272,
"preview": "package main\n\nimport (\n\t\"fmt\"\n\t_ \"unsafe\"\n)\n\n//go:linkname zerobase runtime.zerobase\nvar zerobase uintptr\n\nfunc main() {"
},
{
"path": "chapter2_interfaces/zeroval.go",
"chars": 361,
"preview": "package main\n\nimport (\n\t\"fmt\"\n\t\"unsafe\"\n)\n\n//go:linkname zeroVal runtime.zeroVal\nvar zeroVal uintptr\n\ntype eface struct{"
},
{
"path": "chapter3_garbage_collector/README.md",
"chars": 161,
"preview": "<!-- Copyright © 2018 Clement Rey <cr.rey.clement@gmail.com>. -->\n<!-- Licensed under the BY-NC-SA Creative Commons 4.0 "
}
]
About this extraction
This page contains the full source code of the teh-cmc/go-internals GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 24 files (175.5 KB), approximately 50.8k tokens, and a symbol index with 60 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.