[split $longString ""] uses far too much memory
Closed / Fixed231523ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2001-02-08 11:56:00
When you feed a 5MB string into [split ... ""], it creates a completely new Tcl_Obj for each character (that's a cost of at least 120MB on a 32-bit architecture!) Surely it should be possible and even imperative that some kind of sharing between character strings should happen in this case? Even just doing this for the ASCII subset or the first UNICODE page (i.e. to better handle binary data) would be an immense benefit in the normal case.
dkfplain2001-02-09 18:08:00
I've got a modification that makes this much faster for large strings (two seconds vs. two minutes unmodified) while still passing all the test suite. It works by using a hash to map from characters to the objects to put in the result list.
dkfplain2001-02-09 18:12:24
My goodness! Further testing indicates this is a speedup even for shorter strings. First, for comparison the tclsh8.3 version which used the old algorithm I believe:
$ tclsh8.3
% set s "This is a long string to type but a short string to split."
This is a long string to type but a short string to split.
% time {split $s {}} 10000
229 microseconds per iteration
Now the new version using my changed code:
$ /tmp/bin/tclsh8.4
% set s "This is a long string to type but a short string to split."
This is a long string to type but a short string to split.
% time {split $s {}} 10000
181 microseconds per iteration
nobodyplain2001-02-10 07:31:06
See http://mini.net/cgi-bin/wikit/1245.html.
dkf Ticket [231523ffff]: 1 change
[History diff]2001-02-16 16:29:04
dkf Closed ticket [231523ffff].
[History diff]2001-02-16 16:29:27
Log in (anonymous is fine) to add a comment.