Skip to content

Commit

Permalink
refactor(configuration): prefer Vector over std::vector
Browse files Browse the repository at this point in the history
Reduce our use of std::vector to reduce binary bloat:
#689
  • Loading branch information
strager committed Nov 21, 2023
1 parent 70c1740 commit b58ae97
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 137 deletions.
5 changes: 4 additions & 1 deletion plugin/vscode/quick-lint-js/vscode/qljs-workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,10 @@ void QLJS_Workspace::check_for_config_file_changes_from_thread(
}

void QLJS_Workspace::check_for_config_file_changes(::Napi::Env env) {
std::vector<Configuration_Change> changes = this->config_loader_.refresh();
Monotonic_Allocator temporary_allocator(
"QLJS_Workspace::check_for_config_file_changes");
Span<Configuration_Change> changes =
this->config_loader_.refresh(&temporary_allocator);
for (const Configuration_Change& change : changes) {
QLJS_DEBUG_LOG("Configuration changed for %s\n",
change.watched_path->c_str());
Expand Down
8 changes: 5 additions & 3 deletions src/quick-lint-js/configuration/configuration-loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,10 @@ void Configuration_Loader::unwatch_all_files() {
this->watched_input_paths_.clear();
}

std::vector<Configuration_Change> Configuration_Loader::refresh() {
std::vector<Configuration_Change> changes;
Span<Configuration_Change> Configuration_Loader::refresh(
Monotonic_Allocator* allocator) {
Vector<Configuration_Change> changes("Configuration_Loader::refresh changes",
allocator);

Hash_Map<Canonical_Path, Loaded_Config_File> loaded_config_files =
std::move(this->loaded_config_files_);
Expand Down Expand Up @@ -515,7 +517,7 @@ std::vector<Configuration_Change> Configuration_Loader::refresh() {
}
}

return changes;
return changes.release_to_span();
}

bool Configuration_Loader::is_config_file_path(
Expand Down
4 changes: 3 additions & 1 deletion src/quick-lint-js/configuration/configuration-loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
#include <optional>
#include <quick-lint-js/configuration/configuration.h>
#include <quick-lint-js/container/hash-map.h>
#include <quick-lint-js/container/monotonic-allocator.h>
#include <quick-lint-js/container/result.h>
#include <quick-lint-js/container/vector.h>
#include <quick-lint-js/diag/buffering-diag-reporter.h>
#include <quick-lint-js/io/file-canonical.h>
#include <quick-lint-js/io/file.h>
#include <quick-lint-js/port/memory-resource.h>
#include <quick-lint-js/port/span.h>
#include <string>
#include <string_view>
#include <vector>
Expand Down Expand Up @@ -140,7 +142,7 @@ class Configuration_Loader {
// quick-lint-js.config's content changed.
// * A quick-lint-js.config file didn't exist, and now one does.
// * A quick-lint-js.config file was moved into an ancestor directory.
std::vector<Configuration_Change> refresh();
Span<Configuration_Change> refresh(Monotonic_Allocator*);

// Returns true if the path might possibly be a configuration file detected by
// load_for_file or watch_and_load_for_file.
Expand Down
29 changes: 17 additions & 12 deletions src/quick-lint-js/lsp/lsp-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,13 @@ void Linting_LSP_Server_Handler::handle_notification(
}

void Linting_LSP_Server_Handler::filesystem_changed() {
std::vector<Configuration_Change> config_changes =
this->config_loader_.refresh();
Monotonic_Allocator temporary_allocator(
"Linting_LSP_Server_Handler::filesystem_changed");
Span<Configuration_Change> config_changes =
this->config_loader_.refresh(&temporary_allocator);
{
Lock_Ptr<LSP_Documents> documents = this->documents_.lock();
this->handle_config_file_changes(
documents, Span<const Configuration_Change>(config_changes));
this->handle_config_file_changes(documents, config_changes);
}
}

Expand Down Expand Up @@ -366,10 +367,12 @@ void Linting_LSP_Server_Handler::handle_text_document_did_change_notification(

switch (doc.type) {
case LSP_Documents::Document_Type::config: {
std::vector<Configuration_Change> config_changes =
this->config_loader_.refresh();
this->handle_config_file_changes(
documents, Span<const Configuration_Change>(config_changes));
Monotonic_Allocator temporary_allocator(
"Linting_LSP_Server_Handler::handle_text_document_did_change_"
"notification");
Span<Configuration_Change> config_changes =
this->config_loader_.refresh(&temporary_allocator);
this->handle_config_file_changes(documents, config_changes);
break;
}

Expand Down Expand Up @@ -516,12 +519,14 @@ void Linting_LSP_Server_Handler::handle_text_document_did_open_notification(
*config_file, notification.uri.json, doc->version_json,
config_diagnostics_json);

std::vector<Configuration_Change> config_changes =
this->config_loader_.refresh();
Monotonic_Allocator temporary_allocator(
"Linting_LSP_Server_Handler::handle_text_document_did_open_"
"notification");
Span<Configuration_Change> config_changes =
this->config_loader_.refresh(&temporary_allocator);
{
Lock_Ptr<LSP_Documents> documents = this->documents_.lock();
this->handle_config_file_changes(
documents, Span<const Configuration_Change>(config_changes));
this->handle_config_file_changes(documents, config_changes);
}

doc_ptr = std::move(doc);
Expand Down
Loading

0 comments on commit b58ae97

Please sign in to comment.