diff options
author | poke1024 <poke1024@gmx.de> | 2017-12-13 20:38:21 +0100 |
---|---|---|
committer | Bernhard Liebl <Bernhard.Liebl@gmx.org> | 2017-12-16 16:27:21 +0100 |
commit | a3f1ed9af9bcc0ee95de7a90e864d4260554fe4b (patch) | |
tree | f0c4f4871a943d2fec51c2205fdc8294f5907f30 /core/os | |
parent | 1c0007ba71bfc1d42213e9feaa0ab3a867f2e163 (diff) |
Faster FileAccess::get_line()
Diffstat (limited to 'core/os')
-rw-r--r-- | core/os/file_access.cpp | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp index 7b2062936b..20c1221f2b 100644 --- a/core/os/file_access.cpp +++ b/core/os/file_access.cpp @@ -273,9 +273,62 @@ String FileAccess::get_token() const { return String::utf8(token.get_data()); } +class CharBuffer { + Vector<char> vector; + char stack_buffer[256]; + + char *buffer; + int capacity; + int written; + + bool grow() { + + if (vector.resize(next_power_of_2(1 + written)) != OK) { + + return false; + } + + if (buffer == stack_buffer) { // first chunk? + + for (int i = 0; i < written; i++) { + + vector[i] = stack_buffer[i]; + } + } + + buffer = vector.ptrw(); + capacity = vector.size(); + ERR_FAIL_COND_V(written >= capacity, false); + + return true; + } + +public: + _FORCE_INLINE_ CharBuffer() : + buffer(stack_buffer), + capacity(sizeof(stack_buffer) / sizeof(char)), + written(0) { + } + + _FORCE_INLINE_ void push_back(char c) { + + if (written >= capacity) { + + ERR_FAIL_COND(!grow()); + } + + buffer[written++] = c; + } + + _FORCE_INLINE_ const char *get_data() const { + + return buffer; + } +}; + String FileAccess::get_line() const { - CharString line; + CharBuffer line; CharType c = get_8(); |