tools/binary_size/libsupersize/file_format.py
def SortSymbols(raw_symbols, check_already_mostly_sorted=True):
  """Sorts the given symbols in the order that they should be archived in.

  The sort order is chosen such that:
    * Padding can be discarded.
    * Ordering is deterministic (total ordering).

  Also sorts |aliases| such that they match the order within |raw_symbols|.

  Args:
    raw_symbols: List of symbols to sort.
    check_already_mostly_sorted: Whether to assert that there are a low number
        of out-of-order elements in raw_symbols. Older .size files are not
        properly sorted, this check makes sense only for "supersize archive".
  """

  def sort_key(s):
    # size_without_padding so that "** symbol gap" sorts before other symbols
    # with same address (necessary for correctness within CalculatePadding()).
    return (
        _SECTION_SORT_ORDER[s.section_name],
        s.IsOverhead(),
        s.address,
        # Only use size_without_padding for native symbols (that have
        # addresses) since padding-only symbols must come first for
        # correctness.
        # DEX also has 0-size symbols (for nested classes, not sure why)
        # and we don't want to sort them differently since they don't have
        # any padding either.
        s.address and s.size_without_padding > 0,
        s.full_name.startswith('**'),
        s.full_name,
        s.object_path)

  def describe(syms):
    return ''.join('%r: %r\n' % (s, sort_key(s)) for s in syms)

  logging.debug('Sorting %d symbols', len(raw_symbols))

  # Sort aliases first to make raw_symbols quicker to sort.
  # Although sorting is done when aliases are first created, aliases that differ
  # only by path can later become out-of-order due to path normalization.
  i = 0
  count = len(raw_symbols)
  while i < count:
    s = raw_symbols[i]
    num_aliases = s.num_aliases
    if s.aliases:
      expected = raw_symbols[i:i + num_aliases]
      assert s.aliases == expected, 'Aliases out of order:\n{}\n{}'.format(
          describe(s.aliases), describe(expected))

      s.aliases.sort(key=sort_key)
      raw_symbols[i:i + num_aliases] = s.aliases
      i += num_aliases
    else:
      i += 1

  if check_already_mostly_sorted:
    count = sum(
        int(sort_key(raw_symbols[i]) > sort_key(raw_symbols[i + 1]))
        for i in range(len(raw_symbols) - 1))
    logging.debug('Number of out-of-order symbols: %d', count)
    if count > 20:
      logging.error('Number of out-of-order symbols: %d', count)
      logging.error('Showing first 10')
      num_reported = 0
      for i in range(len(raw_symbols) - 1):
        if sort_key(raw_symbols[i]) > sort_key(raw_symbols[i + 1]):
          num_reported += 1
          logging.error('\n%s', describe(raw_symbols[i:i + 2]))
          if num_reported == 10:
            break

  # Python's sort() is faster when the input list is already mostly sorted.
  raw_symbols.sort(key=sort_key)
Sorts the given symbols in the order that they should be archived in.

  The sort order is chosen such that:
    * Padding can be discarded.
    * Ordering is deterministic (total ordering).

  Also sorts |aliases| such that they match the order within |raw_symbols|.

  Args:
    raw_symbols: List of symbols to sort.
    check_already_mostly_sorted: Whether to assert that there are a low number
        of out-of-order elements in raw_symbols. Older .size files are not
        properly sorted, this check makes sense only for "supersize archive".
  
Sorts the given symbols in the order that they should be archived in.
The sort order is chosen such that:
Padding can be discarded.
Ordering is deterministic (total ordering).

