Skip to content

Commit

Permalink
perf: Long URIs may cause high CPU usage
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored Jul 10, 2023
1 parent 75cbcd6 commit f0536df
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 86 deletions.
17 changes: 17 additions & 0 deletions parser_library/src/context/hlasm_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,23 @@ location hlasm_context::current_statement_location(bool consider_macros) const
}
}

const utils::resource::resource_location& hlasm_context::current_statement_source(bool consider_macros) const
{
if (!consider_macros || source_stack_.size() > 1 || scope_stack_.size() == 1)
{
if (source_stack_.back().copy_stack.size())
return source_stack_.back().copy_stack.back().definition_location()->resource_loc;
else
return source_stack_.back().current_instruction.resource_loc;
}
else
{
const auto& mac_invo = scope_stack_.back().this_macro;

return mac_invo->copy_nests[mac_invo->current_statement].back().loc.resource_loc;
}
}

const std::deque<code_scope>& hlasm_context::scope_stack() const { return scope_stack_; }

const source_context& hlasm_context::current_source() const { return source_stack_.back(); }
Expand Down
1 change: 1 addition & 0 deletions parser_library/src/context/hlasm_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class hlasm_context
processing_frame processing_stack_top(bool consider_macros = true);
processing_stack_details_t processing_stack_details();
location current_statement_location(bool consider_macros = true) const;
const utils::resource::resource_location& current_statement_source(bool consider_macros = true) const;

// gets macro nest
const std::deque<code_scope>& scope_stack() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ std::optional<stmt_lines_range> get_stmt_lines_range(const semantics::core_state
}
} // namespace

utils::resource::resource_location hit_count_analyzer::get_current_stmt_rl(processing_kind proc_kind) const
const utils::resource::resource_location& hit_count_analyzer::get_current_stmt_rl(processing_kind proc_kind) const
{
return m_ctx.current_statement_location(proc_kind != processing_kind::LOOKAHEAD).resource_loc;
return m_ctx.current_statement_source(proc_kind != processing_kind::LOOKAHEAD);
}

hit_count_entry& hit_count_analyzer::get_hc_entry_reference(const utils::resource::resource_location& rl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class hit_count_analyzer final : public statement_analyzer
statement_type m_next_stmt_type = statement_type::REGULAR;
size_t m_macro_level = 0;

utils::resource::resource_location get_current_stmt_rl(processing_kind proc_kind) const;
const utils::resource::resource_location& get_current_stmt_rl(processing_kind proc_kind) const;
hit_count_entry& get_hc_entry_reference(const utils::resource::resource_location& rl);

statement_type get_stmt_type(const semantics::core_statement& statement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ bool lsp_analyzer::analyze(const context::hlasm_statement& statement,
break;
}

assign_statement_occurrences(hlasm_ctx_.current_statement_location().resource_loc);
assign_statement_occurrences(hlasm_ctx_.current_statement_source());

return false;
}
Expand Down Expand Up @@ -395,11 +395,8 @@ void lsp_analyzer::add_var_def(const semantics::variable_symbol* var, context::S
!= opencode_var_defs_.end())
return;

opencode_var_defs_.emplace_back(var->access_basic()->name,
type,
global,
hlasm_ctx_.current_statement_location().resource_loc,
var->symbol_range.start);
opencode_var_defs_.emplace_back(
var->access_basic()->name, type, global, hlasm_ctx_.current_statement_source(), var->symbol_range.start);
}

void lsp_analyzer::add_copy_operand(context::id_index name, const range& operand_range, bool evaluated_model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,8 @@ void macrodef_processor::process_sequence_symbol(const semantics::label_si& labe
{
auto& sym = result_.sequence_symbols[seq.name];
if (!sym)
sym = std::make_unique<context::macro_sequence_symbol>(seq.name,
location(label.field_range.start, hlasm_ctx.current_statement_location().resource_loc),
curr_line_);
sym = std::make_unique<context::macro_sequence_symbol>(
seq.name, location(label.field_range.start, hlasm_ctx.current_statement_source()), curr_line_);
}
}
}
Expand Down
37 changes: 32 additions & 5 deletions utils/include/utils/resource_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#define HLASMPLUGIN_UTILS_RESOURCE_LOCATION_H

#include <compare>
#include <memory>
#include <string>
#include <string_view>

namespace hlasm_plugin::utils::resource {

Expand All @@ -32,7 +34,7 @@ class resource_location
std::string get_path() const;
std::string to_presentable(bool debug = false) const;

bool empty() const { return m_uri.empty(); }
bool empty() const { return !m_data; }

bool is_local() const;
static bool is_local(std::string_view uri);
Expand All @@ -51,7 +53,7 @@ class resource_location
static resource_location join(resource_location rl, std::string_view other);

// Relative reference resolution based on RFC 3986
void relative_reference_resolution(std::string_view other);
resource_location& relative_reference_resolution(std::string_view other);
static resource_location relative_reference_resolution(resource_location rl, std::string_view other);

resource_location& replace_filename(std::string_view other);
Expand All @@ -65,15 +67,40 @@ class resource_location
bool is_prefix_of(const resource_location& candidate) const;
static bool is_prefix(const resource_location& candidate, const resource_location& base);

std::strong_ordering operator<=>(const resource_location& rl) const noexcept = default;
bool operator==(const resource_location& rl) const noexcept
{
return m_data == rl.m_data
|| m_data && rl.m_data && m_data->hash == rl.m_data->hash && m_data->uri == rl.m_data->uri;
}
std::strong_ordering operator<=>(const resource_location& rl) const noexcept
{
if (m_data == rl.m_data)
return std::strong_ordering::equal;

std::string_view l;
std::string_view r;
if (m_data)
l = m_data->uri;
if (rl.m_data)
r = rl.m_data->uri;
return l.compare(r) <=> 0; // libc++14
}

private:
std::string m_uri;
struct data
{
data(std::string s);
size_t hash;
std::string uri;
};
std::shared_ptr<const data> m_data;

friend struct resource_location_hasher;
};

struct resource_location_hasher
{
std::size_t operator()(const resource_location& rl) const;
std::size_t operator()(const resource_location& rl) const { return rl.m_data ? rl.m_data->hash : 0; }
};

} // namespace hlasm_plugin::utils::resource
Expand Down
Loading

0 comments on commit f0536df

Please sign in to comment.