Go: Are pointers a performance optimization?

What are pointers?

How could using a pointer be an optimization?

Can pointers negatively affect performance?

  1. Dereferencing pointers isn’t free. It’s not a huge cost, but it can add up.
  2. Sharing data via pointers will likely cause the data to be placed in the “heap.” The heap is a section of memory for data that lives longer than a single function call. There is overhead to adding data to the heap and heap data can only be cleaned up by the garbage collector. The more data in the heap, the more work the garbage collector has to do, and the more impact it’ll have on your application.

Stack vs Heap

Stack: Function-local memory

Heap: Area for shared data

  1. Placing data in the heap requires asking for memory from the runtime. Again, not a huge overhead, but not free either.
  2. If there’s not already enough heap space, the runtime will have to ask for additional memory from the OS, which is additional overhead.
  3. Once a value has been placed in the heap it needs to stay there until no functions have a pointer to it anymore. When there are no more pointers to the data it needs to be cleaned up. In Go, this is the job of the garbage collector. It has to find all the unreferenced values and mark their space in the heap as free. The more values placed in the heap, the more work the garbage collector has to do, and the more potential there is to impact your application.

So why use pointers?

Conclusion

Notes:

  • I intentionally simplified many concepts in this article to keep it relatively short and approachable.
  • A few of the related concepts I did not cover include: escape analysis, interface conversions, function inlining, stack growth.
  • Many types, such as slices, strings, and maps, contain pointers to underlying data, passing pointers to these types rarely makes sense.

Additional Reading

--

--

--

I pretend to know stuff about things.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Introduction to Git Workflow

VIPER Design Pattern — Part 2

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

Enable Memory and Disk Utilization Metric for Ubuntu Instance on AWS

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

Real Time Notification System with Sidekiq, Redis and Devise in Rails 6

How to Setup Flutter in Ubuntu 21.10

Why Spark scores when it comes to streaming Data pipeline…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kale Blankenship

Kale Blankenship

I pretend to know stuff about things.

More from Medium

Golang with Leetcode: Remove Nth Node From End of List

Complete guide on Golang Control Structure

Details of Go JSON serialization of []byte type

What are GOROOT and GOPATH in Go?

Golang image with gopher