From 65ac5d42c9209c4b89c590c93cfa8d985e66b168 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 15 Aug 2024 11:41:35 +1000 Subject: Remove unused ConstexprVector and rename constexpr_utils.hh to array.hh --- src/buffer.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/buffer.hh') diff --git a/src/buffer.hh b/src/buffer.hh index 7b671c0e..ec2b9620 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -3,7 +3,7 @@ #include "clock.hh" #include "coord.hh" -#include "constexpr_utils.hh" +#include "array.hh" #include "enum.hh" #include "file.hh" #include "optional.hh" -- cgit v1.2.3 From 8769b94eb25cd7402db6caa4f0d4b417f6365703 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 30 Nov 2024 12:27:11 +1100 Subject: Cache buffer lines ArrayView in BufferIterator The extra indirection of going through the buffer can be costly as the compiler does not know the buffer is not supposed to be mutated during iteration, so it has to actually reload the values which adds memory accesses in the Buffer instance which can be costly in say regex searches where memory access tends to dominate performance. Storing this in the BufferIterator lets the compiler put this info in registers and not reload it. --- src/buffer.hh | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/buffer.hh') diff --git a/src/buffer.hh b/src/buffer.hh index ec2b9620..fd748213 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -95,6 +95,7 @@ public: private: SafePtr m_buffer; + ArrayView m_lines; BufferCoord m_coord; StringView m_line; }; @@ -122,6 +123,7 @@ public: ReadOnly = 1 << 6, }; friend constexpr bool with_bit_ops(Meta::Type) { return true; } + friend class BufferIterator; enum class HistoryId : size_t { First = 0, Invalid = (size_t)-1 }; -- cgit v1.2.3 From d29fa04adc6a15c8d62ffcd4390e5fc785d92b62 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 3 Dec 2024 21:26:57 +1100 Subject: Do not store buffer pointer in BufferIterator This makes BufferIterator smaller and trivially move/copyable --- src/buffer.hh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/buffer.hh') diff --git a/src/buffer.hh b/src/buffer.hh index fd748213..2b27f7c3 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -64,8 +64,9 @@ public: // costly, so this is not strictly random access using iterator_category = std::bidirectional_iterator_tag; - BufferIterator() noexcept : m_buffer{nullptr}, m_line{} {} + BufferIterator() = default; BufferIterator(const Buffer& buffer, BufferCoord coord) noexcept; + BufferIterator(ArrayView lines, BufferCoord coord) noexcept; bool operator== (const BufferIterator& iterator) const noexcept; auto operator<=>(const BufferIterator& iterator) const noexcept; @@ -75,7 +76,7 @@ public: const char& operator[](size_t n) const noexcept; size_t operator- (const BufferIterator& iterator) const; - explicit operator bool() const { return static_cast(m_buffer); } + explicit operator bool() const { return not m_lines.empty(); } BufferIterator operator+ (ByteCount size) const; BufferIterator operator- (ByteCount size) const; @@ -94,7 +95,6 @@ public: using Sentinel = BufferCoord; private: - SafePtr m_buffer; ArrayView m_lines; BufferCoord m_coord; StringView m_line; @@ -159,9 +159,12 @@ public: String string(BufferCoord begin, BufferCoord end) const; StringView substr(BufferCoord begin, BufferCoord end) const; + static BufferCoord advance(ArrayView lines, BufferCoord coord, ByteCount count); + static ByteCount distance(ArrayView lines, BufferCoord begin, BufferCoord end); + const char& byte_at(BufferCoord c) const; - ByteCount distance(BufferCoord begin, BufferCoord end) const; - BufferCoord advance(BufferCoord coord, ByteCount count) const; + ByteCount distance(BufferCoord begin, BufferCoord end) const { return distance(m_lines, begin, end); } + BufferCoord advance(BufferCoord coord, ByteCount count) const { return advance(m_lines, coord, count); } BufferCoord next(BufferCoord coord) const; BufferCoord prev(BufferCoord coord) const; -- cgit v1.2.3 From 19f23e0ff81d4b97a7b95567e7ce6f3b49bac81d Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 3 Dec 2024 21:29:47 +1100 Subject: Compact BufferIterator to avoid padding --- src/buffer.hh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/buffer.hh') diff --git a/src/buffer.hh b/src/buffer.hh index 2b27f7c3..50423502 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -66,7 +66,7 @@ public: BufferIterator() = default; BufferIterator(const Buffer& buffer, BufferCoord coord) noexcept; - BufferIterator(ArrayView lines, BufferCoord coord) noexcept; + BufferIterator(const StringDataPtr* lines, LineCount line_count, BufferCoord coord) noexcept; bool operator== (const BufferIterator& iterator) const noexcept; auto operator<=>(const BufferIterator& iterator) const noexcept; @@ -76,7 +76,7 @@ public: const char& operator[](size_t n) const noexcept; size_t operator- (const BufferIterator& iterator) const; - explicit operator bool() const { return not m_lines.empty(); } + explicit operator bool() const { return m_lines; } BufferIterator operator+ (ByteCount size) const; BufferIterator operator- (ByteCount size) const; @@ -95,9 +95,10 @@ public: using Sentinel = BufferCoord; private: - ArrayView m_lines; + const StringDataPtr* m_lines; + [[no_unique_address]] StringView m_line; + [[no_unique_address]] LineCount m_line_count; BufferCoord m_coord; - StringView m_line; }; using BufferLines = Vector; -- cgit v1.2.3