diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2024-02-08 20:38:08 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2024-02-09 10:37:01 +0000 |
commit | ffb88643646c0304850971a39c38f96b46b88217 (patch) | |
tree | 3a349004f34b357da989e0ff0ec38de4fd4b766b /vespalib | |
parent | 6e03787d79b327915dff98815db777d879986396 (diff) |
Use smaller buffer for reading file headers. And GC some unused code.
Diffstat (limited to 'vespalib')
4 files changed, 33 insertions, 51 deletions
diff --git a/vespalib/src/vespa/fastlib/io/bufferedfile.cpp b/vespalib/src/vespa/fastlib/io/bufferedfile.cpp index 0fa62aca295..4d46427ed33 100644 --- a/vespalib/src/vespa/fastlib/io/bufferedfile.cpp +++ b/vespalib/src/vespa/fastlib/io/bufferedfile.cpp @@ -202,7 +202,7 @@ Fast_BufferedFile::ReadLine(char *line, size_t buflen) p = line; ep = line + buflen - 1; - while (1) { + while (true) { while (_bufi < _bufe && *_bufi != '\n' && p < ep) *p++ = *_bufi++; if (p >= ep) { @@ -258,7 +258,7 @@ Fast_BufferedFile::Read(void *dst, size_t dstlen) { char * p = static_cast<char *>(dst); char * pe = p + dstlen; - while (1) { + while (true) { int64_t sz = std::min(_bufe - _bufi, pe - p); memcpy(p, _bufi, sz); p += sz; @@ -334,11 +334,6 @@ Fast_BufferedFile::WriteOpen(const char *name) _openFlags = FASTOS_FILE_OPEN_WRITE; } -Fast_BufferedFile::Fast_BufferedFile(FastOS_FileInterface *file) : - Fast_BufferedFile(file, DEFAULT_BUF_SIZE) -{ -} - Fast_BufferedFile::Fast_BufferedFile() : Fast_BufferedFile(DEFAULT_BUF_SIZE) { diff --git a/vespalib/src/vespa/fastlib/io/bufferedfile.h b/vespalib/src/vespa/fastlib/io/bufferedfile.h index 8a22ec89bd7..16153b79171 100644 --- a/vespalib/src/vespa/fastlib/io/bufferedfile.h +++ b/vespalib/src/vespa/fastlib/io/bufferedfile.h @@ -28,30 +28,40 @@ private: char * buf() { return static_cast<char *>(_buf.get()); } const char * buf() const { return static_cast<const char *>(_buf.get()); } -protected: /** The file instance used for low-level file access. */ std::unique_ptr<FastOS_FileInterface> _file; -public: - /** - * Create buffered file. - * @param file file instance that should be used for low-level - * file access. If this is NULL, an instance of - * FastOS_File will be created. NOTE: the file - * instance given here will be deleted by - * the destructor. - **/ Fast_BufferedFile(FastOS_FileInterface *file, size_t bufferSize); - Fast_BufferedFile(FastOS_FileInterface *file); + /** + * Reset the internal start and end pointers to the + * head of the buffer, thus "emptying" it. + */ + void ResetBuf(); + /** + * Write the buffer to the file. Caution: Uses obsolete + * FastOS_FileInterface::WriteBuf. + * Allocates a 32kB buffer if not previously allocated. + */ + void flushWriteBuf(); + /** + * Read from the file into the buffer. Allocates a 32kB + * buffer if not previously allocated. Fills the buffer, + * or reads as much as possible if the (rest of) the file + * is smaller than the buffer. + * Caution: If the amount read is smaller than the expected + * amount, the method will abort. + */ + void fillReadBuf(); +public: Fast_BufferedFile(); - Fast_BufferedFile(size_t bufferSize); + explicit Fast_BufferedFile(size_t bufferSize); Fast_BufferedFile(const Fast_BufferedFile &) = delete; Fast_BufferedFile & operator = (const Fast_BufferedFile &) = delete; /** * Delete the file instance used for low-level file access. **/ - virtual ~Fast_BufferedFile(); + ~Fast_BufferedFile() override; /** * Open an existing file for reading. * @@ -71,26 +81,7 @@ public: * @param name The name of the file to open. */ void WriteOpen(const char *name); - /** - * Reset the internal start and end pointers to the - * head of the buffer, thus "emptying" it. - */ - void ResetBuf(); - /** - * Write the buffer to the file. Caution: Uses obsolete - * FastOS_FileInterface::WriteBuf. - * Allocates a 32kB buffer if not previously allocated. - */ - void flushWriteBuf(); - /** - * Read from the file into the buffer. Allocates a 32kB - * buffer if not previously allocated. Fills the buffer, - * or reads as much as possible if the (rest of) the file - * is smaller than the buffer. - * Caution: If the amount read is smaller than the expected - * amount, the method will abort. - */ - void fillReadBuf(); + /** * Read the next line of the buffered file into a buffer, * reading from the file as necessary. diff --git a/vespalib/src/vespa/vespalib/util/mmap_file_allocator.cpp b/vespalib/src/vespa/vespalib/util/mmap_file_allocator.cpp index a663c6a601b..e6a00f4b86d 100644 --- a/vespalib/src/vespa/vespalib/util/mmap_file_allocator.cpp +++ b/vespalib/src/vespa/vespalib/util/mmap_file_allocator.cpp @@ -75,7 +75,7 @@ PtrAndSize MmapFileAllocator::alloc(size_t sz) const { if (sz == 0) { - return PtrAndSize(); // empty allocation + return {}; // empty allocation } static constexpr size_t alignment = 128; sz = (sz + alignment - 1) & -alignment; // round sz to a multiple of alignment @@ -107,7 +107,7 @@ MmapFileAllocator::alloc_large(size_t sz) const retval = madvise(buf, sz, MADV_DONTDUMP); assert(retval == 0); #endif - return PtrAndSize(buf, sz); + return {buf, sz}; } void* diff --git a/vespalib/src/vespa/vespalib/util/mmap_file_allocator.h b/vespalib/src/vespa/vespalib/util/mmap_file_allocator.h index 96a69f50e43..f568a14572a 100644 --- a/vespalib/src/vespa/vespalib/util/mmap_file_allocator.h +++ b/vespalib/src/vespa/vespalib/util/mmap_file_allocator.h @@ -24,15 +24,11 @@ class MmapFileAllocator : public MemoryAllocator { struct SizeAndOffset { size_t size; uint64_t offset; - SizeAndOffset() - : SizeAndOffset(0u, 0u) - { - } - SizeAndOffset(size_t size_in, uint64_t offset_in) + SizeAndOffset() noexcept : SizeAndOffset(0u, 0u) { } + SizeAndOffset(size_t size_in, uint64_t offset_in) noexcept : size(size_in), offset(offset_in) - { - } + { } }; using Allocations = hash_map<void *, SizeAndOffset>; const vespalib::string _dir_name; @@ -55,9 +51,9 @@ class MmapFileAllocator : public MemoryAllocator { public: static constexpr uint32_t default_small_limit = 128_Ki; static constexpr uint32_t default_premmap_size = 1_Mi; - MmapFileAllocator(const vespalib::string& dir_name); + explicit MmapFileAllocator(const vespalib::string& dir_name); MmapFileAllocator(const vespalib::string& dir_name, uint32_t small_limit, uint32_t premmap_size); - ~MmapFileAllocator(); + ~MmapFileAllocator() override; PtrAndSize alloc(size_t sz) const override; void free(PtrAndSize alloc) const noexcept override; size_t resize_inplace(PtrAndSize, size_t) const override; |