# Sync.Once vs Singleton Initialisation

**URL:** <https://forum.golangbridge.org/t/sync-once-vs-singleton-initialisation/33520>\
**Category:** Technical Discussion\
**Created:** [November 3, 2023, 7:32pm UTC](https://forum.golangbridge.org/t/sync-once-vs-singleton-initialisation/33520 "2023-11-03T19:32:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harshit\_Soni](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/harshit_soni/32/14238_2.png) [@Harshit\_Soni](https://forum.golangbridge.org/u/Harshit_Soni)\
**Post date:** [November 3, 2023, 7:32pm UTC](https://forum.golangbridge.org/t/sync-once-vs-singleton-initialisation/33520/1 "2023-11-03T19:32:56Z")

</div>

I have to get a single instance of DB. For this I wrote the following code -

```auto
func getDb() map[string]int {
	if db == nil {
		lock.Lock()
		if db == nil {
			db = map[string]int{}
		}
		lock.Unlock()
	}
	return db
}

```

There is another way to do this using Sync.Once -

```auto
func (o *Once) Do(f func()) {
	if atomic.LoadUint32(&o.done) == 0 {
		o.doSlow(f)
	}
}
func (o *Once) doSlow(f func()) {
	o.m.Lock()
	defer o.m.Unlock()
	if o.done == 0 {
		defer atomic.StoreUint32(&o.done, 1)
		f()
	}
}

```

Which one should I use ?  
Is the  
`atomic.LoadUint32(&o.done) == 0` computationally expensive than `db == nil` ?

---

<div class="post-metadata">

**Author:** ![Massimo.Costa](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/massimo.costa/32/9802_2.png) [@Massimo.Costa](https://forum.golangbridge.org/u/Massimo.Costa)\
**Post date:** [November 4, 2023, 12:10pm UTC](https://forum.golangbridge.org/t/sync-once-vs-singleton-initialisation/33520/2 "2023-11-04T12:10:45Z")

</div>

the two examples are not doing the same thing. Once is a ready to use solution while the first example is handmade. if have to do a lot of times …maybe better Once

---

<div class="post-metadata">

**Author:** ![Harshit\_Soni](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/harshit_soni/32/14238_2.png) [@Harshit\_Soni](https://forum.golangbridge.org/u/Harshit_Soni)\
**Post date:** [November 4, 2023, 6:10pm UTC](https://forum.golangbridge.org/t/sync-once-vs-singleton-initialisation/33520/3 "2023-11-04T18:10:58Z")

</div>

But, the benchmark tool suggests otherwise -

 ![Screenshot 2023-11-04 at 11.40.06 PM](https://forum.golangbridge.org/uploads/default/original/2X/a/aeaa011f395902615269ccf64e1b4359c36d74c7.png)

---

<div class="post-metadata">

**Author:** ![Massimo.Costa](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/massimo.costa/32/9802_2.png) [@Massimo.Costa](https://forum.golangbridge.org/u/Massimo.Costa)\
**Post date:** [November 4, 2023, 9:49pm UTC](https://forum.golangbridge.org/t/sync-once-vs-singleton-initialisation/33520/4 "2023-11-04T21:49:28Z")

</div>

well, if for your use case 130ns are critical … use the first solution, otherwise I prefer to use “ready-to-use” solution; but it’s just my idea

---

<div class="post-metadata">

**Author:** ![telo\_tade](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/telo_tade/32/8433_2.png) [@telo\_tade](https://forum.golangbridge.org/u/telo_tade)\
**Post date:** [November 4, 2023, 9:52pm UTC](https://forum.golangbridge.org/t/sync-once-vs-singleton-initialisation/33520/5 "2023-11-04T21:52:44Z")

</div>

> [@Harshit\_Soni](#):
>
> enchmark tool suggests oth

Hi @Harshit_Soni ,

Use sync.Once to create singletons, that is the idiomatic Golang way.

Why do you care how fast is this trivial and rarely used operation ? You usually only need to create a connection to DB only once in your application, why do you care if it takes 10% longer ? If a code is easier to read (maintain) it is usually worth to choose the worse algorithm.

Your benchmark shows only a very small difference, and may even show the opposite if you run it for a longer time. Seems like an easy choice to go for sync.Once

---

<div class="post-metadata">

**Author:** ![Dean\_Davidson](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dean_davidson/32/9685_2.png) [@Dean\_Davidson](https://forum.golangbridge.org/u/Dean_Davidson)\
**Post date:** [November 4, 2023, 11:37pm UTC](https://forum.golangbridge.org/t/sync-once-vs-singleton-initialisation/33520/6 "2023-11-04T23:37:14Z")

</div>

> [@Massimo.Costa](#):
>
> well, if for your use case 130ns are critical … use the first solution, otherwise I prefer to use “ready-to-use” solution; but it’s just my idea

Right. If I’m not mistaken, we are talking about 0.00000013 seconds on an operation that, by definition, is going to be performed only once. Depending on the use case, this might also be a good candidate for an [init function](https://www.digitalocean.com/community/tutorials/understanding-init-in-go). That way you can take concurrency out of the picture completely and this `db` property is just there when your app starts.

Atomics are low-level memory primitives and are pretty performant based on my usage/understanding of them. I’m wondering if that very slight performance gap is caused by the `defer o.m.Unlock()`? Also perhaps there’s very slight overhead from having multiple functions / a closure. Either way, it doesn’t matter because I can guarantee you that connecting to your database and authenticating is going to take orders of magnitude longer than any performance hit you might incur by using `sync.Once`.

---

<div class="post-metadata">

**Author:** ![Harshit\_Soni](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/harshit_soni/32/14238_2.png) [@Harshit\_Soni](https://forum.golangbridge.org/u/Harshit_Soni)\
**Post date:** [November 5, 2023, 2:05am UTC](https://forum.golangbridge.org/t/sync-once-vs-singleton-initialisation/33520/7 "2023-11-05T02:05:44Z")

</div>

Thanks a lot everyone,  
got my answer. 😄

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [February 3, 2024, 2:06am UTC](https://forum.golangbridge.org/t/sync-once-vs-singleton-initialisation/33520/8 "2024-02-03T02:06:39Z")

</div>

Closed on February 3, 2024, 2:06am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
