I have a program that plays solitaire.
There is a humungous data structure %positions. It looks something like this:
do {
require MIME::Base64;
(
MIME::Base64::decode("2qyANnIPJvTGkGfTQ8PTHtBUaKY4qm6eP8eTiC2MFzej
+iJLwnIwZayMREOmyRFgJ+3IHdV1fja3Y4lq3smfArLVhRVYYzbV1ZOVTw9aelSthia4Bq
+2pAw5Ws/StQvBNao1fpirK8Zq9Q4iDDuQY="),
{
desc => "move 4C from col 4 to 0 turning a card (88)",
metric => 5295,
parent => MIME::Base64::decode("2qyANnIPJvTGkGfTQ8PTHtBUaKY4qm6e
+P8eTiC2MFzejiJLwnIwZayMREOmytBL25Q7qur4aW7HFtW5lz4BZa8OKrDCaa2vCyqeGR
+daelSthia4Bq2pAw5Ws/StQvBNao1fpirK8Zq9Q4iDDuQY="),
},
MIME::Base64::decode("2qyANnIPJvTGkGfTQ8PTHtBUaKY4qm6eP8eTiC2MFzej
+iJLwnIwZayMREOmytBL25Q7qur4aW7HFtW5lz4BZa4NnRVYYzbU1YeVTwyLr6MqVsKwBq
+2pAw5Ws/StQvBNao1fpirK8Zq9Q4iDDuQY="),
{
desc => "move 9H from col 5 to 3 turning a card (92)",
metric => 5291,
parent => MIME::Base64::decode("2qyANnIPJvTGkGfTQ8PTHtBUaKY4qm6e
+P8eTiC2MFzejiJLwnIwZayMREOmytBL25Q7qur4aW7HFtW5lz4BZa8OKrDCaa2vCyqeGR
+daelSthia4Bq2pAw5Ws/StQvBNao1fpirK8Zq9Q4iDDuQY="),
},
...
MIME::Base64::decode("2qyANnIPJvTGkGfTQ8PTHtBUaKY4qm6eP8eTiC2MFzej
+iJLLknQl7Msd1HUdshpbscW1DrGyZ8CstQG5IiuM5tqa0F751LDI2hNbuRKW6BrAXFUDG
+q5k7ZetQPFOaI3OrdIVZXnNhq9Q4iDDuQY="),
{
desc => "deal cards (-208)",
metric => 5591,
parent => MIME::Base64::decode("2qyANnIPJvTGkGfTQ8PTHtBUaKY4qm6e
+P8eTiC2MFzejiJLwnIwZayMREOmytBL25Q7qur4aW7HFtW5lz4BZa8OKrDCaa2vCyqeGR
+daelSthia4Bq2pAw5Ws/StQvBNao1fpirK8Zq9Q4iDDuQY="),
},
MIME::Base64::decode("2qyANnIPJvTGkGfTQ8PTHtBUaKY4qm6eP8eTiC2MFzej
+iJLwnIwZayMREOmytBL25Q7qur4aW7HFtW5lz4BZa8OKrDCaa2vCyqeGRdaelSthia4Bq
+2pAw5Ws/StQvBNao1fpirK8Zq9Q4iDDuQY="),
{ desc => "initial deal", metric => 5383 },
);
}
After a couple of hours, the memory used by the perl app is several gigs in size, and I assume its due to this guy. Anyway, after running for several hours, my program gets tired of playing that hand and wants to play another. It goes to a re-initialization script that has a segment like this:
$|=1;
open F, ">>state.txt";
my $position = Spider::unmash($best);
movestack($position, $best, ">lastpos".$$.".txt", 1);
print F scalar localtime(time), "Moved stack\n";
close F;
open F, ">>state2.txt";
%positions = ();
print F scalar localtime(time), "Reset positions\n";
close F;
The file state.txt gets updated appropriately, while state2.txt doesn't -- it just hangs. On the other hand, when %positions isn't that humungous -- say if I sigint after only ten minutes, or it finds a solution -- then the hash gets cleared and all is well.
I thought about breaking the hash into 32nds or something, and doing a pre-hash before looking up the hash key, to keep %positions fairly small, but that seems like such a crock. (And frankly, I don't care that much. If while playing solitaire my computer zones out, well, no harm done.)
Is there a better way than %positions = () to clear a hash?
--woody