From 45dfd6bce8635865d4211a9cf65c529fa472370a Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 13 Jun 2016 14:02:37 -0400 Subject: [PATCH] lib/core: Made more attributes fun since they are not used frequently Signed-off-by: Lucas Bajolet --- lib/core/text/abstract_text.nit | 9 --------- lib/core/text/flat.nit | 16 ++++------------ lib/core/text/ropes.nit | 37 ++++++++++++++++++++----------------- 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/lib/core/text/abstract_text.nit b/lib/core/text/abstract_text.nit index b3852c0..2ac2ca4 100644 --- a/lib/core/text/abstract_text.nit +++ b/lib/core/text/abstract_text.nit @@ -1411,9 +1411,6 @@ abstract class Buffer redef type SELFTYPE: Buffer is fixed - # Specific implementations MUST set this to `true` in order to invalidate caches - protected var is_dirty = true - # Copy-On-Write flag # # If the `Buffer` was to_s'd, the next in-place altering @@ -1539,12 +1536,6 @@ abstract class Buffer end end - redef fun hash - do - if is_dirty then hash_cache = null - return super - end - # In Buffers, the internal sequence of character is mutable # Thus, `chars` can be used to modify the buffer. redef fun chars: Sequence[Char] is abstract diff --git a/lib/core/text/flat.nit b/lib/core/text/flat.nit index a6f955a..53c6e7a 100644 --- a/lib/core/text/flat.nit +++ b/lib/core/text/flat.nit @@ -420,9 +420,9 @@ abstract class FlatString # Index at which `self` begins in `_items`, inclusively redef var first_byte is noinit - redef var chars = new FlatStringCharView(self) is lazy + redef fun chars do return new FlatStringCharView(self) - redef var bytes = new FlatStringByteView(self) is lazy + redef fun bytes do return new FlatStringByteView(self) redef fun to_cstring do var blen = _byte_length @@ -874,13 +874,9 @@ class FlatBuffer super FlatText super Buffer - redef var chars: Sequence[Char] = new FlatBufferCharView(self) is lazy + redef fun chars do return new FlatBufferCharView(self) - redef var bytes = new FlatBufferByteView(self) is lazy - - private var char_cache: Int = -1 - - private var byte_cache: Int = -1 + redef fun bytes do return new FlatBufferByteView(self) private var capacity = 0 @@ -926,7 +922,6 @@ class FlatBuffer do assert index >= 0 and index <= _length if written then reset - is_dirty = true if index == _length then add item return @@ -949,7 +944,6 @@ class FlatBuffer redef fun add(c) do if written then reset - is_dirty = true var clen = c.u8char_len var bt = _byte_length enlarge(bt + clen) @@ -959,7 +953,6 @@ class FlatBuffer end redef fun clear do - is_dirty = true _byte_length = 0 _length = 0 if written then @@ -1046,7 +1039,6 @@ class FlatBuffer redef fun append(s) do if s.is_empty then return - is_dirty = true var sl = s.byte_length var nln = _byte_length + sl enlarge(nln) diff --git a/lib/core/text/ropes.nit b/lib/core/text/ropes.nit index f472d96..b828f5d 100644 --- a/lib/core/text/ropes.nit +++ b/lib/core/text/ropes.nit @@ -70,9 +70,9 @@ private class Concat super Rope super String - redef var chars is lazy do return new RopeChars(self) + redef fun chars do return new RopeChars(self) - redef var bytes is lazy do return new RopeBytes(self) + redef fun bytes do return new RopeBytes(self) redef var length is noinit @@ -86,11 +86,9 @@ private class Concat var flat_cache: FlatString is noinit # Position of the beginning of `flat_cache` in `self` - var flat_last_pos_start: Int = -1 + var flat_last_pos_start: Int is noinit - var flat_last_pos_end: Int = -1 - - redef var to_cstring is lazy do + redef fun to_cstring do var len = _byte_length var ns = new NativeString(len + 1) ns[len] = 0u8 @@ -111,8 +109,9 @@ private class Concat init do var l = _left var r = _right - length = l.length + r.length + _length = l.length + r.length _byte_length = l.byte_length + r.byte_length + _flat_last_pos_start = _length end redef fun is_empty do return _byte_length == 0 @@ -131,10 +130,11 @@ private class Concat end redef fun [](i) do - assert i >= 0 and i <= _length + assert i >= 0 and i < _length var flps = _flat_last_pos_start - if flps != -1 and i >= flps and i <= _flat_last_pos_end then - return _flat_cache.fetch_char_at(i - flps) + if i >= flps then + var fc = _flat_cache + if i < flps + fc._length then return fc.fetch_char_at(i - flps) end var lf = get_leaf_at(i) return lf.fetch_char_at(i - _flat_last_pos_start) @@ -142,8 +142,9 @@ private class Concat fun get_leaf_at(pos: Int): FlatString do var flps = _flat_last_pos_start - if flps != -1 and pos >= flps and pos <= _flat_last_pos_end then - return _flat_cache + if pos >= flps then + var fc = _flat_cache + if pos < flps + fc._length then return fc end var s: String = self var st = pos @@ -160,7 +161,6 @@ private class Concat end end _flat_last_pos_start = st - pos - _flat_last_pos_end = st - pos + s.length - 1 _flat_cache = s return s end @@ -178,8 +178,11 @@ private class Concat var end_index = from + count - 1 var flps = _flat_last_pos_start - if flps != -1 and from >= flps and end_index <= _flat_last_pos_end then - return _flat_cache.substring_impl(from - flps, count, end_index - flps) + if from >= flps then + var fc = _flat_cache + if end_index < flps + fc._length then + return fc.substring_impl(from - flps, count, end_index - flps) + end end var lft = _left @@ -296,9 +299,9 @@ class RopeBuffer super Rope super Buffer - redef var chars: Sequence[Char] is lazy do return new RopeBufferChars(self) + redef fun chars do return new RopeBufferChars(self) - redef var bytes is lazy do return new RopeBufferBytes(self) + redef fun bytes do return new RopeBufferBytes(self) # The final string being built on the fly private var str: String = "" -- 1.7.9.5