Skip to content

Replaced OrderedDict with dict #7524

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

Closed
wants to merge 1 commit into from
Closed

Conversation

smithdc1
Copy link
Contributor

@smithdc1 smithdc1 commented Sep 6, 2020

dict is now assumed to be ordered in all currently supported versions of Python. This pull request changes OrderedDict to dict across the project. While making the changes I did some tests to show the performance benefit.

  1. Preserve order and remove duplicates via fromkeys() (e.g. line 334 of openapi.py)
%timeit list(dict.fromkeys(field.choices))
553 ns ± 1.97 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit list(OrderedDict.fromkeys(field.choices))
680 ns ± 5.24 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Conclusion dict is 19% faster

  1. Create a dict from a list

(e.g line 221 of pagination.py)

%timeit Response(OrderedDict([('count', self.page.paginator.count), ('next', self.get_next_link()), ('previous', self.get_previous_link()), ('results', data)]))
53.2 µs ± 204 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%timeit Response({'count': self.page.paginator.count, 'next': self.get_next_link(), 'previous': self.get_previous_link(), 'results': data,})
52.1 µs ± 473 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

(e.g line 660 in renderers.py)

%timeit -r20 OrderedDict(sorted(response.items()))
900 ns ± 9.79 ns per loop (mean ± std. dev. of 20 runs, 1000000 loops each)

%timeit -r20 dict(sorted(response.items()))
684 ns ± 3.59 ns per loop (mean ± std. dev. of 20 runs, 1000000 loops each)

Conclusion dict faster, 2% in the first example, 25% in the second. I suspect in practice the benefit is closer to the first; however, it is consistently faster.

  1. Adding key,values to a dict (e.g. line 62 or model_meta.py)
# OrderedDict()
%timeit for field in [field for field in opts.fields if field.serialize and not field.remote_field]: fields[field.name] = field
  ...:     
517 ns ± 4.83 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

# dict
%timeit for field in [field for field in opts.fields if field.serialize and not field.remote_field]: fields[field.name] = field
   ...:     
500 ns ± 4.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Conclusion dict is marginally (3%) faster

`dict` is now assumed to be ordered in all currently supported versions of Python
@smithdc1 smithdc1 closed this Sep 6, 2020
@carltongibson
Copy link
Collaborator

Django 2.2 still supports Python 3.5.

(But yes... after that.)

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.

2 participants