Skip to content

Improve performance of walk utility #4579

Closed as not planned
Closed as not planned
@AbhiPrasad

Description

@AbhiPrasad

We have a walk util which we use to walk objects to normalize them.

export function walk(key: string, value: any, depth: number = +Infinity, memo: MemoFunc = memoBuilder()): any {

This is used extensively throughout the SDK, primarily through

export function normalize(input: any, depth?: number): any {
try {
return JSON.parse(JSON.stringify(input, (key: string, value: any) => walk(key, value, depth)));
} catch (_oO) {
return '**non-serializable**';
}
}

@timfish and some others have reported OOM errors on coming from the walk function - which shows that it is not always super safe to use. We've also seen issues like #4470.

We should try to do a couple things here:

  1. Profile normalize and walk, see where the bottlenecks are in various scenarios. It's used in electron and react native also, so it'll be interesting to see the effects on that platform.
  2. Experiment with adding a limit to the number of leaves, not just limiting serialization depth
  3. Explore going beyond using a JSON.Stringify replacer to more a more rigorous object traversal to try to improve efficiency.

We can test potential performance improvements with https://github.com/getsentry/sentry-sdk-benchmark/

In a slightly related note, we also use JSON.stringify a lot throughout the codebase, and it's not always clear when to use walk to normalize. We should make the usage consistent, perhaps by extracting into distinct helpers based on the use case. This also means we could have "more expensive" and "less expensive" normalization functions - perhaps that will just alleviate the issue we have here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Waiting for: Product Owner

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions