I'm new to Perl, and I need to improve the performance of an application someone else wrote.
Profiling showed that the program is spending a lot of time in the XML::Simple
library.
Based on knowledge about how the application's use changed over time, we're suspecting that it is re-parsing the same XML data several times over.
Memoizing the XML parsing function seemed like a straightforward fix. The files it gets the XML data from are assumed not to change while the program runs, so let's just cache the results for each file.
Such function, the point-of-entry for the library, is XMLin.
My single change to the software was adding
use Memoize;
memoize('XMLin');
Trying to run in returns the error:
Not a HASH reference at C:\QuEST\Scripts\RangeAnalyzer/ParseETP.pl line 269.
Line 269 is:
@constantElements = @{$xml->{declarations}->{Package}->{declarations}->{Constant}};
... and $xml
is defined a few lines up as:
my $xml = XMLin($Filename, KeyAttr => {ConstValue => '', Operator => '', VariableRef => '', Variable => '', StateMachine => '', State => '', IfBlock => '', WhenBlock => '', SizeParameter => ''}, ForceArray => ['Variable', 'ConstValue', 'DataArrayOp', 'Constant']);
Undoing the change fixes the error.
Why did memoizing the function break its return value? How to fix it?
I noticed XML::Simple
is deprecated, and replacing it, preferrably with something faster, is in the list of things to try.
Nevertheless, this error broke my mental model of how memoization was supposed to work.
I'm using Perl 5.10.0.