Solved

String speed in D1(short) vs. D2&D3(long)

Posted on 1997-10-27
11
271 Views
Last Modified: 2010-04-04
null
0
Comment
Question by:jodyl
  • 6
  • 2
  • 2
  • +1
11 Comments
 

Author Comment

by:jodyl
ID: 1348951
Edited text of question
0
 

Author Comment

by:jodyl
ID: 1348952
Edited text of question
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 1348953
Long string handling is 'clever' in that long strings have a reference count. If you assign string A to string B, then the reference count is simply updated. If you later alter string B, the reference count goes down, A still points to the original, but a completely new string ( with reference count 1 ) is created for B.

This means there is a fair overhead in sorting out these reference counts. Short strings are simply 256 characters long, full stop, so the overhead doesn't exist.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jodyl
ID: 1348954
Yes, I know that this is how Delphi is handling strings.  I did read the help about long strings.  Although I wouldn't call the way copying of long strings is done 'clever'.  How often would you copy a string and NOT do anything to it?  The reference count seems to me to be unnessasary overhead.

What I'm trying to find out is whether or not D3 is any faster? If it is, then I'll probably purchase it, just to save myself the  reprogramming time.  If it's not, then I have some serious changes to make to my program.  Reading in less than 1M of specifications shouldn't take longer to do than the complex processing a 10M data file.

Jody

0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1348955
I think so in your example is possible to slow down processing. This depends on your algorithm, but you can rewrite your procedures to new delphi style and then this must be faster than with delphi1. If you find any difficulties with rewriting the code for delphi2 than submit here question about this with some example code.
0
 
LVL 1

Expert Comment

by:ergates
ID: 1348956
There are some key differences between short and long strings which can lead to some "gotchas". First a recap:

A short string has a fixed maxumum size, up to 255 because the size is stored in byte. I don't understand your reference to garbage collection under Win95, I couldn't find this in the help. You can allocate short strings (with new or getmem) on the heap, but you are responsible for freeing them, they will not be freed automatically.

Long strings have a four byte length, and *always* have a dynamic portion that grows according to the current length of the string. You shouldn't need to worry about how it does this, but be careful that some of the operations for short strings are different, e.g. using s[0] to access the length byte doesn't work.

I think the problem could be that you are frequently trying to extend the length of a string. In a shortstring, this is quick, because it all takes place within the 'string' already allocated Consider adding a character to a string, using s := s + 'a'. This takes 3 steps, find the length of s, put 'a' in position length +1, increment length. This can be done in a handful of machine instructions and is very quick.

For a dynamic string, if the buffer Delphi allocated isn't big enough to add the 'a', its got to:
1.Allocate a (bigger) new string from heap.
2.Copy the old string to the new string.
3.Deallocate the old string.
4.Put the 'a' at the end and increment the length.

The extra 3 steps inolving memory management and copying are really slow in comparison.

There is a speed advantage to long strings in that if you pass one to a procedure as a value parameter, it doesn't need to copy it, but long strings can also be slower due to the extra overhead of memory management.

The two big plusses are, obviously, longer strings, and completely dynamic use of memory, which are the two major problems I always had with shortstrings. So while long strings are generally 'better', you may find cases where you hit a downside, particularly if you are hitting the string handling hard.

You may be able to get round the problem by presetting the long strings to allocate a large buffer, then set them to '', but I'm not sure if this works. In the meantime it might be best to stick with short strings, a pain I know.

This applies to D2, I haven't got D3 yet!

Regards,


0
 

Author Comment

by:jodyl
ID: 1348957
I already know this.  I've read the help file about how strings work.  The info about garbage collecion and short strings is in one of the help topics that compares short and long strings.

Since no one seems to know whether or not D3 is anymore efficient than D2 as far as the string manipulation is concerned, I'll just have to check this for myself.  Although I don't hold much hope that it'll be any faster.  Null-term strings look like they'll be the best way to handle this.


Jody

0
 

Author Comment

by:jodyl
ID: 1348958
Since the only answers I'm getting concerning Q1 & Q2 are repeating information out of the help file (which I have already read), I'm only interested in an answer to Q3:

Does D3 handle long strings faster than D2?


Jody

0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1348959
mayby 10-20% but not more.
0
 
LVL 1

Accepted Solution

by:
ergates earned 100 total points
ID: 1348960
D3 may handle strings slightly faster than D2, but it isn't going to magically solve your problem.

Short strings do not take part in any garbage collection, automatic or otherwise, unless this is something you have programmed yourself. Long strings will only be faster in the specific case of copying strings, in general they will be slower than shortstrings, in specific cases such as yours they may be a lot worse. The point of longstrings is size and dynamic allocation, not speed.

I can suggest two concrete ways to solve your problem with a minimum of effort:

1. Use shortstrings. These should work exactly the same way as they did before. If you encounter a problem, it's due to something else. I suspect you may have some other memory leaks that manifest under win95.

2. 'Preallocate' the long strings before you use them. You could use SetLength(str,256) to do this.

I have suggested a reason why the code may be a lot slower using longstrings, but without seeing your code I can't be sure. Could you let us know if the code does a lot of "string1 + string2" type operations?

Rewriting your code to use null-terminated strings is going to be a lot of effort, be very messy and introduce obscure bugs. You will have to rewrite all your string expressions to use strcat etc.

Ultimately whether using D3 will restore the performance of your application is only something you can tell us, unless you can send us some sample source which we could have a look at. I'm sure I could improve the D2 performance, and I have D3 on order.

I think the questions that you did ask have all been answered correctly, but as I say there is no magic answer to your problem. I could have said No, No, Yes and No, but I didn't think that was what you were looking for!

If you try D3, good luck, but I think you already have the answers.

Regards,
0
 

Author Comment

by:jodyl
ID: 1348961
A small speed increase was what I had expected to hear.  Yes, in one place it does a lot of "String1 := String1 + String2" operations, but only when it's reading in a specifications file for display.  It does alot more of "record.stringX := SubStr( String )" type operations when it's reading in the spec file for actual use and this is just as slow.

I'm just going to shelve the conversion to 32-bit for now.  I have too many other enhancements to this appliction waiting.  

I also do a fair amount of programming in C, so dealing with null-term strings isn't a big deal.  To simplify this I've already isolated the places where I'd need to deal with this into just a couple of functions so that I don't have alot of places to add the memory allocation and deallocation routines.  


Thanks for your help.

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question