SlideShare a Scribd company logo
Move From C to Go
Cherie Hsieh
1
Move From C to Go
Golang Taiwan, Gopher Taipei
2
7056 members
Move From C to Go
Go Turns 10
3
10 years of Go!!
Move From C to Go
Go Turns 10
4
Special stickers for
gophers in Taiwan
Move From C to Go
Beginning
5
Robert Griesemer Rob Pike Ken Thompson
The designers of Go language.
Move From C to Go
Beginning
6
Move From C to Go
The Goals of Go
7
Go was designed by and for people who write—and read and debug and
maintain—large software systems.
ELIMINATE
SLOWNESS
ELIMINATE
CLUMSINESS
IMPROVE
EFFECTIVENESS
IMPROVE
SCALE
Move From C to Go
The Future of Go
8
Image is from GMOM- Go Slide Template - 10th Birthday
Memory Allocation
9
Move From C to Go
Move From C to Go
Memory Allocation
10
void numbers()
{
int numbers[50];
}
void numbers()
{
int *numbers = malloc(sizeof(int) * 50);
}
Memory allocation in C.
Stack
Heap
Move From C to Go
Memory Allocation
11
func Numbers() {
var numbers [50]int
}
Memory allocation in Go.
Stack
func GetNumbers() [50]int {
var numbers [50]int
return numbers
}
func main() {
n := GetNumbers()
}
Stack
Stack
GetNumbers stack frame
Main stack frame
Move From C to Go
Memory Allocation
12
Go's arrays are values.
An array variable denotes the entire array.
It is not a pointer to the first array element like C.
Read More: Go Slices: usage and internals
Move From C to Go
Memory Allocation
13
func GetNumbers() {
numbers := new([50]int)
}
Memory allocation in Go.
Stack
new(T) allocates zeroed storage for a new item of type T and returns its address, a value
of type *T.
The variable may not be allocated in heap like C/C++.
Read More: Effective Go - Allocation with New
Move From C to Go
Memory Allocation
14
func GetNumbers() *[50]int {
numbers := new([50]int)
return numbers
}
func main() {
n := GetNumbers()
n[0] = 1
}
Memory allocation in Go.
Heap
Move From C to Go
Memory Allocation
15
How do I know whether a variable is allocated on the heap or the stack?
In the current compilers, if a variable has its address taken, that variable is a candidate
for allocation on the heap. However, a basic escape analysis recognizes some cases
when such variables will not live past the return from the function and can reside on the
stack.
From Frequently Asked Questions (FAQ)
Read More: Escape-Analysis Flaws
Move From C to Go
Memory Allocation
16
1. Do we need to change the underlying value?
2. Should the value be nil?
The basic rules of passing by pointers or values
Read More: Allocation efficiency in high-performance Go services
Move From C to Go
Memory Allocation
17
func GetNumbers() [50]int {
var numbers [50]int
return numbers
}
func main() {
n := GetNumbers()
}
Stack
Stack
The address is not in the system stack
Move From C to Go
Memory Allocation
18
Memory allocator in Go (A branch of TCMalloc).
Move From C to Go
Memory Allocation
19
Memory allocator in Go.
User space thread
Move From C to Go
Memory Allocation
20
Memory allocator in Go.
User space thread
Move From C to Go
Memory Allocation
21
To prevent the fragmentation problem on 32-bit platform, Go will make a large initial
reservation of virtual memory (~258MB).
Note
Read More:
1. A visual guide to Go Memory Allocator from scratch
2. Go’s Memory Allocator - Overview
Move From C to Go
Memory Allocation
22
1. Use local variables (copy by value)
2. For large cache libraries, they use mmap (or some tricky ways) to avoid the GC
scan. (e.g. bigCache library).
The practices to reduce the stress of Garbage Collection
Read More: Writing a very fast cache service with millions of entries in Go
Move From C to Go
Memory Allocation
23
More about the goroutine stack
resizeable goroutine stack (continuous stack)
Read More: Contiguous stacks in Go
Question: How can we copy the stack to a new one without losing pointers?
Go Toolchain
24
Move From C to Go
Move From C to Go
Go Toolchain
25
Support five major architectures
1. x86
2. ARM
3. PowerPC
4. MIPS
5. IBM
6. WebAssembly
- GC (Go compiler. Generate an object file for the Go linker)
- Go Linker
Move From C to Go
Go Toolchain
26
gccgo: the front-end of gcc compiler
- support most of gcc compiler options.
An alternative compiler
Front-end
GCCGO
Mid-end
GCC
Back-end
GCC
Analyzing the source code to
build an internal representation
of the program.
performs optimizations on the
intermediate representation.
Responsible for the CPU
a r c h i t e c t u r e s p e c i fi c
optimizations and for  code
generation.
Move From C to Go
Go Toolchain
27
Performance Comparison 

