Skip to content

perf: fewer memory allocation #597

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

Merged
merged 3 commits into from
May 15, 2025
Merged

perf: fewer memory allocation #597

merged 3 commits into from
May 15, 2025

Conversation

joeyzhao2018
Copy link
Contributor

@joeyzhao2018 joeyzhao2018 commented May 13, 2025

What does this PR do?

Less memory allocation and better performance.

Explanation

Memory allocation timing:

  • d.get("k", {}) always creates a new empty dictionary when the function is called, regardless of whether it will be used
  • d.get("k") or {} only creates the empty dictionary if d.get("k") returns a falsy value

Handling of falsy values:

  • d.get("k", {}) only uses the default when the key is missing
  • d.get("k") or {} uses the empty dict when the key is missing OR when the value exists but is falsy (None, empty string, 0, False, etc.)

Performance implications:

  • d.get("k", {}) has a slight overhead of always creating an object that might be discarded
  • d.get("k") or {} is slightly more efficient when the key exists with a truthy value

this is a follow-up PR for PR #593.
https://datadoghq.atlassian.net/browse/APMSVLS-10

Motivation

Testing Guidelines

Additional Notes

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)

@joeyzhao2018 joeyzhao2018 requested a review from a team as a code owner May 13, 2025 17:51
@joeyzhao2018 joeyzhao2018 changed the title fewer memory allocation perf: fewer memory allocation May 13, 2025
@joeyzhao2018 joeyzhao2018 merged commit b74068b into main May 15, 2025
60 checks passed
@joeyzhao2018 joeyzhao2018 deleted the joey/request_context branch May 15, 2025 02:27
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