Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-04

making FPC more code optimized

19 messages · 8 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ik

Hello,

Here is something that I'm asking without really know anything about the subject, so please bear with me.

I'ved asked few places that works with Pascal (Delphi and FPC), why does they use C as the infrastructure, and they all say that there is not even one Pascal compiler that makes the code optimized and fast running with low resources, while many C compilers does have such features.
For them it's not the language, but the compilers that are problematic.

I'm not talking about "killer feature" that never exists, but what stops FPC and/or other Pascal compilers to be more optimized, with lower footprint, and faster code like with C compilers for example ? Is there anything that can be done to make them "there" as well ?

Thank,
Ido

Reply by email

Reinier Olislagers
to ik
ik wrote 16 lines
Hello, Here is something that I'm asking without really know anything about the subject, so please bear with me. I'ved asked few places that works with Pascal (Delphi and FPC), why does they use C as the infrastructure, and they all say that there is not even one Pascal compiler that makes the code optimized and fast running with low resources, while many C compilers does have such features. For them it's not the language, but the compilers that are problematic. I'm not talking about "killer feature" that never exists, but what stops FPC and/or other Pascal compilers to be more optimized, with lower footprint, and faster code like with C compilers for example ? Is there anything that can be done to make them "there" as well ?

Ido,

I don't quite understand what you mean.... perhaps you could expand a bit...

Are you saying:
1. FPC is using "C infrastructure" and you want to use something else. What do you mean by that? I don't understand.
Oh, perhaps you mean that some companies/institutions are switching from FPC/Delphi to C?
2. FPC does not generate small, fast code?
Are you describing some particular performance problems that might perhaps be addressed or is this a general comment? Are you talking about a specific embedded platform perhaps?

Anyway, to me as a user on a desktop platform, FPC is plenty fast enough.

With today's processors, having a compiler that works on Windows, OSX and Linux is a much bigger advantage than perhaps getting the last ounce of performance out of the compiler.... but I understand the needs of embedded developers can be different.

IIRC, e.g. on the "Size Matters" wiki FAQ page, some strategies for stripping FPC for embedded development are mentioned. Perhaps the people you are talking to are not aware of those methods?

Regards,
Reinier

Reply by email

ik
to Reinier Olislagers
Reinier Olislagers wrote 27 lines
On 29-4-2012 11:40, ik wrote: > Hello, > > Here is something that I'm asking without really know anything about > the subject, so please bear with me. > > I'ved asked few places that works with Pascal (Delphi and FPC), why > does they use C as the infrastructure, and they all say that there is > not even one Pascal compiler that makes the code optimized and fast > running with low resources, while many C compilers does have such > features. > For them it's not the language, but the compilers that are problematic. > > I'm not talking about "killer feature" that never exists, but what > stops FPC and/or other Pascal compilers to be more optimized, with > lower footprint, and faster code like with C compilers for example ? > Is there anything that can be done to make them "there" as well ? Ido, I don't quite understand what you mean.... perhaps you could expand a bit... Are you saying: 1. FPC is using "C infrastructure" and you want to use something else. What do you mean by that? I don't understand. Oh, perhaps you mean that some companies/institutions are switching from FPC/Delphi to C?

Not at all.

2. FPC does not generate small, fast code? Are you describing some particular performance problems that might perhaps be addressed or is this a general comment? Are you talking about a specific embedded platform perhaps?

I'm working at the moment on a distributed service that I'm writing in OP. If I need to control high availability, and process many requests, then C will have much faster implementation for it (not only in how I write it, but in the way that it is translated to machine code).

Let's take simple program:

---- pascal code ----

program hello;
begin
  writeln('Hello World');
end.

----- pascal code ----

vs c code:

------ c code -----
#include <stdio.h>
int main(int argc, char ** argv) {
  printf("Hello World\n");
  return 0;
}

------ c code ------

The code executable will be not only smaller, but takes less memory to run, and will be more optimized.

Show quoted text 7 lines
Anyway, to me as a user on a desktop platform, FPC is plenty fast enough. With today's processors, having a compiler that works on Windows, OSX and Linux is a much bigger advantage than perhaps getting the last ounce of performance out of the compiler.... but I understand the needs of embedded developers can be different.

Desktop today is changing to "smart" phones, so it's more and more embedded like development.
FPC with JVM is one of the smartest implementation of a compiler imho. It makes you keep your language, but distribute your code in many platforms.
Next can be also supporting the MS .NET runtime library, and making Pascal as the only current programming language that can be used on all the environments that really matters today.

But all of that is not important, if C code that does exactly what the Pascal code, can do it with less resources.