GCCGO still yielding (much) slower code than the standard compiler.
(GCCGO in 2019 https://meltware.com/2019/01/16/gccgo-benchmarks-2019.html)
Why use GCCGO?
Brief overview of gccgo, "the other" Go compiler
Move From C to Go
Go Toolchain
28
More about Go linker
This approach is different from the less conventional gc compiler, which does
significant code generation work at link time.
The Go runtime needs to know where all the pointers are in the data and bss
segments, and a standard linker won't put that information together for you. The
Go linker does that. The Go linker also writes out runtime information for
unwinding stacks during GC and translating program counters to file:line for use
in stack dumps. Again, this is all much easier to do with a toolchain that we control
entirely than having to build into existing ones.
Building a better Go linker (2019/09)
(https://docs.google.com/document/d/1D13QhciikbdLtaI67U6Ble5d_1nsI4befEd6_k1z91U/mobilebasic)
User-Space Scheduler in Go
29
Move From C to Go
Move From C to Go
User-Space Scheduler in Go
30
Thread Models
- NPTL C Library
1:1 (kernel-level threading)
- Go
M:N scheduling
M
P
(processor)
G2
G3
G4
Local Runnable
Queue
Kernel Thread
G1
Execute
Move From C to Go
User-Space Scheduler in Go
31
M
P
(processor)
G2
G3
G4
Local Runnable
Queue
Execute
31
Context Switch
semtable
G1
Sleep
Wake up
- Generally, there is no system call
being invoked during rescheduling
- Data Locality for Processor
Move From C to Go
User-Space Scheduler in Go
32
M
P1
(processor)
G1
Execute
32
Create a New Goroutine
G2
Local Runnable
Queue
(1) Add a new goroutine to LRQ
Global Queue
(2) Move half of tasks from a local queue to the global
runnable queue if the local queue full.
M
P2
(processor)
Steal
Grab
Move From C to Go
User-Space Scheduler in Go
Note
- The current implementation of the Go scheduler is not a preemptive scheduler
but a cooperating scheduler.
(go 1.14 may release non-cooperative goroutine preemption)
- The scheduler needs well-defined user space events that happen at safe points
in the code to make scheduling decisions.
Enjoy your developer life with Go
34

More Related Content

What's hot

Customize and Secure the Runtime and Dependencies of Your Procedural Language...
Customize and Secure the Runtime and Dependencies of Your Procedural Language...Customize and Secure the Runtime and Dependencies of Your Procedural Language...
Customize and Secure the Runtime and Dependencies of Your Procedural Language...
VMware Tanzu
 
CMake best practices
CMake best practicesCMake best practices
CMake best practices
Henry Schreiner
 
PEARC17: Modernizing GooFit: A Case Study
PEARC17: Modernizing GooFit: A Case StudyPEARC17: Modernizing GooFit: A Case Study
PEARC17: Modernizing GooFit: A Case Study
Henry Schreiner
 
Streams for the Web
Streams for the WebStreams for the Web
Streams for the Web
Domenic Denicola
 
Debugging CUDA applications
Debugging CUDA applicationsDebugging CUDA applications
Debugging CUDA applications
Rogue Wave Software
 
Next Generation Indexes For Big Data Engineering (ODSC East 2018)
Next Generation Indexes For Big Data Engineering (ODSC East 2018)Next Generation Indexes For Big Data Engineering (ODSC East 2018)
Next Generation Indexes For Big Data Engineering (ODSC East 2018)
Daniel Lemire
 
Exploring the Programming Models for the LUMI Supercomputer
Exploring the Programming Models for the LUMI Supercomputer Exploring the Programming Models for the LUMI Supercomputer
Exploring the Programming Models for the LUMI Supercomputer
George Markomanolis
 
Developing High Performance Application with Aerospike & Go
Developing High Performance Application with Aerospike & GoDeveloping High Performance Application with Aerospike & Go
Developing High Performance Application with Aerospike & Go
Chris Stivers
 
Xdp and ebpf_maps
Xdp and ebpf_mapsXdp and ebpf_maps
Xdp and ebpf_maps
lcplcp1
 
Upgrading To The New Map Reduce API
Upgrading To The New Map Reduce APIUpgrading To The New Map Reduce API
Upgrading To The New Map Reduce API
Tom Croucher
 
PyPy - is it ready for production
PyPy - is it ready for productionPyPy - is it ready for production
PyPy - is it ready for production
Mark Rees
 
Digital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meetingDigital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meeting
Henry Schreiner
 
Scheduling in Linux and Web Servers
Scheduling in Linux and Web ServersScheduling in Linux and Web Servers
Scheduling in Linux and Web Servers
David Evans
 
Linux Kernel 개발참여방법과 문화 (Contribution)
Linux Kernel 개발참여방법과 문화 (Contribution)Linux Kernel 개발참여방법과 문화 (Contribution)
Linux Kernel 개발참여방법과 문화 (Contribution)
Ubuntu Korea Community
 
The Quantum Physics of Java
The Quantum Physics of JavaThe Quantum Physics of Java
The Quantum Physics of Java
Michael Heinrichs
 
Improving go-git performance
Improving go-git performanceImproving go-git performance
Improving go-git performance
source{d}
 
PyHEP 2019: Python 3.8
PyHEP 2019: Python 3.8PyHEP 2019: Python 3.8
PyHEP 2019: Python 3.8
Henry Schreiner
 
Intro2 Cuda Moayad
Intro2 Cuda MoayadIntro2 Cuda Moayad
Intro2 Cuda Moayad
Moayadhn
 
20210928_pgunconf_hll_count
20210928_pgunconf_hll_count20210928_pgunconf_hll_count
20210928_pgunconf_hll_count
Kohei KaiGai
 
The future is CSN
The future is CSNThe future is CSN
The future is CSN
Alexander Korotkov
 

What's hot (20)

Customize and Secure the Runtime and Dependencies of Your Procedural Language...
Customize and Secure the Runtime and Dependencies of Your Procedural Language...Customize and Secure the Runtime and Dependencies of Your Procedural Language...
Customize and Secure the Runtime and Dependencies of Your Procedural Language...
 
CMake best practices
CMake best practicesCMake best practices
CMake best practices
 
PEARC17: Modernizing GooFit: A Case Study
PEARC17: Modernizing GooFit: A Case StudyPEARC17: Modernizing GooFit: A Case Study
PEARC17: Modernizing GooFit: A Case Study
 
Streams for the Web
Streams for the WebStreams for the Web
Streams for the Web
 
Debugging CUDA applications
Debugging CUDA applicationsDebugging CUDA applications
Debugging CUDA applications
 
Next Generation Indexes For Big Data Engineering (ODSC East 2018)
Next Generation Indexes For Big Data Engineering (ODSC East 2018)Next Generation Indexes For Big Data Engineering (ODSC East 2018)
Next Generation Indexes For Big Data Engineering (ODSC East 2018)
 
Exploring the Programming Models for the LUMI Supercomputer
Exploring the Programming Models for the LUMI Supercomputer Exploring the Programming Models for the LUMI Supercomputer
Exploring the Programming Models for the LUMI Supercomputer
 
Developing High Performance Application with Aerospike & Go
Developing High Performance Application with Aerospike & GoDeveloping High Performance Application with Aerospike & Go
Developing High Performance Application with Aerospike & Go
 
Xdp and ebpf_maps
Xdp and ebpf_mapsXdp and ebpf_maps
Xdp and ebpf_maps
 
Upgrading To The New Map Reduce API
Upgrading To The New Map Reduce APIUpgrading To The New Map Reduce API
Upgrading To The New Map Reduce API
 
PyPy - is it ready for production
PyPy - is it ready for productionPyPy - is it ready for production
PyPy - is it ready for production
 
Digital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meetingDigital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meeting
 
Scheduling in Linux and Web Servers
Scheduling in Linux and Web ServersScheduling in Linux and Web Servers
Scheduling in Linux and Web Servers
 
Linux Kernel 개발참여방법과 문화 (Contribution)
Linux Kernel 개발참여방법과 문화 (Contribution)Linux Kernel 개발참여방법과 문화 (Contribution)
Linux Kernel 개발참여방법과 문화 (Contribution)
 
The Quantum Physics of Java
The Quantum Physics of JavaThe Quantum Physics of Java
The Quantum Physics of Java
 
Improving go-git performance
Improving go-git performanceImproving go-git performance
Improving go-git performance
 
PyHEP 2019: Python 3.8
PyHEP 2019: Python 3.8PyHEP 2019: Python 3.8
PyHEP 2019: Python 3.8
 
Intro2 Cuda Moayad
Intro2 Cuda MoayadIntro2 Cuda Moayad
Intro2 Cuda Moayad
 
20210928_pgunconf_hll_count
20210928_pgunconf_hll_count20210928_pgunconf_hll_count
20210928_pgunconf_hll_count
 
The future is CSN
The future is CSNThe future is CSN
The future is CSN
 

Similar to Move from C to Go

Go. why it goes v2
Go. why it goes v2Go. why it goes v2
Go. why it goes v2
Sergey Pichkurov
 
Python VS GO
Python VS GOPython VS GO
Python VS GO
Ofir Nir
 
Introduction to Big Data
Introduction to Big DataIntroduction to Big Data
Introduction to Big Data
Albert Bifet
 
不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)
Douglas Chen
 
Generative AI for Reengineering Variants into Software Product Lines: An Expe...
Generative AI for Reengineering Variants into Software Product Lines: An Expe...Generative AI for Reengineering Variants into Software Product Lines: An Expe...
Generative AI for Reengineering Variants into Software Product Lines: An Expe...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
Two C++ Tools: Compiler Explorer and Cpp Insights
Two C++ Tools: Compiler Explorer and Cpp InsightsTwo C++ Tools: Compiler Explorer and Cpp Insights
Two C++ Tools: Compiler Explorer and Cpp Insights
Alison Chaiken
 
Gopher in performance_tales_ms_go_cracow
Gopher in performance_tales_ms_go_cracowGopher in performance_tales_ms_go_cracow
Gopher in performance_tales_ms_go_cracow
MateuszSzczyrzyca
 
ICT C++
ICT C++ ICT C++
ICT C++
Karthikeyan A K
 
C interview questions
C interview questionsC interview questions
C interview questions
Soba Arjun
 
lecture16-recap-questions-and-answers.pdf
lecture16-recap-questions-and-answers.pdflecture16-recap-questions-and-answers.pdf
lecture16-recap-questions-and-answers.pdf
AyushKumar93531
 
Keynote (Mike Muller) - Is There Anything New in Heterogeneous Computing - by...
Keynote (Mike Muller) - Is There Anything New in Heterogeneous Computing - by...Keynote (Mike Muller) - Is There Anything New in Heterogeneous Computing - by...
Keynote (Mike Muller) - Is There Anything New in Heterogeneous Computing - by...
AMD Developer Central
 
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
corehard_by
 
Why Go Lang?
Why Go Lang?Why Go Lang?
Why Go Lang?
Sathish VJ
 
FluentMigrator - Dayton .NET - July 2023
FluentMigrator - Dayton .NET - July 2023FluentMigrator - Dayton .NET - July 2023
FluentMigrator - Dayton .NET - July 2023
Matthew Groves
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io
 
Virtual Flink Forward 2020: A deep dive into Flink SQL - Jark Wu
Virtual Flink Forward 2020: A deep dive into Flink SQL - Jark WuVirtual Flink Forward 2020: A deep dive into Flink SQL - Jark Wu
Virtual Flink Forward 2020: A deep dive into Flink SQL - Jark Wu
Flink Forward
 
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization ApproachesPragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Marina Kolpakova
 
Low pause GC in HotSpot
Low pause GC in HotSpotLow pause GC in HotSpot
Low pause GC in HotSpot
jClarity
 
Golang workshop - Mindbowser
Golang workshop - MindbowserGolang workshop - Mindbowser
Golang workshop - Mindbowser
Mindbowser Inc
 

Similar to Move from C to Go (20)

Go. why it goes v2
Go. why it goes v2Go. why it goes v2
Go. why it goes v2
 
Python VS GO
Python VS GOPython VS GO
Python VS GO
 
Introduction to Big Data
Introduction to Big DataIntroduction to Big Data
Introduction to Big Data
 
不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)
 
