# HG changeset patch # User Arun Giridhar # Date 1664980531 14400 # Node ID c5c8bf50449c97974f1a44ac08fced217a049aed # Parent e39634a768e753ffe787c89c4ccdcacd1ad09b13 Improve performance of isfield from linear time to constant time (bug #58105) Patch author: Anonymous in Bug #58105 Comment #7 https://savannah.gnu.org/bugs/index.php?58105#comment7 The isfield function was taking time proportional to the number of fields in a struct to determine whether something was a member or not. With this change, isfield performance becomes constant irrespective of the number of fields, speeding up performance by over 200X, and also allowing the use of struct as a hashmap if the user desires. ov-base.h: Add new isfield function declaration ov-base.cc: Add new isfield function definition ov-struct.h: Add new isfield function definitions ov-struct.cc: Do not call map_value ov.h: Add new isfield function definition diff -r e39634a768e7 -r c5c8bf50449c libinterp/octave-value/ov-base.cc --- a/libinterp/octave-value/ov-base.cc Wed Oct 05 10:55:13 2022 +0200 +++ b/libinterp/octave-value/ov-base.cc Wed Oct 05 10:35:31 2022 -0400 @@ -901,6 +901,12 @@ err_wrong_type_arg ("octave_base_value::map_keys()", type_name ()); } +bool +octave_base_value::isfield (const std::string&) const +{ + err_wrong_type_arg ("octave_base_value::isfield()", type_name ()); +} + std::size_t octave_base_value::nparents (void) const { diff -r e39634a768e7 -r c5c8bf50449c libinterp/octave-value/ov-base.h --- a/libinterp/octave-value/ov-base.h Wed Oct 05 10:55:13 2022 +0200 +++ b/libinterp/octave-value/ov-base.h Wed Oct 05 10:35:31 2022 -0400 @@ -670,6 +670,8 @@ virtual string_vector map_keys (void) const; + virtual bool isfield (const std::string&) const; + virtual std::size_t nparents (void) const; virtual std::list parent_class_name_list (void) const; diff -r e39634a768e7 -r c5c8bf50449c libinterp/octave-value/ov-struct.cc --- a/libinterp/octave-value/ov-struct.cc Wed Oct 05 10:55:13 2022 +0200 +++ b/libinterp/octave-value/ov-struct.cc Wed Oct 05 10:35:31 2022 -0400 @@ -1951,7 +1951,7 @@ if (args(0).isstruct ()) { - octave_map m = args(0).map_value (); + octave_value m = args(0); // FIXME: should this work for all types that can do // structure reference operations? diff -r e39634a768e7 -r c5c8bf50449c libinterp/octave-value/ov-struct.h --- a/libinterp/octave-value/ov-struct.h Wed Oct 05 10:55:13 2022 +0200 +++ b/libinterp/octave-value/ov-struct.h Wed Oct 05 10:35:31 2022 -0400 @@ -130,6 +130,9 @@ string_vector map_keys (void) const { return m_map.fieldnames (); } + bool isfield (const std::string& field_name) const + { return m_map.isfield (field_name); } + void print (std::ostream& os, bool pr_as_read_syntax = false); void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; @@ -256,6 +259,9 @@ string_vector map_keys (void) const { return m_map.fieldnames (); } + bool isfield (const std::string& field_name) const + { return m_map.isfield (field_name); } + void print (std::ostream& os, bool pr_as_read_syntax = false); void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; diff -r e39634a768e7 -r c5c8bf50449c libinterp/octave-value/ov.h --- a/libinterp/octave-value/ov.h Wed Oct 05 10:55:13 2022 +0200 +++ b/libinterp/octave-value/ov.h Wed Oct 05 10:35:31 2022 -0400 @@ -1070,6 +1070,9 @@ string_vector map_keys (void) const { return m_rep->map_keys (); } + bool isfield (const std::string& field_name) const + { return m_rep->isfield (field_name); } + std::size_t nparents (void) const { return m_rep->nparents (); }