IIRC, e.g. on the "Size Matters" wiki FAQ page, some strategies for stripping FPC for embedded development are mentioned. Perhaps the people you are talking to are not aware of those methods?

They are aware of them, but still, both Linux and Windows (afaik), loads the whole executable to memory. Smaller executable, and dynamic executable are taking less memory.

But I'm talking about how the code is implemented when it is converted into assembler.
The C code is more "efficient" and optimized when it is translated into machine code at the end. It also has less memory footprint and there-for, you can use the same machine to do more.

I'm looking to understand, what is required by the compiler to make it create better outcome that will make it the same if not better then the C code, making the choice in Pascal for people like myself and others (that does willing to use it that is), to know that they can create good infrastructures using Pascal.
I prefer Pascal to any other compiled language, without thinking twice, as long as it answers my needs.

Show quoted text 6 lines
Regards, Reinier _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Ido

Reply by email

Florian Klämpfl
to ik: Here is something that I'm asking without really know anything about the subject, so…
ik wrote 15 lines
Hello, Here is something that I'm asking without really know anything about the subject, so please bear with me. I'ved asked few places that works with Pascal (Delphi and FPC), why does they use C as the infrastructure, and they all say that there is not even one Pascal compiler that makes the code optimized and fast running with low resources, while many C compilers does have such features. For them it's not the language, but the compilers that are problematic. I'm not talking about "killer feature" that never exists, but what stops FPC and/or other Pascal compilers to be more optimized, with lower footprint,

Regarding footprint, FPC is far ahead of everything:

http://shootout.alioth.debian.org/u32/which-language-is-best.php?calc=chart&gpp=on&java=on&ghc=on&sbcl=on&csharp=on&racket=on&v8=on&hipe=on&vw=on&php=on&python3=on&lua=on&perl=on&yarv=on&cint=on&xfullcpu=0&xmem=1&xloc=0&nbody=1&fannkuchredux=1&meteor=0&fasta=1&fastaredux=1&spectralnorm=1&revcomp=1&mandelbrot=1&knucleotide=1&regexdna=1&pidigits=1&chameneosredux=0&threadring=0&binarytrees=1

faster code like with C compilers for example ?

Spent more man power into optimizer development, though some optimizations are hard in pascal because we assume stricter aliasing rules and because we don't have a volatile keyword.

Reply by email

Florian Klämpfl
to ik: On Sun, Apr 29, 2012 at 12:58, Reinier Olislagers Not at all. I'm working at the moment…
ik wrote 26 lines
Let's take simple program: ---- pascal code ---- program hello; begin writeln('Hello World'); end. ----- pascal code ---- vs c code: ------ c code ----- #include <stdio.h> int main(int argc, char ** argv) { printf("Hello World\n"); return 0; } ------ c code ------ The code executable will be not only smaller, but takes less memory to run,

How did you measure this? Did you take the dyn. libs into account or did you link statically? For me, the FPC executable is much smaller, gcc -static creates something with 500 kB, and 1000 runs of the FPC version take 20 per cent less time than the C one?

Reply by email

Jonas Maebe
to Florian Klämpfl: Regarding footprint, FPC is far ahead of everything:…
Florian Klämpfl wrote
Spent more man power into optimizer development, though some optimizations are hard in pascal because we assume stricter aliasing rules and because we don't have a volatile keyword.

At least as far as our current optimizers are concerned, nothing is volatile in Pascal: both the assembler and node tree optimizers will replace multiple loads of global data with a single load when they can. So that should not limit any optimizations at this time.

Regarding aliasing, I'm not sure whether C defines anything regarding that. You at least need to use the -fstrict-aliasing gcc flag to tell it to assume that pointers to different types cannot alias each other (and hence get better optimizations). In standard Pascal, pointers by definition can only point to data of one particular type, so it could be compiled with -fstrict-aliasing in any case. Since in FPC/Delphi this is no longer the case, you could introduce a similar switch there to allow programmers to tell the compiler that their code also obeys strict aliasing rules.

In general, I think it's indeed simply a matter of the amount of man power spent on optimizations.

Jonas

Reply by email

Jorge Aldo G. de F. Junior
to Jonas Maebe

What would be usefull is to move the loads of code stored in sysutils/runtime into a dll/so.

thats what makes C code look "smaller", theres a nice libc (and others) there that groups mostly used functions into a single instance, while in pascal every executable replicates the very same code for each instance.

make sysutils/runtime etc an external loadable library and we are going to see FPC generate similar sized executables for similarly sized programs.

2012/4/29 Jonas Maebe :