Generative AI for Reengineering Variants into Software Product Lines: An Expe...
Generative AI for Reengineering Variants into Software Product Lines: An Expe...Generative AI for Reengineering Variants into Software Product Lines: An Expe...
Generative AI for Reengineering Variants into Software Product Lines: An Expe...
 
Two C++ Tools: Compiler Explorer and Cpp Insights
Two C++ Tools: Compiler Explorer and Cpp InsightsTwo C++ Tools: Compiler Explorer and Cpp Insights
Two C++ Tools: Compiler Explorer and Cpp Insights
 
Gopher in performance_tales_ms_go_cracow
Gopher in performance_tales_ms_go_cracowGopher in performance_tales_ms_go_cracow
Gopher in performance_tales_ms_go_cracow
 
ICT C++
ICT C++ ICT C++
ICT C++
 
C interview questions
C interview questionsC interview questions
C interview questions
 
lecture16-recap-questions-and-answers.pdf
lecture16-recap-questions-and-answers.pdflecture16-recap-questions-and-answers.pdf
lecture16-recap-questions-and-answers.pdf
 
Keynote (Mike Muller) - Is There Anything New in Heterogeneous Computing - by...
Keynote (Mike Muller) - Is There Anything New in Heterogeneous Computing - by...Keynote (Mike Muller) - Is There Anything New in Heterogeneous Computing - by...
Keynote (Mike Muller) - Is There Anything New in Heterogeneous Computing - by...
 
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
 
