Merge: Basename fix
authorJean Privat <jean@pryen.org>
Mon, 28 Sep 2015 14:42:57 +0000 (10:42 -0400)
committerJean Privat <jean@pryen.org>
Mon, 28 Sep 2015 14:42:57 +0000 (10:42 -0400)
Should close #1736 if performances are adequate for @privat.

This PR optimizes some methods in both `SequenceRead` and `FlatString`, on the test program exposed in #1736, the valgrind runtime has gone from 418,872,526 Ir to 136,522,095 Ir (~67.5% improvement)

Some more improvements could be observed if we could get rid of some useless Boxes in `NativeString::to_s_with_copy` because of a `is_same_instance` which boxes NativeStrings (24k allocations in total to remove).

Pull-Request: #1740
Reviewed-by: Jean Privat <jean@pryen.org>

1  2 
lib/core/collection/abstract_collection.nit

@@@ -228,16 -228,6 +228,16 @@@ interface Iterator[E
        # Iterate over `self`
        fun iterator: Iterator[E] do return self
  
 +      # Pre-iteration hook.
 +      #
 +      # Used to inform `self` that the iteration is starting.
 +      # Specific iterators can use this to prepare some resources.
 +      #
 +      # Is automatically invoked at the beginning of `for` structures.
 +      #
 +      # Do nothing by default.
 +      fun start do end
 +
        # Post-iteration hook.
        #
        # Used to inform `self` that the iteration is over.
@@@ -718,16 -708,6 +718,16 @@@ interface MapIterator[K, V
        # Set a new `item` at `key`.
        #fun item=(item: E) is abstract
  
 +      # Pre-iteration hook.
 +      #
 +      # Used to inform `self` that the iteration is starting.
 +      # Specific iterators can use this to prepare some resources.
 +      #
 +      # Is automatically invoked at the beginning of `for` structures.
 +      #
 +      # Do nothing by default.
 +      fun start do end
 +
        # Post-iteration hook.
        #
        # Used to inform `self` that the iteration is over.
@@@ -853,18 -833,13 +853,13 @@@ interface SequenceRead[E
        #     assert a.last_index_of_from(20, 2)   == 1
        #     assert a.last_index_of_from(20, 1)   == 1
        #     assert a.last_index_of_from(20, 0)   == -1
-       fun last_index_of_from(item: nullable Object, pos: Int): Int
-       do
-               var res = -1
-               var p = 0
-               var i = iterator
-               while i.is_ok do
-                       if p>pos then break
-                       if i.item == item then res = p
-                       i.next
-                       p += 1
+       fun last_index_of_from(item: nullable Object, pos: Int): Int do
+               var i = pos
+               while i >= 0 do
+                       if self[i] == item then return i
+                       i -= 1
                end
-               return res
+               return -1
        end
  
        # Two sequences are equals if they have the same items in the same order.