Show quoted text 17 lines
On 29 Apr 2012, at 12:22, Florian Klämpfl wrote: > Spent more man power into optimizer development, though some > optimizations are hard in pascal because we assume stricter aliasing > rules and because we don't have a volatile keyword. At least as far as our current optimizers are concerned, nothing is volatile in Pascal: both the assembler and node tree optimizers will replace multiple loads of global data with a single load when they can. So that should not limit any optimizations at this time. Regarding aliasing, I'm not sure whether C defines anything regarding that. You at least need to use the -fstrict-aliasing gcc flag to tell it to assume that pointers to different types cannot alias each other (and hence get better optimizations). In standard Pascal, pointers by definition can only point to data of one particular type, so it could be compiled with -fstrict-aliasing in any case. Since in FPC/Delphi this is no longer the case, you could introduce a similar switch there to allow programmers to tell the compiler that their code also obeys strict aliasing rules. In general, I think it's indeed simply a matter of the amount of man power spent on optimizations. Jonas_______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Jonas Maebe
to Jorge Aldo G. de F. Junior
Jorge Aldo G. de F. Junior wrote
What would be usefull is to move the loads of code stored in sysutils/runtime into a dll/so.

That's mainly useful if the external API of the RTL will ever stabilize, which is certainly not the case today. Otherwise you create a dynamic library hell.

Jonas

Reply by email

Florian Klämpfl
to Jorge Aldo G. de F. Junior: What would be usefull is to move the loads of code stored in sysutils/runtime into a…
Jorge Aldo G. de F. Junior wrote 11 lines
What would be usefull is to move the loads of code stored in sysutils/runtime into a dll/so. thats what makes C code look "smaller", theres a nice libc (and others) there that groups mostly used functions into a single instance, while in pascal every executable replicates the very same code for each instance. make sysutils/runtime etc an external loadable library and we are going to see FPC generate similar sized executables for similarly sized programs.

It makes them look smaller but their footprint is actually larger so this is exactly what Ido doesn't want.

Reply by email

Mark Morgan Lloyd
to Jonas Maebe: That's mainly useful if the external API of the RTL will ever stabilize, which is…
Jonas Maebe wrote 6 lines
On 29 Apr 2012, at 14:23, Jorge Aldo G. de F. Junior wrote: > What would be usefull is to move the loads of code stored in > sysutils/runtime into a dll/so. That's mainly useful if the external API of the RTL will ever stabilize, which is certainly not the case today. Otherwise you create a dynamic library hell.

I had to download somebody's archive viewer a few weeks ago: first there was his binary, then there were his custom DLLs, then there was the MS runtime. I think it came to about 100Mb in total, which made the equivalent written using Lazarus look pretty good- once we'd thrashed out some smartlinking issues.

However there's a definite acceptance problem: in the past I've talked to people who insisted in programming in C "because the customer might be bothered about what language we use". Since ABIs (i.e. calling conventions etc.) have settled down a lot since those days it should, in theory, no longer be an issue: but I suspect that many old prejudices remain.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Jorge Aldo G. de F. Junior
to Florian Klämpfl: It makes them look smaller but their footprint is actually larger so this is exactly what…

If you deploy one executable, ok, but if you want to create a handfull of executables (following unix logic of many small programs) then you have a problem.

EVERY executable will have the same code again again again, i think libraries have been created to address exactly this issue.

about the library hell, why we cant use lib versions ?

lets say,

libpas.so.2.4.4 for fpc 2.4.4
libpas.so.2.6.1 for fpc 2.6.1
libpas.sysutils.so.2.4.4
libpas.classes.so.2.4.4

etc...
etc...

the only problem i see is the lack of a sane way to export class instances to/from the libraries.

2012/4/29 Florian Klämpfl :

Show quoted text 18 lines
Am 29.04.2012 14:23, schrieb Jorge Aldo G. de F. Junior: > What would be usefull is to move the loads of code stored in > sysutils/runtime into a dll/so. > > thats what makes C code look "smaller", theres a nice libc (and > others) there that groups mostly used functions into a single > instance, while in pascal every executable replicates the very same > code for each instance. > > make sysutils/runtime etc an external loadable library and we are > going to see FPC generate similar sized executables for similarly > sized programs. It makes them look smaller but their footprint is actually larger so this is exactly what Ido doesn't want. _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Jonas Maebe
to Jorge Aldo G. de F. Junior
Jorge Aldo G. de F. Junior wrote
If you deploy one executable, ok, but if you want to create a handfull of executables (following unix logic of many small programs) then you have a problem.

You certainly do end up with larger executables. Whether or not this is a problem depends on the situation.

