Changes between Version 16 and Version 17 of new_meta_api


Ignore:
Timestamp:
Jul 11, 2014, 8:52:52 AM (10 years ago)
Author:
pirosb3
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • new_meta_api

    v16 v17  
    302302Our final iteration was to add all the field types as flags to get_field. By making m2m as first parameter, we avoid breaking existing implementations
    303303and maintain a similarity with the 'get_fields' API.
     304
     305=== Performance
     306Throughout my project I have always kept an eye on performance. Throughout the development of my API I have refactored often and always looked for
     307bottlenecks using cProfile. I am happy to say no major decrease in speed has happened, and the new implementation does a couple of optimizations
     308that were not present in the old system. Said this, I prefer to not comment on performance but just to show the benchmarks. It will be the core
     309team to decide if this is feasible or not.
     310
     311=== Main optimization points
     312
     313==== Compute inverse relation map on first access
     314In order to find related objects, the current implementation does the following
     315
     316{{{
     317    for each model in apps
     318       for each field in model
     319          if field is a related object:
     320             if field is related to self:
     321                 add to related_objects
     322}}}
     323REF: https://github.com/django/django/blob/master/django/db/models/options.py#L488
     324
     325This tends to be expensive depending on the setup, but results in a O(models * fields) complexity. We can increase performance by
     326computing a inverse relation map on first access. This is done only **once**, not once per model
     327
     328REF: https://github.com/PirosB3/django/blob/soc2014_meta_refactor_upgrade_flags_get_field/django/apps/registry.py#L176
     329
     330In this way we have a map of model -> [related_object, related_object, ..] and computing a hash lookup is O(1).
     331
     332https://github.com/PirosB3/django/blob/soc2014_meta_refactor_upgrade_flags_get_field/django/db/models/options.py#L423
     333
     334Now, only 1 small loop is needed.
     335
     336==== Benchmarks
     337Here is a benchmarks table. It is benchmarking soc2014_meta_refactor_upgrade_flags_get_field (68dc11708eb2170540729b71db6bcaf4c46d6504)
     338against django/master
     339
     340https://gist.github.com/PirosB3/35a9231ee0214427321d
Back to Top