Skip to content

Fix varmap to apply func before recursion #1593

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 2 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ endif::[]
//===== Bug fixes
//

=== Unreleased

// Unreleased changes go here
// When the next release happens, nest these changes under the "Python Agent version 6.x" heading
//[float]
//===== Features
//
[float]
===== Bug fixes

* Fixed a performance issue for local variable shortening via `varmap()` {pull}1593[#1593]


[[release-notes-6.x]]
=== Python Agent version 6.x

Expand Down
8 changes: 6 additions & 2 deletions elasticapm/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ def varmap(func, var, context=None, name=None, **kwargs):
return func(name, "<...>", **kwargs)
context.add(objid)
if isinstance(var, dict):
# Apply func() before recursion, so that `shorten()` doesn't have to iterate over all the trimmed values
ret = func(name, var, **kwargs)
# iterate over a copy of the dictionary to avoid "dictionary changed size during iteration" issues
ret = func(name, dict((k, varmap(func, v, context, k, **kwargs)) for k, v in var.copy().items()), **kwargs)
ret = dict((k, varmap(func, v, context, k, **kwargs)) for k, v in ret.copy().items())
elif isinstance(var, (list, tuple)):
ret = func(name, [varmap(func, f, context, name, **kwargs) for f in var], **kwargs)
# Apply func() before recursion, so that `shorten()` doesn't have to iterate over all the trimmed values
ret = func(name, var, **kwargs)
ret = [varmap(func, f, context, name, **kwargs) for f in ret]
else:
ret = func(name, var, **kwargs)
context.remove(objid)
Expand Down