EVERY executable will have the same code again again again, i think libraries have been created to address exactly this issue.

That's true.

Show quoted text 6 lines
about the library hell, why we cant use lib versions ? lets say, libpas.so.2.4.4 for fpc 2.4.4 libpas.so.2.6.1 for fpc 2.6.1

There is a new FPC 2.6.1 every time a revision is committed to the fixes_2_6 branch. Also, these libpas.so files would be quite large (about 21MB for FPC 2.6.0). You need quite a few small unix utilities to compensate for that in terms of disk space. Of course, you will also reduce memory usage somewhat in case you run several of those utilities at the same time when using dynamic libraries (but startup will be a little slower due to the run time relocation and copy-on-write).

the only problem i see is the lack of a sane way to export class instances to/from the libraries.

That's what (the as of yet unsupported) Delphi-style packages solve. They have the same versioning issue though.

Jonas

Reply by email

Florian Klämpfl
to Jorge Aldo G. de F. Junior: If you deploy one executable, ok, but if you want to create a handfull of executables…
Jorge Aldo G. de F. Junior wrote
If you deploy one executable, ok, but if you want to create a handfull of executables (following unix logic of many small programs) then you have a problem.

Indeed, but I don't think this is the way FPC programs are typically used.

Show quoted text 5 lines
EVERY executable will have the same code again again again, i think libraries have been created to address exactly this issue. about the library hell, why we cant use lib versions ?

Because nobody is interested in doing so? Else we got already patches for the install builders to contain .so as well. FPC can create shared libs on unixes and with small flaws, it even works ;)

ldd ./test
libfprtl-2.7.1.so =>
/home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7.1.so (0x008d3000) ls -la
/home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7.1.so -rw-r--r-- 1 florian florian 2026701 2012-04-29 15:42 /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7.1.so ./test
Hello world
Inconsistency detected by ld.so: dl-fini.c: 194: _dl_fini: Assertion `ns != 0 || i == nloaded' failed!

Show quoted text 13 lines
lets say, libpas.so.2.4.4 for fpc 2.4.4 libpas.so.2.6.1 for fpc 2.6.1 libpas.sysutils.so.2.4.4 libpas.classes.so.2.4.4 etc... etc... the only problem i see is the lack of a sane way to export class instances to/from the libraries.

For .so this is no problem, they act just like simple libs.

Show quoted text 24 lines
2012/4/29 Florian Klämpfl : > Am 29.04.2012 14:23, schrieb Jorge Aldo G. de F. Junior: >> What would be usefull is to move the loads of code stored in >> sysutils/runtime into a dll/so. >> >> thats what makes C code look "smaller", theres a nice libc (and >> others) there that groups mostly used functions into a single >> instance, while in pascal every executable replicates the very same >> code for each instance. >> >> make sysutils/runtime etc an external loadable library and we are >> going to see FPC generate similar sized executables for similarly >> sized programs. > > It makes them look smaller but their footprint is actually larger so > this is exactly what Ido doesn't want. > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Marco van de Voort
to Florian Klämpfl: It makes them look smaller but their footprint is actually larger so this is exactly what…

In our previous episode, Florian Kl?mpfl said:

Show quoted text 6 lines
> make sysutils/runtime etc an external loadable library and we are > going to see FPC generate similar sized executables for similarly > sized programs. It makes them look smaller but their footprint is actually larger so this is exactly what Ido doesn't want.

And on top of that it sounds like there are lumbering assumptions that smaller is also faster. Something that definitely isn't true when you move from static to dynamic linking.

Reply by email

Thomas Schatzl
to Florian Klämpfl: Indeed, but I don't think this is the way FPC programs are typically used. Because nobody…

Hi

Florian Klämpfl wrote 20 lines
Am 29.04.2012 15:13, schrieb Jorge Aldo G. de F. Junior: > If you deploy one executable, ok, but if you want to create a handfull > of executables (following unix logic of many small programs) then you > have a problem. Indeed, but I don't think this is the way FPC programs are typically used. > > EVERY executable will have the same code again again again, i think > libraries have been created to address exactly this issue. > > about the library hell, why we cant use lib versions ? Because nobody is interested in doing so? Else we got already patches for the install builders to contain .so as well. FPC can create shared libs on unixes and with small flaws, it even works ;) ldd ./test libfprtl-2.7.1.so => /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7,

Actually, this output shows the bug already: for some reason it does not link to ld.so as well.
I thought I had fixed that some time ago.

Show quoted text 9 lines
.1.so (0x008d3000) ls -la /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7.1.so -rw-r--r-- 1 florian florian 2026701 2012-04-29 15:42 /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7.1.so ./test Hello world Inconsistency detected by ld.so: dl-fini.c: 194: _dl_fini: Assertion `ns != 0 || i == nloaded' failed!

