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

Optimize stacktrace from 7.36kB to 2.30kB and speed too #467

Merged
merged 3 commits into from Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 13 additions & 10 deletions client.go
Expand Up @@ -338,6 +338,10 @@ func (client *Client) setupIntegrations() {
integration.SetupOnce(client)
Logger.Printf("Integration installed: %s\n", integration.Name())
}

sort.Slice(client.integrations, func(i, j int) bool {
return client.integrations[i].Name() < client.integrations[j].Name()
})
}

// AddEventProcessor adds an event processor to the client. It must not be
Expand Down Expand Up @@ -582,22 +586,22 @@ func (client *Client) prepareEvent(event *Event, hint *EventHint, scope EventMod
}

if event.ServerName == "" {
if client.Options().ServerName != "" {
event.ServerName = client.Options().ServerName
} else {
event.ServerName = client.Options().ServerName

if event.ServerName == "" {
event.ServerName = hostname
}
}

if event.Release == "" && client.Options().Release != "" {
if event.Release == "" {
event.Release = client.Options().Release
}

if event.Dist == "" && client.Options().Dist != "" {
if event.Dist == "" {
event.Dist = client.Options().Dist
}

if event.Environment == "" && client.Options().Environment != "" {
if event.Environment == "" {
event.Environment = client.Options().Environment
}

Expand Down Expand Up @@ -641,11 +645,10 @@ func (client *Client) prepareEvent(event *Event, hint *EventHint, scope EventMod
}

func (client Client) listIntegrations() []string {
integrations := make([]string, 0, len(client.integrations))
for _, integration := range client.integrations {
integrations = append(integrations, integration.Name())
integrations := make([]string, len(client.integrations))
for i, integration := range client.integrations {
integrations[i] = integration.Name()
}
sort.Strings(integrations)
return integrations
}

Expand Down
14 changes: 4 additions & 10 deletions scope.go
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"io"
"net/http"
"reflect"
"sync"
"time"
)
Expand Down Expand Up @@ -339,10 +338,6 @@ func (scope *Scope) ApplyToEvent(event *Event, hint *EventHint) *Event {
defer scope.mu.RUnlock()

if len(scope.breadcrumbs) > 0 {
if event.Breadcrumbs == nil {
event.Breadcrumbs = []*Breadcrumb{}
}

event.Breadcrumbs = append(event.Breadcrumbs, scope.breadcrumbs...)
}

Expand Down Expand Up @@ -384,14 +379,13 @@ func (scope *Scope) ApplyToEvent(event *Event, hint *EventHint) *Event {
}
}

if (reflect.DeepEqual(event.User, User{})) {
var emptyUser User
if event.User == emptyUser {
event.User = scope.user
}

if (event.Fingerprint == nil || len(event.Fingerprint) == 0) &&
len(scope.fingerprint) > 0 {
event.Fingerprint = make([]string, len(scope.fingerprint))
copy(event.Fingerprint, scope.fingerprint)
if len(event.Fingerprint) == 0 {
event.Fingerprint = append(event.Fingerprint, scope.fingerprint...)
}

if scope.level != "" {
Expand Down
14 changes: 9 additions & 5 deletions stacktrace.go
Expand Up @@ -245,21 +245,24 @@ func splitQualifiedFunctionName(name string) (pkg string, fun string) {
}

func extractFrames(pcs []uintptr) []Frame {
var frames []Frame
var frames = make([]Frame, 0, len(pcs))
callersFrames := runtime.CallersFrames(pcs)

for {
callerFrame, more := callersFrames.Next()

frames = append([]Frame{
NewFrame(callerFrame),
}, frames...)
frames = append(frames, NewFrame(callerFrame))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is main optimization of NewStacktrace because append faster that prepend and use less memory


if !more {
break
}
}

// reverse
for i, j := 0, len(frames)-1; i < j; i, j = i+1, j-1 {
frames[i], frames[j] = frames[j], frames[i]
}

return frames
}

Expand All @@ -270,7 +273,8 @@ func filterFrames(frames []Frame) []Frame {
return nil
}

filteredFrames := make([]Frame, 0, len(frames))
// reuse
filteredFrames := frames[:0]

for _, frame := range frames {
// Skip Go internal frames.
Expand Down
8 changes: 5 additions & 3 deletions transport.go
Expand Up @@ -556,14 +556,16 @@ func (t *HTTPSyncTransport) disabled(c ratelimit.Category) bool {
// Only used internally when an empty DSN is provided, which effectively disables the SDK.
type noopTransport struct{}

func (t *noopTransport) Configure(options ClientOptions) {
var _ Transport = noopTransport{}

func (noopTransport) Configure(ClientOptions) {
Logger.Println("Sentry client initialized with an empty DSN. Using noopTransport. No events will be delivered.")
}

func (t *noopTransport) SendEvent(event *Event) {
func (noopTransport) SendEvent(*Event) {
Logger.Println("Event dropped due to noopTransport usage.")
}

func (t *noopTransport) Flush(_ time.Duration) bool {
func (noopTransport) Flush(time.Duration) bool {
return true
}