Merge: More lazyness in standard
authorJean Privat <jean@pryen.org>
Sat, 7 Mar 2015 05:17:21 +0000 (12:17 +0700)
committerJean Privat <jean@pryen.org>
Sat, 7 Mar 2015 05:17:21 +0000 (12:17 +0700)
Make lazy some attributes of often used classes. So that the attribute creation cost only when they are really used.

More impact that initially imagined:

for nitc/nitc/nitc
before: 7.188
after: 7.008 (-2.5%)

Pull-Request: #1188
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>

1  2 
lib/standard/string.nit

diff --combined lib/standard/string.nit
@@@ -350,12 -350,12 +350,12 @@@ abstract class Tex
        #
        #     assert " \n\thello \n\t".l_trim == "hello \n\t"
        #
 -      # A whitespace is defined as any character which ascii value is less than or equal to 32
 +      # `Char::is_whitespace` determines what is a whitespace.
        fun l_trim: SELFTYPE
        do
                var iter = self.chars.iterator
                while iter.is_ok do
 -                      if iter.item.ascii > 32 then break
 +                      if not iter.item.is_whitespace then break
                        iter.next
                end
                if iter.index == length then return self.empty
        #
        #     assert " \n\thello \n\t".r_trim == " \n\thello"
        #
 -      # A whitespace is defined as any character which ascii value is less than or equal to 32
 +      # `Char::is_whitespace` determines what is a whitespace.
        fun r_trim: SELFTYPE
        do
                var iter = self.chars.reverse_iterator
                while iter.is_ok do
 -                      if iter.item.ascii > 32 then break
 +                      if not iter.item.is_whitespace then break
                        iter.next
                end
                if iter.index < 0 then return self.empty
        end
  
        # Trims trailing and preceding white spaces
        #
        #     assert "  Hello  World !  ".trim   == "Hello  World !"
        #     assert "\na\nb\tc\t".trim          == "a\nb\tc"
 +      #
 +      # `Char::is_whitespace` determines what is a whitespace.
        fun trim: SELFTYPE do return (self.l_trim).r_trim
  
 +      # Is the string non-empty but only made of whitespaces?
 +      #
 +      #    assert " \n\t ".is_whitespace    == true
 +      #    assert "  hello  ".is_whitespace == false
 +      #    assert "".is_whitespace          == false
 +      #
 +      # `Char::is_whitespace` determines what is a whitespace.
 +      fun is_whitespace: Bool
 +      do
 +              if is_empty then return false
 +              for c in self.chars do
 +                      if not c.is_whitespace then return false
 +              end
 +              return true
 +      end
 +
        # Returns `self` removed from its last line terminator (if any).
        #
        #    assert "Hello\n".chomp == "Hello"
@@@ -1069,7 -1052,7 +1069,7 @@@ class FlatStrin
        # Indes in _items of the last item of the string
        private var index_to: Int is noinit
  
-       redef var chars: SequenceRead[Char] = new FlatStringCharView(self)
+       redef var chars: SequenceRead[Char] = new FlatStringCharView(self) is lazy
  
        redef fun [](index)
        do
@@@ -1539,7 -1522,7 +1539,7 @@@ class FlatBuffe
        super FlatText
        super Buffer
  
-       redef var chars: Sequence[Char] = new FlatBufferCharView(self)
+       redef var chars: Sequence[Char] = new FlatBufferCharView(self) is lazy
  
        private var capacity: Int = 0