Why Go Lang?
Why Go Lang?Why Go Lang?
Why Go Lang?
 
FluentMigrator - Dayton .NET - July 2023
FluentMigrator - Dayton .NET - July 2023FluentMigrator - Dayton .NET - July 2023
FluentMigrator - Dayton .NET - July 2023
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
 
Virtual Flink Forward 2020: A deep dive into Flink SQL - Jark Wu
Virtual Flink Forward 2020: A deep dive into Flink SQL - Jark WuVirtual Flink Forward 2020: A deep dive into Flink SQL - Jark Wu
Virtual Flink Forward 2020: A deep dive into Flink SQL - Jark Wu
 
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization ApproachesPragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
 
Low pause GC in HotSpot
Low pause GC in HotSpotLow pause GC in HotSpot
Low pause GC in HotSpot
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
 
Golang workshop - Mindbowser
Golang workshop - MindbowserGolang workshop - Mindbowser
Golang workshop - Mindbowser
 

Recently uploaded

Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 

Recently uploaded (20)

Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 

Move from C to Go

  • 1. Move From C to Go Cherie Hsieh 1
  • 2. Move From C to Go Golang Taiwan, Gopher Taipei 2 7056 members
  • 3. Move From C to Go Go Turns 10 3 10 years of Go!!
  • 4. Move From C to Go Go Turns 10 4 Special stickers for gophers in Taiwan
  • 5. Move From C to Go Beginning 5 Robert Griesemer Rob Pike Ken Thompson The designers of Go language.
  • 6. Move From C to Go Beginning 6
  • 7. Move From C to Go The Goals of Go 7 Go was designed by and for people who write—and read and debug and maintain—large software systems. ELIMINATE SLOWNESS ELIMINATE CLUMSINESS IMPROVE EFFECTIVENESS IMPROVE SCALE
  • 8. Move From C to Go The Future of Go 8 Image is from GMOM- Go Slide Template - 10th Birthday
  • 10. Move From C to Go Memory Allocation 10 void numbers() { int numbers[50]; } void numbers() { int *numbers = malloc(sizeof(int) * 50); } Memory allocation in C. Stack Heap
  • 11. Move From C to Go Memory Allocation 11 func Numbers() { var numbers [50]int } Memory allocation in Go. Stack func GetNumbers() [50]int { var numbers [50]int return numbers } func main() { n := GetNumbers() } Stack Stack GetNumbers stack frame Main stack frame
  • 12. Move From C to Go Memory Allocation 12 Go's arrays are values. An array variable denotes the entire array. It is not a pointer to the first array element like C. Read More: Go Slices: usage and internals
  • 13. Move From C to Go Memory Allocation 13 func GetNumbers() { numbers := new([50]int) } Memory allocation in Go. Stack new(T) allocates zeroed storage for a new item of type T and returns its address, a value of type *T. The variable may not be allocated in heap like C/C++. Read More: Effective Go - Allocation with New
  • 14. Move From C to Go Memory Allocation 14 func GetNumbers() *[50]int { numbers := new([50]int) return numbers } func main() { n := GetNumbers() n[0] = 1 } Memory allocation in Go. Heap
  • 15. Move From C to Go Memory Allocation 15 How do I know whether a variable is allocated on the heap or the stack? In the current compilers, if a variable has its address taken, that variable is a candidate for allocation on the heap. However, a basic escape analysis recognizes some cases when such variables will not live past the return from the function and can reside on the stack. From Frequently Asked Questions (FAQ) Read More: Escape-Analysis Flaws
  • 16. Move From C to Go Memory Allocation 16 1. Do we need to change the underlying value? 2. Should the value be nil? The basic rules of passing by pointers or values Read More: Allocation efficiency in high-performance Go services
  • 17. Move From C to Go Memory Allocation 17 func GetNumbers() [50]int { var numbers [50]int return numbers } func main() { n := GetNumbers() } Stack Stack The address is not in the system stack
  • 18. Move From C to Go Memory Allocation 18 Memory allocator in Go (A branch of TCMalloc).
  • 19. Move From C to Go Memory Allocation 19 Memory allocator in Go. User space thread
  • 20. Move From C to Go Memory Allocation 20 Memory allocator in Go. User space thread
  • 21. Move From C to Go Memory Allocation 21 To prevent the fragmentation problem on 32-bit platform, Go will make a large initial reservation of virtual memory (~258MB). Note Read More: 1. A visual guide to Go Memory Allocator from scratch 2. Go’s Memory Allocator - Overview
  • 22. Move From C to Go Memory Allocation 22 1. Use local variables (copy by value) 2. For large cache libraries, they use mmap (or some tricky ways) to avoid the GC scan. (e.g. bigCache library). The practices to reduce the stress of Garbage Collection Read More: Writing a very fast cache service with millions of entries in Go
  • 23. Move From C to Go Memory Allocation 23 More about the goroutine stack resizeable goroutine stack (continuous stack) Read More: Contiguous stacks in Go Question: How can we copy the stack to a new one without losing pointers?
  • 25. Move From C to Go Go Toolchain 25 Support five major architectures 1. x86 2. ARM 3. PowerPC 4. MIPS 5. IBM 6. WebAssembly - GC (Go compiler. Generate an object file for the Go linker) - Go Linker
  • 26. Move From C to Go Go Toolchain 26 gccgo: the front-end of gcc compiler - support most of gcc compiler options. An alternative compiler Front-end GCCGO Mid-end GCC Back-end GCC Analyzing the source code to build an internal representation of the program. performs optimizations on the intermediate representation. Responsible for the CPU a r c h i t e c t u r e s p e c i fi c optimizations and for  code generation.
  • 27. Move From C to Go Go Toolchain 27 Performance Comparison GCCGO still yielding (much) slower code than the standard compiler. (GCCGO in 2019 https://meltware.com/2019/01/16/gccgo-benchmarks-2019.html) Why use GCCGO? Brief overview of gccgo, "the other" Go compiler
  • 28. Move From C to Go Go Toolchain 28 More about Go linker This approach is different from the less conventional gc compiler, which does significant code generation work at link time. The Go runtime needs to know where all the pointers are in the data and bss segments, and a standard linker won't put that information together for you. The Go linker does that. The Go linker also writes out runtime information for unwinding stacks during GC and translating program counters to file:line for use in stack dumps. Again, this is all much easier to do with a toolchain that we control entirely than having to build into existing ones. Building a better Go linker (2019/09) (https://docs.google.com/document/d/1D13QhciikbdLtaI67U6Ble5d_1nsI4befEd6_k1z91U/mobilebasic)
  • 29. User-Space Scheduler in Go 29 Move From C to Go
  • 30. Move From C to Go User-Space Scheduler in Go 30 Thread Models - NPTL C Library 1:1 (kernel-level threading) - Go M:N scheduling M P (processor) G2 G3 G4 Local Runnable Queue Kernel Thread G1 Execute
  • 31. Move From C to Go User-Space Scheduler in Go 31 M P (processor) G2 G3 G4 Local Runnable Queue Execute 31 Context Switch semtable G1 Sleep Wake up - Generally, there is no system call being invoked during rescheduling - Data Locality for Processor
  • 32. Move From C to Go User-Space Scheduler in Go 32 M P1 (processor) G1 Execute 32 Create a New Goroutine G2 Local Runnable Queue (1) Add a new goroutine to LRQ Global Queue (2) Move half of tasks from a local queue to the global runnable queue if the local queue full. M P2 (processor) Steal Grab
  • 33. Move From C to Go User-Space Scheduler in Go Note - The current implementation of the Go scheduler is not a preemptive scheduler but a cooperating scheduler. (go 1.14 may release non-cooperative goroutine preemption) - The scheduler needs well-defined user space events that happen at safe points in the code to make scheduling decisions.
  • 34. Enjoy your developer life with Go 34