Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Reduce arenamanager allocations#73

Merged
mangalaman93 merged 7 commits intograndecola:mainfrom
moredure:patch-1
Apr 23, 2021
Merged

Reduce arenamanager allocations#73
mangalaman93 merged 7 commits intograndecola:mainfrom
moredure:patch-1

Conversation

@moredure
Copy link
Contributor

@moredure moredure commented Apr 19, 2021

Reduce memory allocation with same path behaiviour, clean dir once instead of doing it each time.

As an alternative we can do

var b strings.Builder
a := strconv.Itoa(i)
b.Grow(len(dir) + 1 + len(a) + len(cArenaFilePrefix))
b.WriteString(dir)
b.WriteByte('/')
b.WriteString(a)
b.WriteString(cArenaFilePrefix)

but it's slightly slower

func BenchmarkSpeeds(b *testing.B) {
	b.Run("compare speeds", func(b *testing.B) {
		var dir = "/home/user/datadir"
		var p = ""
		b.Run("builder", func(b *testing.B) {
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				var b strings.Builder
                                a := strconv.Itoa(i)
				b.Grow(len(dir) + 1 + len(a) + len("_arena.dat"))
				b.WriteString(dir)
				b.WriteByte('/')
				b.WriteString(a)
				b.WriteString("_arena.dat")
				p = b.String()
			}
		})
		b.Run("slice", func(b *testing.B) {
			var filePath []byte
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				filePath = append(filePath[:0], dir...)
				filePath = append(filePath, '/')
				filePath = strconv.AppendInt(filePath, int64(i), 10)
				filePath = append(filePath, "_arena.dat"...)
				p = string(filePath)
			}
		})
		b.Run("path", func(b *testing.B) {
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				fileName := strconv.Itoa(i) + "_arena.dat"
				p = path.Join(dir, fileName)
			}
		})
		runtime.KeepAlive(p)
	})
}
cpu: Intel(R) Core(TM) i5-7600 CPU @ 3.50GHz
BenchmarkSpeeds
BenchmarkSpeeds/compare_speeds
BenchmarkSpeeds/compare_speeds/builder
BenchmarkSpeeds/compare_speeds/builder-4 	15500703	        76.11 ns/op	      55 B/op	       1 allocs/op
BenchmarkSpeeds/compare_speeds/slice
BenchmarkSpeeds/compare_speeds/slice-4   	23539419	        56.07 ns/op	      47 B/op	       1 allocs/op
BenchmarkSpeeds/compare_speeds/path
BenchmarkSpeeds/compare_speeds/path-4    	 5428278	       203.3 ns/op	     103 B/op	       2 allocs/op

@mangalaman93, @ashish-goswami please take a look!

@codecov
Copy link

codecov bot commented Apr 19, 2021

Codecov Report

❗ No coverage uploaded for pull request base (main@dcd5305). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main      #73   +/-   ##
=======================================
  Coverage        ?   79.60%           
=======================================
  Files           ?       10           
  Lines           ?      402           
  Branches        ?        0           
=======================================
  Hits            ?      320           
  Misses          ?       42           
  Partials        ?       40           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dcd5305...9fac3db. Read the comment docs.

@moredure moredure changed the title Update arenamanager.go Reduce arenamanager.go allocations Apr 19, 2021
@moredure moredure changed the title Reduce arenamanager.go allocations Reduce arenamanager allocations and improve performance Apr 19, 2021
@moredure moredure changed the title Reduce arenamanager allocations and improve performance Reduce arenamanager allocations Apr 19, 2021
Copy link
Member

@mangalaman93 mangalaman93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. Just one comment, thanks for the benchmark. It would be fine to have the benchmark in the repo too. No big deal.

@qlty-cloud-legacy
Copy link

qlty-cloud-legacy bot commented Apr 22, 2021

Code Climate has analyzed commit 9fac3db and detected 0 issues on this pull request.

View more on Code Climate.

@mangalaman93
Copy link
Member

Waiting for build to pass before we can merge it. Could be related to #78.

@mangalaman93 mangalaman93 merged commit b7aa817 into grandecola:main Apr 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants