[ruby/prism] Remove various unused memsize infra
https://github.com/ruby/prism/commit/283938ed1f
This commit is contained in:
parent
b8681c2e37
commit
b04c959621
@ -1065,32 +1065,6 @@ named_captures(VALUE self, VALUE source) {
|
|||||||
return names;
|
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:
|
* call-seq:
|
||||||
* Debug::profile_file(filepath) -> nil
|
* Debug::profile_file(filepath) -> nil
|
||||||
@ -1347,7 +1321,6 @@ Init_prism(void) {
|
|||||||
// internal tasks. We expose these to make them easier to test.
|
// internal tasks. We expose these to make them easier to test.
|
||||||
VALUE rb_cPrismDebug = rb_define_module_under(rb_cPrism, "Debug");
|
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, "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, "profile_file", profile_file, 1);
|
||||||
rb_define_singleton_method(rb_cPrismDebug, "format_errors", format_errors, 2);
|
rb_define_singleton_method(rb_cPrismDebug, "format_errors", format_errors, 2);
|
||||||
|
|
||||||
|
21
prism/node.h
21
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);
|
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.
|
* Returns a string representation of the given node type.
|
||||||
*
|
*
|
||||||
|
@ -1,19 +1,6 @@
|
|||||||
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
|
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
|
||||||
#include "prism/node.h"
|
#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
|
* 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
|
* 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);
|
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.
|
* Returns a string representation of the given node type.
|
||||||
*/
|
*/
|
||||||
|
@ -61,14 +61,6 @@ pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id) {
|
|||||||
return false;
|
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.
|
* Free the memory associated with a list of constant ids.
|
||||||
*/
|
*/
|
||||||
|
@ -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);
|
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.
|
* Free the memory associated with a list of constant ids.
|
||||||
*
|
*
|
||||||
|
@ -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;
|
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
|
* 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
|
* than the right integer, 0 if they are equal, and 1 if the left integer is
|
||||||
|
@ -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);
|
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
|
* 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
|
* than the right integer, 0 if they are equal, and 1 if the left integer is
|
||||||
|
@ -245,18 +245,6 @@ pm_string_file_init(pm_string_t *string, const char *filepath) {
|
|||||||
#endif
|
#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
|
* Ensure the string is owned. If it is not, then reinitialize it as owned and
|
||||||
* copy over the previous source.
|
* copy over the previous source.
|
||||||
|
@ -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);
|
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
|
* Ensure the string is owned. If it is not, then reinitialize it as owned and
|
||||||
* copy over the previous source.
|
* copy over the previous source.
|
||||||
|
@ -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
|
|
Loading…
x
Reference in New Issue
Block a user