And that's exactly what happens if the library is missing the reference to ld.so on ubuntu ...

I will have a look why this broke (again).

Thomas

Reply by email

Florian Klämpfl
to Thomas Schatzl
Thomas Schatzl wrote 41 lines
Hi On Sun, 2012-04-29 at 15:50 +0200, Florian Klämpfl wrote: > Am 29.04.2012 15:13, schrieb Jorge Aldo G. de F. Junior: >> If you deploy one executable, ok, but if you want to create a handfull >> of executables (following unix logic of many small programs) then you >> have a problem. > > Indeed, but I don't think this is the way FPC programs are typically used. > >> >> EVERY executable will have the same code again again again, i think >> libraries have been created to address exactly this issue. >> >> about the library hell, why we cant use lib versions ? > > Because nobody is interested in doing so? Else we got already patches > for the install builders to contain .so as well. FPC can create shared > libs on unixes and with small flaws, it even works ;) > > ldd ./test > libfprtl-2.7.1.so => > /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7, Actually, this output shows the bug already: for some reason it does not link to ld.so as well. I thought I had fixed that some time ago. > .1.so (0x008d3000) > ls -la > /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7.1.so > -rw-r--r-- 1 florian florian 2026701 2012-04-29 15:42 > /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7.1.so > ./test > Hello world > Inconsistency detected by ld.so: dl-fini.c: 194: _dl_fini: Assertion `ns > != 0 || i == nloaded' failed! And that's exactly what happens if the library is missing the reference to ld.so on ubuntu ... I will have a look why this broke (again).

Does libfprtl have to reference ld.so or the main program?

Reply by email

Thomas Schatzl
to Florian Klämpfl

Hi,

Florian Klämpfl wrote 27 lines
Am 29.04.2012 17:34, schrieb Thomas Schatzl: > Hi >> ldd ./test >> libfprtl-2.7.1.so => >> /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7, > > Actually, this output shows the bug already: for some reason it does not > link to ld.so as well. > I thought I had fixed that some time ago. > >> .1.so (0x008d3000) >> ls -la >> /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7.1.so >> -rw-r--r-- 1 florian florian 2026701 2012-04-29 15:42 >> /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7.1.so >> ./test >> Hello world >> Inconsistency detected by ld.so: dl-fini.c: 194: _dl_fini: Assertion `ns >> != 0 || i == nloaded' failed! > > And that's exactly what happens if the library is missing the reference > to ld.so on ubuntu ... > > I will have a look why this broke (again). Does libfprtl have to reference ld.so or the main program?

iirc it is sufficient that libfprtl references it. But that error message is just what you get if it does not. So maybe my reply was too quick.

I will have a try and see anyway; further thinking about it, if there were changes to that code, the corresponding testsuite programs should fail too, so maybe the original changes just work for some cases or cover up the real issue.

Thomas

Reply by email

Florian Klämpfl
to Thomas Schatzl: iirc it is sufficient that libfprtl references it. But that error message is just what…
Thomas Schatzl wrote 39 lines
Hi, On Sun, 2012-04-29 at 17:38 +0200, Florian Klämpfl wrote: > Am 29.04.2012 17:34, schrieb Thomas Schatzl: >> Hi >>> ldd ./test >>> libfprtl-2.7.1.so => >>> /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7, >> >> Actually, this output shows the bug already: for some reason it does not >> link to ld.so as well. >> I thought I had fixed that some time ago. >> >>> .1.so (0x008d3000) >>> ls -la >>> /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7.1.so >>> -rw-r--r-- 1 florian florian 2026701 2012-04-29 15:42 >>> /home/florian/./fpc/svn/rtl/units/i386-linux/libfprtl-2.7.1.so >>> ./test >>> Hello world >>> Inconsistency detected by ld.so: dl-fini.c: 194: _dl_fini: Assertion `ns >>> != 0 || i == nloaded' failed! >> >> And that's exactly what happens if the library is missing the reference >> to ld.so on ubuntu ... >> >> I will have a look why this broke (again). > > Does libfprtl have to reference ld.so or the main program? > iirc it is sufficient that libfprtl references it. But that error message is just what you get if it does not. So maybe my reply was too quick. I will have a try and see anyway; further thinking about it, if there were changes to that code, the corresponding testsuite programs should fail too, so maybe the original changes just work for some cases or cover up the real issue.

libfprtl is generated by ppumove and not the compiler so this might be the reason.

Reply by email