From b04c959621562de18552aa1a002b52f311a7185e Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Fri, 24 May 2024 11:35:59 -0400 Subject: [PATCH] [ruby/prism] Remove various unused memsize infra https://github.com/ruby/prism/commit/283938ed1f --- prism/extension.c | 27 -------------- prism/node.h | 21 ----------- prism/templates/src/node.c.erb | 64 ---------------------------------- prism/util/pm_constant_pool.c | 8 ----- prism/util/pm_constant_pool.h | 8 ----- prism/util/pm_integer.c | 8 ----- prism/util/pm_integer.h | 8 ----- prism/util/pm_string.c | 12 ------- prism/util/pm_string.h | 8 ----- test/prism/memsize_test.rb | 17 --------- 10 files changed, 181 deletions(-) delete mode 100644 test/prism/memsize_test.rb diff --git a/prism/extension.c b/prism/extension.c index e67ea9b9dc..856bcb9654 100644 --- a/prism/extension.c +++ b/prism/extension.c @@ -1065,32 +1065,6 @@ named_captures(VALUE self, VALUE source) { return names; } -/** - * call-seq: - * Debug::memsize(source) -> { length: xx, memsize: xx, node_count: xx } - * - * Return a hash of information about the given source string's memory usage. - */ -static VALUE -memsize(VALUE self, VALUE string) { - pm_parser_t parser; - size_t length = RSTRING_LEN(string); - pm_parser_init(&parser, (const uint8_t *) RSTRING_PTR(string), length, NULL); - - pm_node_t *node = pm_parse(&parser); - pm_memsize_t memsize; - pm_node_memsize(node, &memsize); - - pm_node_destroy(&parser, node); - pm_parser_free(&parser); - - VALUE result = rb_hash_new(); - rb_hash_aset(result, ID2SYM(rb_intern("length")), INT2FIX(length)); - rb_hash_aset(result, ID2SYM(rb_intern("memsize")), INT2FIX(memsize.memsize)); - rb_hash_aset(result, ID2SYM(rb_intern("node_count")), INT2FIX(memsize.node_count)); - return result; -} - /** * call-seq: * Debug::profile_file(filepath) -> nil @@ -1347,7 +1321,6 @@ Init_prism(void) { // internal tasks. We expose these to make them easier to test. VALUE rb_cPrismDebug = rb_define_module_under(rb_cPrism, "Debug"); rb_define_singleton_method(rb_cPrismDebug, "named_captures", named_captures, 1); - rb_define_singleton_method(rb_cPrismDebug, "memsize", memsize, 1); rb_define_singleton_method(rb_cPrismDebug, "profile_file", profile_file, 1); rb_define_singleton_method(rb_cPrismDebug, "format_errors", format_errors, 2); diff --git a/prism/node.h b/prism/node.h index 8736e59a94..e8686a327c 100644 --- a/prism/node.h +++ b/prism/node.h @@ -56,27 +56,6 @@ void pm_node_list_free(pm_node_list_t *list); */ PRISM_EXPORTED_FUNCTION void pm_node_destroy(pm_parser_t *parser, struct pm_node *node); -/** - * This struct stores the information gathered by the pm_node_memsize function. - * It contains both the memory footprint and additionally metadata about the - * shape of the tree. - */ -typedef struct { - /** The total memory footprint of the node and all of its children. */ - size_t memsize; - - /** The number of children the node has. */ - size_t node_count; -} pm_memsize_t; - -/** - * Calculates the memory footprint of a given node. - * - * @param node The node to calculate the memory footprint of. - * @param memsize The memory footprint of the node and all of its children. - */ -PRISM_EXPORTED_FUNCTION void pm_node_memsize(pm_node_t *node, pm_memsize_t *memsize); - /** * Returns a string representation of the given node type. * diff --git a/prism/templates/src/node.c.erb b/prism/templates/src/node.c.erb index e1c35f5a45..deb2ea49fc 100644 --- a/prism/templates/src/node.c.erb +++ b/prism/templates/src/node.c.erb @@ -1,19 +1,6 @@ #line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>" #include "prism/node.h" -static void -pm_node_memsize_node(pm_node_t *node, pm_memsize_t *memsize); - -/** - * Calculate the size of the node list in bytes. - */ -static size_t -pm_node_list_memsize(pm_node_list_t *node_list, pm_memsize_t *memsize) { - pm_node_t *node; - PM_NODE_LIST_FOREACH(node_list, index, node) pm_node_memsize_node(node, memsize); - return sizeof(pm_node_list_t) + (node_list->capacity * sizeof(pm_node_t *)); -} - /** * Attempts to grow the node list to the next size. If there is already * capacity in the list, this function does nothing. Otherwise it reallocates @@ -156,57 +143,6 @@ pm_node_destroy(pm_parser_t *parser, pm_node_t *node) { xfree(node); } -static void -pm_node_memsize_node(pm_node_t *node, pm_memsize_t *memsize) { - memsize->node_count++; - - switch (PM_NODE_TYPE(node)) { - // We do not calculate memsize of a ScopeNode - // as it should never be generated - case PM_SCOPE_NODE: - return; - <%- nodes.each do |node| -%> -#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>" - case <%= node.type %>: { - pm_<%= node.human %>_t *cast = (pm_<%= node.human %>_t *) node; - memsize->memsize += sizeof(*cast); - <%- node.fields.each do |field| -%> - <%- case field -%> - <%- when Prism::Template::ConstantField, Prism::Template::OptionalConstantField, Prism::Template::UInt8Field, Prism::Template::UInt32Field, Prism::Template::FlagsField, Prism::Template::LocationField, Prism::Template::OptionalLocationField, Prism::Template::DoubleField -%> - <%- when Prism::Template::NodeField -%> - pm_node_memsize_node((pm_node_t *)cast-><%= field.name %>, memsize); - <%- when Prism::Template::OptionalNodeField -%> - if (cast-><%= field.name %> != NULL) { - pm_node_memsize_node((pm_node_t *)cast-><%= field.name %>, memsize); - } - <%- when Prism::Template::StringField -%> - memsize->memsize += (pm_string_memsize(&cast-><%= field.name %>) - sizeof(pm_string_t)); - <%- when Prism::Template::NodeListField -%> - memsize->memsize += (pm_node_list_memsize(&cast-><%= field.name %>, memsize) - sizeof(pm_node_list_t)); - <%- when Prism::Template::ConstantListField -%> - memsize->memsize += (pm_constant_id_list_memsize(&cast-><%= field.name %>) - sizeof(pm_constant_id_list_t)); - <%- when Prism::Template::IntegerField -%> - memsize->memsize += (pm_integer_memsize(&cast-><%= field.name %>) - sizeof(pm_integer_t)); - <%- else -%> - <%- raise -%> - <%- end -%> - <%- end -%> - break; - } - <%- end -%> -#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>" - } -} - -/** - * Calculates the memory footprint of a given node. - */ -PRISM_EXPORTED_FUNCTION void -pm_node_memsize(pm_node_t *node, pm_memsize_t *memsize) { - *memsize = (pm_memsize_t) { .memsize = 0, .node_count = 0 }; - pm_node_memsize_node(node, memsize); -} - /** * Returns a string representation of the given node type. */ diff --git a/prism/util/pm_constant_pool.c b/prism/util/pm_constant_pool.c index 2a3203f4c4..624002cec9 100644 --- a/prism/util/pm_constant_pool.c +++ b/prism/util/pm_constant_pool.c @@ -61,14 +61,6 @@ pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id) { return false; } -/** - * Get the memory size of a list of constant ids. - */ -size_t -pm_constant_id_list_memsize(pm_constant_id_list_t *list) { - return sizeof(pm_constant_id_list_t) + (list->capacity * sizeof(pm_constant_id_t)); -} - /** * Free the memory associated with a list of constant ids. */ diff --git a/prism/util/pm_constant_pool.h b/prism/util/pm_constant_pool.h index 0fe16858a0..6df23f8f50 100644 --- a/prism/util/pm_constant_pool.h +++ b/prism/util/pm_constant_pool.h @@ -87,14 +87,6 @@ void pm_constant_id_list_insert(pm_constant_id_list_t *list, size_t index, pm_co */ bool pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id); -/** - * Get the memory size of a list of constant ids. - * - * @param list The list to get the memory size of. - * @return The memory size of the list. - */ -size_t pm_constant_id_list_memsize(pm_constant_id_list_t *list); - /** * Free the memory associated with a list of constant ids. * diff --git a/prism/util/pm_integer.c b/prism/util/pm_integer.c index ab6ac78b5c..137f736536 100644 --- a/prism/util/pm_integer.c +++ b/prism/util/pm_integer.c @@ -536,14 +536,6 @@ pm_integer_parse(pm_integer_t *integer, pm_integer_base_t base, const uint8_t *s integer->value = (uint32_t) value; } -/** - * Return the memory size of the integer. - */ -size_t -pm_integer_memsize(const pm_integer_t *integer) { - return sizeof(pm_integer_t) + integer->length * sizeof(uint32_t); -} - /** * Compare two integers. This function returns -1 if the left integer is less * than the right integer, 0 if they are equal, and 1 if the left integer is diff --git a/prism/util/pm_integer.h b/prism/util/pm_integer.h index 1ede1f12b9..91b28ad2f3 100644 --- a/prism/util/pm_integer.h +++ b/prism/util/pm_integer.h @@ -84,14 +84,6 @@ typedef enum { */ void pm_integer_parse(pm_integer_t *integer, pm_integer_base_t base, const uint8_t *start, const uint8_t *end); -/** - * Return the memory size of the integer. - * - * @param integer The integer to get the memory size of. - * @return The size of the memory associated with the integer. - */ -size_t pm_integer_memsize(const pm_integer_t *integer); - /** * Compare two integers. This function returns -1 if the left integer is less * than the right integer, 0 if they are equal, and 1 if the left integer is diff --git a/prism/util/pm_string.c b/prism/util/pm_string.c index 8342edc34e..dfc121b6a2 100644 --- a/prism/util/pm_string.c +++ b/prism/util/pm_string.c @@ -245,18 +245,6 @@ pm_string_file_init(pm_string_t *string, const char *filepath) { #endif } -/** - * Returns the memory size associated with the string. - */ -size_t -pm_string_memsize(const pm_string_t *string) { - size_t size = sizeof(pm_string_t); - if (string->type == PM_STRING_OWNED) { - size += string->length; - } - return size; -} - /** * Ensure the string is owned. If it is not, then reinitialize it as owned and * copy over the previous source. diff --git a/prism/util/pm_string.h b/prism/util/pm_string.h index a68e2a7c91..d23792c0ba 100644 --- a/prism/util/pm_string.h +++ b/prism/util/pm_string.h @@ -120,14 +120,6 @@ PRISM_EXPORTED_FUNCTION bool pm_string_mapped_init(pm_string_t *string, const ch */ PRISM_EXPORTED_FUNCTION bool pm_string_file_init(pm_string_t *string, const char *filepath); -/** - * Returns the memory size associated with the string. - * - * @param string The string to get the memory size of. - * @return The size of the memory associated with the string. - */ -size_t pm_string_memsize(const pm_string_t *string); - /** * Ensure the string is owned. If it is not, then reinitialize it as owned and * copy over the previous source. diff --git a/test/prism/memsize_test.rb b/test/prism/memsize_test.rb deleted file mode 100644 index d7e1448dbc..0000000000 --- a/test/prism/memsize_test.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -require_relative "test_helper" - -return if Prism::BACKEND == :FFI - -module Prism - class MemsizeTest < TestCase - def test_memsize - result = Debug.memsize("2 + 3") - - assert_equal 5, result[:length] - assert_kind_of Integer, result[:memsize] - assert_equal 6, result[:node_count] - end - end -end