Skip to content

Fix for performance issue in as_dict function #53

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 4.0.2 (2022-11-15)

- Fix for performance issue in as_dict function

## 4.0.1 (2020-02-13)

- Gracefully fail to install if setuptools is too old
Expand Down
8 changes: 2 additions & 6 deletions flatdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections import MutableMapping
import sys

__version__ = '4.0.1'
__version__ = '4.0.2'

NO_DEFAULT = object()

Expand Down Expand Up @@ -177,11 +177,7 @@ def as_dict(self):
if self._has_delimiter(ck):
ck = ck.split(self._delimiter, 1)[0]
if isinstance(self._values[pk], FlatDict) and pk not in out:
out[pk] = {}
if isinstance(self._values[pk][ck], FlatDict):
out[pk][ck] = self._values[pk][ck].as_dict()
else:
out[pk][ck] = self._values[pk][ck]
out[pk] = self._values[pk].as_dict()
else:
out[key] = self._values[key]
return out
Expand Down
1 change: 1 addition & 0 deletions requires/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ flake8-import-order
flake8-quotes
flake8-rst-docstrings
flake8-tuple
timeit2
86 changes: 86 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import pickle
import random
import timeit
import unittest
import uuid

Expand Down Expand Up @@ -117,6 +118,83 @@ class FlatDictTests(unittest.TestCase):
}
}

PERF_DICT = {
'zero': {
'one': {
'two': 'red',
'three': 'blue',
'four': 'green'
},
'two': {
'three': 'red',
'four': 'blue',
'five': 'green'
},
'three': {
'four': {
'five': {
'six': {
'seven': {
'eight': 'red',
'nine': 'blue',
'ten': 'green'
}
},
'seven': {
'eight': {
'nine': 'red',
'ten': 'blue',
'eleven': 'green'
},
'nine': {
'ten': {
'eleven': {
'twelve': {
'thirteen': {
'fourteen': 'red',
'fifteen': 'blue',
'sixteen': 'green'
}
}
}
}
}
},
'eight': {
'nine': {
'ten': 'red',
'eleven': 'blue',
'twelve': 'green'
},
'ten': {
'eleven': {
'twelve': {
'thirteen': {
'fourteen': {
'fifteen': 'red',
'sixteen': 'blue',
'seventeen': 'green'
},
'fifteen': {
'sixteen': {
'seventeen': {
'twenty': 'red',
'twentey_one': 'blue',
'twenty_two': 'green'
}
}
}
}
}
}
}
}
}
}
}
}
}

def setUp(self):
self.value = self.TEST_CLASS(self.VALUES, ':')

Expand Down Expand Up @@ -304,6 +382,14 @@ def test_empty_dict_as_value(self):
value = flat.as_dict()
self.assertDictEqual(value, expectation)

def test_as_dict_perf(self):
m = self.TEST_CLASS(self.PERF_DICT)
num_runs = 5
max = 0.002
duration = timeit.Timer(m.as_dict).timeit(number=num_runs)
avg_duration = duration / num_runs
self.assertLess(avg_duration, max)


class FlatterDictTests(FlatDictTests):

Expand Down