Skip to content
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

Refactor Cell.hash function to avoid aggressive recursion #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shuva10v
Copy link

Current implementation of Cell.hash() implies aggressive recursion processing refs. For some branchy cells it leads to serious performance issues. For example for such case 'te6ccsEBFwEAYAAEBQQFBQQFBAQEBAQEBAQEBAQFBAQEAwECyQEBBMsXAgECEgMBBMsXBAEEywcF\nAQIUBgEEywcHAQIBCAECJQkBAsgKAQKhCwECAQwBAiENAQIiDgECqA8BAgEQAQIhEQECARIBBIAD\nEwECqBQBAgEVAQIhFgACIX1S8I0=':

{'data': {'b64': 'yQ==', 'len': 8},
 'refs': [{'data': {'b64': 'yxc=', 'len': 16},
   'refs': [{'data': {'b64': 'Eg==', 'len': 8},
     'refs': [{'data': {'b64': 'yxc=', 'len': 16},
       'refs': [{'data': {'b64': 'ywc=', 'len': 16},
         'refs': [{'data': {'b64': 'FA==', 'len': 8},
           'refs': [{'data': {'b64': 'ywc=', 'len': 16},
             'refs': [{'data': {'b64': 'AQ==', 'len': 8},
               'refs': [{'data': {'b64': 'JQ==', 'len': 8},
                 'refs': [{'data': {'b64': 'yA==', 'len': 8},
                   'refs': [{'data': {'b64': 'oQ==', 'len': 8},
                     'refs': [{'data': {'b64': 'AQ==', 'len': 8},
                       'refs': [{'data': {'b64': 'IQ==', 'len': 8},
                         'refs': [{'data': {'b64': 'Ig==', 'len': 8},
                           'refs': [{'data': {'b64': 'qA==', 'len': 8},
                             'refs': [{'data': {'b64': 'AQ==', 'len': 8},
                               'refs': [{'data': {'b64': 'IQ==', 'len': 8},
                                 'refs': [{'data': {'b64': 'AQ==', 'len': 8},
                                   'refs': [{'data': {'b64': 'gAM=',
                                      'len': 16},
                                     'refs': [{'data': {'b64': 'qA==',
                                        'len': 8},
                                       'refs': [{'data': {'b64': 'AQ==',
                                          'len': 8},
                                         'refs': [{'data': {'b64': 'IQ==',
                                            'len': 8},
                                           'refs': [{'data': {'b64': 'IQ==',
                                              'len': 8},
                                             'refs': [],
                                             'special': False}],
                                           'special': False}],
                                         'special': False}],
                                       'special': False}],
                                     'special': False}],
                                   'special': False}],
                                 'special': False}],
                               'special': False}],
                             'special': False}],
                           'special': False}],
                         'special': False}],
                       'special': False}],
                     'special': False}],
                   'special': False}],
                 'special': False}],
               'special': False}],
             'special': False}],
           'special': False}],
         'special': False}],
       'special': False}],
     'special': False}],
   'special': False}],
 'special': False}

Current hash implementation leads to 25167033 function calls:

   25167033 function calls (12583868 primitive calls) in 4.252 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    4.252    4.252 {built-in method builtins.exec}
        1    0.000    0.000    4.252    4.252 <string>:1(<module>)
     23/1    0.000    0.000    4.252    4.252 cell.py:174(hash)
     23/1    0.000    0.000    4.252    4.252 cell.py:166(repr)
       22    0.000    0.000    4.251    0.193 cell.py:147(encoded_depth)
12582866/44    3.793    0.000    4.251    0.097 cell.py:138(depth)
12583006/12582960    0.459    0.000    0.459    0.000 {built-in method builtins.len}
...

After this patch it costs 702 calls:


         702 function calls (613 primitive calls) in 0.001 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.001    0.001 {built-in method builtins.exec}
        1    0.000    0.000    0.001    0.001 <string>:1(<module>)
        1    0.000    0.000    0.001    0.001 cell.py:212(hash2)
     23/1    0.000    0.000    0.001    0.001 cell.py:183(__depth_level_hash)
     22/1    0.000    0.000    0.001    0.001 cell.py:192(<listcomp>)
       23    0.000    0.000    0.000    0.000 cell.py:88(top_upped_bytes)
       23    0.000    0.000    0.000    0.000 cell.py:159(bits_descriptor)
  162/116    0.000    0.000    0.000    0.000 {built-in method builtins.len}

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.

1 participant