Lichen

Changeset

837:8e119abbd91c
2018-07-03 Paul Boddie raw files shortlog changelog graph Introduced a class (unpackable) and method (__get_single_item_unchecked__) to support sequence unpacking without index checking overhead, since the translator emits a test for the length of each sequence and controls the actual index values used. tuple-optimisations
common.py (file) lib/__builtins__/list.py (file) lib/__builtins__/sequence.py (file) lib/__builtins__/tuple.py (file)
     1.1 --- a/common.py	Tue Jul 03 15:58:44 2018 +0200
     1.2 +++ b/common.py	Tue Jul 03 16:02:02 2018 +0200
     1.3 @@ -462,7 +462,7 @@
     1.4          for i, node in enumerate(n.nodes):
     1.5              statements.append(
     1.6                  compiler.ast.Assign([node], compiler.ast.CallFunc(
     1.7 -                    compiler.ast.Getattr(compiler.ast.Name(temp), "__get_single_item__"),
     1.8 +                    compiler.ast.Getattr(compiler.ast.Name(temp), "__get_single_item_unchecked__"),
     1.9                      [compiler.ast.Const(i, str(i))]))
    1.10                  )
    1.11  
     2.1 --- a/lib/__builtins__/list.py	Tue Jul 03 15:58:44 2018 +0200
     2.2 +++ b/lib/__builtins__/list.py	Tue Jul 03 16:02:02 2018 +0200
     2.3 @@ -3,7 +3,7 @@
     2.4  """
     2.5  List objects.
     2.6  
     2.7 -Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
     2.8 +Copyright (C) 2015, 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk>
     2.9  
    2.10  This program is free software; you can redistribute it and/or modify it under
    2.11  the terms of the GNU General Public License as published by the Free Software
    2.12 @@ -20,11 +20,11 @@
    2.13  """
    2.14  
    2.15  from __builtins__.iteration.iterator import itemiterator
    2.16 -from __builtins__.sequence import sequence, _get_absolute_index
    2.17 +from __builtins__.sequence import unpackable, _get_absolute_index
    2.18  from native import list_append, list_concat, list_element, list_init, \
    2.19                     list_len, list_nonempty, list_setelement, list_setsize
    2.20  
    2.21 -class list(sequence):
    2.22 +class list(unpackable):
    2.23  
    2.24      "Implementation of list."
    2.25  
     3.1 --- a/lib/__builtins__/sequence.py	Tue Jul 03 15:58:44 2018 +0200
     3.2 +++ b/lib/__builtins__/sequence.py	Tue Jul 03 16:02:02 2018 +0200
     3.3 @@ -20,7 +20,7 @@
     3.4  """
     3.5  
     3.6  from __builtins__.int import maxint
     3.7 -from native import isinstance as _isinstance, is_int
     3.8 +from native import isinstance as _isinstance, is_int, list_element
     3.9  
    3.10  class itemaccess:
    3.11  
    3.12 @@ -255,6 +255,19 @@
    3.13  
    3.14      # __iter__(self)
    3.15  
    3.16 +class unpackable(sequence):
    3.17 +
    3.18 +    "Class for list and tuple unpacking."
    3.19 +
    3.20 +    def __get_single_item_unchecked__(self, index):
    3.21 +
    3.22 +        """
    3.23 +        NOTE: Should restrict this to internal translator use.
    3.24 +        NOTE: This also uses implementation-specific access.
    3.25 +        """
    3.26 +
    3.27 +        return list_element(self.__data__, index)
    3.28 +
    3.29  def _get_absolute_index(index, length):
    3.30  
    3.31      """
     4.1 --- a/lib/__builtins__/tuple.py	Tue Jul 03 15:58:44 2018 +0200
     4.2 +++ b/lib/__builtins__/tuple.py	Tue Jul 03 16:02:02 2018 +0200
     4.3 @@ -3,7 +3,7 @@
     4.4  """
     4.5  Tuple objects.
     4.6  
     4.7 -Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
     4.8 +Copyright (C) 2015, 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk>
     4.9  
    4.10  This program is free software; you can redistribute it and/or modify it under
    4.11  the terms of the GNU General Public License as published by the Free Software
    4.12 @@ -20,11 +20,11 @@
    4.13  """
    4.14  
    4.15  from __builtins__.iteration.iterator import itemiterator
    4.16 -from __builtins__.sequence import hashable, sequence
    4.17 +from __builtins__.sequence import hashable, unpackable
    4.18  from native import tuple_init, \
    4.19                     list_element, list_len, list_setsize, list_setelement
    4.20  
    4.21 -class tuple(sequence, hashable):
    4.22 +class tuple(unpackable, hashable):
    4.23  
    4.24      "Implementation of tuple."
    4.25  
    4.26 @@ -65,7 +65,7 @@
    4.27          'step'.
    4.28          """
    4.29  
    4.30 -        return tuple(get_using(sequence.__getslice__, self)(start, end, step))
    4.31 +        return tuple(get_using(unpackable.__getslice__, self)(start, end, step))
    4.32  
    4.33      def __len__(self):
    4.34