Also sorts |aliases| such that they match the order within |raw_symbols|.
Sorts the given symbols in the order that they should be archived in . The sort order is chosen such that : Padding can be discarded . Ordering is deterministic ( total ordering ) . Also sorts |aliases| such that they match the order within |raw_symbols| .
def SortSymbols(raw_symbols, check_already_mostly_sorted=True):
  def sort_key(s):
    return (
        _SECTION_SORT_ORDER[s.section_name],
        s.IsOverhead(),
        s.address,
        s.address and s.size_without_padding > 0,
        s.full_name.startswith('**'),
        s.full_name,
        s.object_path)
  def describe(syms):
    return ''.join('%r: %r\n' % (s, sort_key(s)) for s in syms)
  logging.debug('Sorting %d symbols', len(raw_symbols))
  i = 0
  count = len(raw_symbols)
  while i < count:
    s = raw_symbols[i]
    num_aliases = s.num_aliases
    if s.aliases:
      expected = raw_symbols[i:i + num_aliases]
      assert s.aliases == expected, 'Aliases out of order:\n{}\n{}'.format(
          describe(s.aliases), describe(expected))
      s.aliases.sort(key=sort_key)
      raw_symbols[i:i + num_aliases] = s.aliases
      i += num_aliases
    else:
      i += 1
  if check_already_mostly_sorted:
    count = sum(
        int(sort_key(raw_symbols[i]) > sort_key(raw_symbols[i + 1]))
        for i in range(len(raw_symbols) - 1))
    logging.debug('Number of out-of-order symbols: %d', count)
    if count > 20:
      logging.error('Number of out-of-order symbols: %d', count)
      logging.error('Showing first 10')
      num_reported = 0
      for i in range(len(raw_symbols) - 1):
        if sort_key(raw_symbols[i]) > sort_key(raw_symbols[i + 1]):
          num_reported += 1
          logging.error('\n%s', describe(raw_symbols[i:i + 2]))
          if num_reported == 10:
            break
  raw_symbols.sort(key=sort_key)
def SortSymbols ( raw_symbols , check_already_mostly_sorted = True ) : def sort_key ( s ) : return ( _SECTION_SORT_ORDER [ s . section_name ] , s . IsOverhead ( ) , s . address , s . address and s . size_without_padding > 0 , s . full_name . startswith ( '**' ) , s . full_name , s . object_path ) def describe ( syms ) : return '' . join ( '%r: %r\n' % ( s , sort_key ( s ) ) for s in syms ) logging . debug ( 'Sorting %d symbols' , len ( raw_symbols ) ) i = 0 count = len ( raw_symbols ) while i < count : s = raw_symbols [ i ] num_aliases = s . num_aliases if s . aliases : expected = raw_symbols [ i : i + num_aliases ] assert s . aliases == expected , 'Aliases out of order:\n{}\n{}' . format ( describe ( s . aliases ) , describe ( expected ) ) s . aliases . sort ( key = sort_key ) raw_symbols [ i : i + num_aliases ] = s . aliases i += num_aliases else : i += 1 if check_already_mostly_sorted : count = sum ( int ( sort_key ( raw_symbols [ i ] ) > sort_key ( raw_symbols [ i + 1 ] ) ) for i in range ( len ( raw_symbols ) - 1 ) ) logging . debug ( 'Number of out-of-order symbols: %d' , count ) if count > 20 : logging . error ( 'Number of out-of-order symbols: %d' , count ) logging . error ( 'Showing first 10' ) num_reported = 0 for i in range ( len ( raw_symbols ) - 1 ) : if sort_key ( raw_symbols [ i ] ) > sort_key ( raw_symbols [ i + 1 ] ) : num_reported += 1 logging . error ( '\n%s' , describe ( raw_symbols [ i : i + 2 ] ) ) if num_reported == 10 : break raw_symbols . sort ( key = sort_key )
Sorts the given symbols in the order that they should be archived in.
Sorts the given symbols in the order that they should be archived in .
"""Sorts the given symbols in the order that they should be archived in.

  The sort order is chosen such that:
    * Padding can be discarded.
    * Ordering is deterministic (total ordering).

  Also sorts |aliases| such that they match the order within |raw_symbols|.

  Args:
    raw_symbols: List of symbols to sort.
    check_already_mostly_sorted: Whether to assert that there are a low number
        of out-of-order elements in raw_symbols. Older .size files are not
        properly sorted, this check makes sense only for "supersize archive".
  """ # size_without_padding so that "** symbol gap" sorts before other symbols # with same address (necessary for correctness within CalculatePadding()). # Only use size_without_padding for native symbols (that have # addresses) since padding-only symbols must come first for # correctness. # DEX also has 0-size symbols (for nested classes, not sure why) # and we don't want to sort them differently since they don't have # any padding either. # Sort aliases first to make raw_symbols quicker to sort. # Although sorting is done when aliases are first created, aliases that differ # only by path can later become out-of-order due to path normalization. # Python's sort() is faster when the input list is already mostly sorted.
raw_symbols
List of symbols to sort.
List of symbols to sort .
check_already_mostly_sorted
Whether to assert that there are a low number
of out-of-order elements in raw_symbols. Older .size files are not
properly sorted, this check makes sense only for "supersize archive".
Whether to assert that there are a low number of out - of - order elements in raw_symbols . Older . size files are not properly sorted this check makes sense only for " supersize archive " .