Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save log waste and tag caching #8

Merged
merged 3 commits into from
May 3, 2024
Merged

save log waste and tag caching #8

merged 3 commits into from
May 3, 2024

Conversation

suhuaqin
Copy link
Contributor

@suhuaqin suhuaqin commented May 3, 2024

Hi, here's a merge request on performance optimization with two main changes:

  1. save log consumption when log printing is not needed
  2. cache tag parsing results

Both of these changes are very simple, here is my benchmark code and the results:

package ngap

import (
	"github.com/free5gc/ngap/ngapType"
	"testing"
)

var by = []byte{0, 14, 0, 128, 168, 0, 0, 9, 0, 10, 0, 5, 96, 248, 4, 128, 182, 0, 85, 0, 3, 64, 48, 57, 0, 18, 64, 10, 3, 255, 5, 0, 100, 240, 0, 0, 0, 1, 0, 28, 0, 7, 0, 100, 240, 0, 1, 0, 65, 0, 0, 0, 5, 2, 1, 1, 1, 1, 0, 119, 0, 9, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 94, 0, 32, 24, 215, 2, 95, 204, 253, 169, 14, 166, 251, 21, 39, 237, 201, 125, 92, 17, 166, 65, 16, 192, 153, 171, 157, 76, 45, 186, 236, 224, 124, 7, 35, 0, 31, 64, 2, 0, 0, 0, 38, 64, 56, 55, 126, 2, 110, 230, 241, 215, 1, 126, 0, 66, 1, 1, 119, 0, 11, 242, 100, 240, 0, 1, 0, 65, 192, 22, 70, 161, 84, 7, 64, 100, 240, 0, 0, 0, 1, 21, 5, 4, 1, 1, 1, 1, 49, 5, 4, 1, 1, 1, 1, 33, 1, 1, 94, 1, 5}

var pdu *ngapType.NGAPPDU

func TestMain(m *testing.M) {
	var err error
	pdu, err = Decoder(by)
	if err != nil {
		panic(err)
	}
	m.Run()
}

func BenchmarkNgap(b *testing.B) {
	b.Run("encoder", benchEncoder)
	b.Run("decoder", benchDecoder)
}

func benchEncoder(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_, err := Encoder(*pdu)
		if err != nil {
			panic(err)
		}
	}
}

func benchDecoder(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_, err := Decoder(by)
		if err != nil {
			panic(err)
		}
	}
}
goos: darwin
goarch: arm64
pkg: github.com/free5gc/ngap
               │     v0.txt     │               v3.txt                │
               │     sec/op     │    sec/op     vs base               │
Ngap/encoder-8    395.80µ ± ∞ ¹   11.30µ ± ∞ ¹  -97.15% (p=0.008 n=5)
Ngap/decoder-8   369.736µ ± ∞ ¹   9.619µ ± ∞ ¹  -97.40% (p=0.008 n=5)
geomean            382.5µ         10.42µ        -97.28%
¹ need >= 6 samples for confidence interval at level 0.95

               │     v0.txt      │                v3.txt                │
               │      B/op       │     B/op       vs base               │
Ngap/encoder-8   194.858Ki ± ∞ ¹   5.461Ki ± ∞ ¹  -97.20% (p=0.008 n=5)
Ngap/decoder-8   186.561Ki ± ∞ ¹   5.719Ki ± ∞ ¹  -96.93% (p=0.008 n=5)
geomean            190.7Ki         5.588Ki        -97.07%
¹ need >= 6 samples for confidence interval at level 0.95

               │    v0.txt    │               v3.txt               │
               │  allocs/op   │  allocs/op   vs base               │
Ngap/encoder-8   3710.0 ± ∞ ¹   335.0 ± ∞ ¹  -90.97% (p=0.008 n=5)
Ngap/decoder-8   3540.0 ± ∞ ¹   253.0 ± ∞ ¹  -92.85% (p=0.008 n=5)
geomean          3.624k         291.1        -91.97%
¹ need >= 6 samples for confidence interval at level 0.95

@tim-ywliu tim-ywliu merged commit 2c4c478 into free5gc:main May 3, 2024
2 checks passed
@ss920386
Copy link

ss920386 commented May 6, 2024

@suhuaqin Thanks for the PR. May I ask will this have a risk of memory leak? I didn't see sync.Map.Delete() called in this PR. Will the cache grow forever?

@suhuaqin
Copy link
Contributor Author

suhuaqin commented May 6, 2024

@suhuaqin Thanks for the PR. May I ask will this have a risk of memory leak? I didn't see sync.Map.Delete() called in this PR. Will the cache grow forever?

The maximum size of the cache depends on the number of structures to be parsed and the number of members of the structure. If you are concerned about memory wastage caused by the small frequency of use of certain caches, consider introducing a cache elimination mechanism

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.

3 participants