Literal strings and code bloat

Post your requests and ideas on the future development of mikroBasic PRO for PIC.
Post Reply
Author
Message
alexconway
Posts: 39
Joined: 22 Oct 2008 12:30
Location: London, United Kingdom

Literal strings and code bloat

#1 Post by alexconway » 18 Sep 2012 22:39

It is well known that

Code: Select all

UART1_Write_Text("hello RS232")
takes up much much more code space than

Code: Select all

const _hello = "hello RS232"

dim text as string[11]
text = _hello
UART1_Write_Text(text]
It appears to me that the second example should compile to similar (not necessarily the same) code as the first, but the compiled code is very very different.

Cheers
Alex

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Literal strings and code bloat

#2 Post by janni » 19 Sep 2012 01:05

Code sizes of your examples are actually comparable (the _hello constant also takes program memory), but the code is indeed different. It would be similar, though, if you modified your second example to

Code: Select all

dim text as string[11]

text = "hello RS232" 
UART1_Write_Text(text)
The difference you noticed comes not from the fact that a string literal was passed as parameter, but that a constant was declared and may be used in many places in code. In such case compiler places the constant in code memory (but not in code) and copies it when necessary to RAM variable with help of special internal function (__CCS2S).

alexconway
Posts: 39
Joined: 22 Oct 2008 12:30
Location: London, United Kingdom

Re: Literal strings and code bloat

#3 Post by alexconway » 19 Sep 2012 15:19

would you have anwered differently if I had used a longer string?
By moving the literal string into a const, it feels like I am doing work that the compiler could be doing for me!

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Literal strings and code bloat

#4 Post by janni » 19 Sep 2012 18:07

alexconway wrote:would you have anwered differently if I had used a longer string?
No :) . I just explained where the difference comes from. I have formulated no opinion about which method is better as they both have their uses.
By moving the literal string into a const, it feels like I am doing work that the compiler could be doing for me!
Well, no compiler is ideal and there are ways to optimize code/RAM space for every compiler. On average, programmers bloat code much more effectively then compilers :wink: .

oliverb
Posts: 570
Joined: 24 May 2007 15:09

Re: Literal strings and code bloat

#5 Post by oliverb » 16 Jun 2014 12:43

Partly this is something we're stuck with due to the PIC's memory organisation.

As I understand it for inline string literals the compiler optimises for short strings, creating a chain of "movlw" "movwf" instructions and taking four bytes per character.
If the string is declared as a constant it is stored "packed" and the code to unpack the constant into a temporary array has a fixed length regardless of string length.

The obvious optimisation would be for the compiler to automatically convert string literals to constants if they are longer than a certain length, maybe 4 characters?

Post Reply

Return to “mikroBasic PRO for PIC Wish List”