Skip to content

Commit

Permalink
OS::readFile: use SmallVector::resize_for_overwrite for minor efficie…
Browse files Browse the repository at this point in the history
…ncy gain
  • Loading branch information
MikePopoloski committed Jul 25, 2023
1 parent 6b556fb commit 9bdde5d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
11 changes: 6 additions & 5 deletions include/slang/text/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "slang/text/SourceLocation.h"
#include "slang/util/Hash.h"
#include "slang/util/SmallVector.h"
#include "slang/util/Util.h"

namespace slang {
Expand Down Expand Up @@ -146,7 +147,7 @@ class SLANG_EXPORT SourceManager {

/// Instead of loading source from a file, move it from text already in memory.
/// Pretend it came from a file located at @a path.
SourceBuffer assignBuffer(std::string_view path, std::vector<char>&& buffer,
SourceBuffer assignBuffer(std::string_view path, SmallVector<char>&& buffer,
SourceLocation includedFrom = SourceLocation(),
const SourceLibrary* library = nullptr);

Expand Down Expand Up @@ -227,12 +228,12 @@ class SLANG_EXPORT SourceManager {
// Stores actual file contents and metadata; only one per loaded file
struct FileData {
const std::string name; // name of the file
const std::vector<char> mem; // file contents
const SmallVector<char> mem; // file contents
std::vector<size_t> lineOffsets; // cache of compute line offsets
const std::filesystem::path* const directory; // directory in which the file exists
const std::filesystem::path fullPath; // full path to the file

FileData(const std::filesystem::path* directory, std::string name, std::vector<char>&& data,
FileData(const std::filesystem::path* directory, std::string name, SmallVector<char>&& data,
std::filesystem::path fullPath) :
name(std::move(name)),
mem(std::move(data)), directory(directory), fullPath(std::move(fullPath)) {}
Expand Down Expand Up @@ -319,7 +320,7 @@ class SLANG_EXPORT SourceManager {
const SourceLibrary* library);
SourceBuffer cacheBuffer(std::filesystem::path&& path, std::string&& pathStr,
SourceLocation includedFrom, const SourceLibrary* library,
std::vector<char>&& buffer);
SmallVector<char>&& buffer);

template<IsLock TLock>
size_t getRawLineNumber(SourceLocation location, TLock& lock) const;
Expand All @@ -339,7 +340,7 @@ class SLANG_EXPORT SourceManager {
template<IsLock TLock>
SourceRange getExpansionRangeImpl(SourceLocation location, TLock& lock) const;

static void computeLineOffsets(const std::vector<char>& buffer,
static void computeLineOffsets(const SmallVector<char>& buffer,
std::vector<size_t>& offsets) noexcept;
};

Expand Down
4 changes: 2 additions & 2 deletions include/slang/util/OS.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#pragma once

#include <filesystem>
#include <vector>

#include "slang/util/ScopeGuard.h"
#include "slang/util/SmallVector.h"
#include "slang/util/Util.h"

namespace fmt {
Expand Down Expand Up @@ -42,7 +42,7 @@ class SLANG_EXPORT OS {
/// Reads a file from @a path into memory. If successful, the bytes are placed
/// into @a buffer -- otherwise, returns false.
/// Note that the buffer will be null-terminated.
static std::error_code readFile(const std::filesystem::path& path, std::vector<char>& buffer);
static std::error_code readFile(const std::filesystem::path& path, SmallVector<char>& buffer);

/// Prints text to stdout.
static void print(std::string_view text);
Expand Down
2 changes: 1 addition & 1 deletion source/driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ bool Driver::processCommandFiles(std::string_view pattern, bool makeRelative) {
return onError(pattern, globEc);

for (auto& path : files) {
std::vector<char> buffer;
SmallVector<char> buffer;
if (auto readEc = OS::readFile(path, buffer))
return onError(getU8Str(path), readEc);

Expand Down
10 changes: 5 additions & 5 deletions source/text/SourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ SourceBuffer SourceManager::assignText(std::string_view path, std::string_view t
path = temp;
}

std::vector<char> buffer;
SmallVector<char> buffer;
buffer.insert(buffer.end(), text.begin(), text.end());
if (buffer.empty() || buffer.back() != '\0')
buffer.push_back('\0');

return assignBuffer(path, std::move(buffer), includedFrom, library);
}

SourceBuffer SourceManager::assignBuffer(std::string_view bufferPath, std::vector<char>&& buffer,
SourceBuffer SourceManager::assignBuffer(std::string_view bufferPath, SmallVector<char>&& buffer,
SourceLocation includedFrom,
const SourceLibrary* library) {
// first see if we have this file cached
Expand Down Expand Up @@ -453,7 +453,7 @@ SourceManager::BufferOrError SourceManager::openCached(const fs::path& fullPath,
}

// do the read
std::vector<char> buffer;
SmallVector<char> buffer;
if (std::error_code ec = OS::readFile(absPath, buffer)) {
std::unique_lock lock(mutex);
lookupCache.emplace(pathStr, std::pair{nullptr, ec});
Expand All @@ -466,7 +466,7 @@ SourceManager::BufferOrError SourceManager::openCached(const fs::path& fullPath,

SourceBuffer SourceManager::cacheBuffer(fs::path&& path, std::string&& pathStr,
SourceLocation includedFrom, const SourceLibrary* library,
std::vector<char>&& buffer) {
SmallVector<char>&& buffer) {
std::string name;
if (!disableProximatePaths) {
std::error_code ec;
Expand Down Expand Up @@ -593,7 +593,7 @@ SourceLocation SourceManager::getOriginalLocImpl(SourceLocation location, TLock&
return std::get<ExpansionInfo>(bufferEntries[buffer.getId()]).originalLoc + location.offset();
}

void SourceManager::computeLineOffsets(const std::vector<char>& buffer,
void SourceManager::computeLineOffsets(const SmallVector<char>& buffer,
std::vector<size_t>& offsets) noexcept {
// first line always starts at offset 0
offsets.push_back(0);
Expand Down
12 changes: 6 additions & 6 deletions source/util/OS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ bool OS::fileSupportsColors(FILE* file) {
return fileSupportsColors(_fileno(file));
}

std::error_code OS::readFile(const fs::path& path, std::vector<char>& buffer) {
std::error_code OS::readFile(const fs::path& path, SmallVector<char>& buffer) {
HANDLE handle = ::CreateFileW(path.native().c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
Expand All @@ -93,7 +93,7 @@ std::error_code OS::readFile(const fs::path& path, std::vector<char>& buffer) {
}
else {
size_t fileSize = (size_t(fileInfo.nFileSizeHigh) << 32) + fileInfo.nFileSizeLow;
buffer.resize(fileSize + 1);
buffer.resize_for_overwrite(fileSize + 1);

char* buf = buffer.data();
while (fileSize) {
Expand Down Expand Up @@ -126,7 +126,7 @@ std::error_code OS::readFile(const fs::path& path, std::vector<char>& buffer) {

size_t currSize = 0;
while (true) {
buffer.resize(currSize + ChunkSize);
buffer.resize_for_overwrite(currSize + ChunkSize + 1);

DWORD bytesRead = 0;
BOOL result = ::ReadFile(handle, buffer.data() + currSize, ChunkSize, &bytesRead, NULL);
Expand Down Expand Up @@ -168,7 +168,7 @@ bool OS::fileSupportsColors(FILE* file) {
return fileSupportsColors(fileno(file));
}

std::error_code OS::readFile(const fs::path& path, std::vector<char>& buffer) {
std::error_code OS::readFile(const fs::path& path, SmallVector<char>& buffer) {
int fd;
while (true) {
fd = ::open(path.native().c_str(), O_RDONLY | O_CLOEXEC);
Expand All @@ -186,7 +186,7 @@ std::error_code OS::readFile(const fs::path& path, std::vector<char>& buffer) {
}
else if (S_ISREG(status.st_mode) || S_ISBLK(status.st_mode)) {
auto fileSize = (size_t)status.st_size;
buffer.resize(fileSize + 1);
buffer.resize_for_overwrite(fileSize + 1);

char* buf = buffer.data();
while (fileSize) {
Expand Down Expand Up @@ -217,7 +217,7 @@ std::error_code OS::readFile(const fs::path& path, std::vector<char>& buffer) {

size_t currSize = 0;
while (true) {
buffer.resize(currSize + ChunkSize);
buffer.resize_for_overwrite(currSize + ChunkSize + 1);

ssize_t numRead = ::read(fd, buffer.data() + currSize, ChunkSize);
if (numRead < 0) {
Expand Down

0 comments on commit 9bdde5d

Please sign in to comment.