D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2642 - Remove big zero-initailzed portion of ClassInfo.init in object code
Summary: Remove big zero-initailzed portion of ClassInfo.init in object code
Status: RESOLVED DUPLICATE of issue 7319
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P3 enhancement
Assignee: Walter Bright
URL:
Keywords: performance
Depends on:
Blocks:
 
Reported: 2009-02-02 04:52 UTC by anonymous4
Modified: 2020-05-15 04:08 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description anonymous4 2009-02-02 04:52:49 UTC
Typical class has its fields all set to zeroes so .init member of this class usually contains plain zeroes, which can result in intensive spam from compiler. Consider this code:
---
class A
{
    int[10] a;
}

void main()
{
    auto a=new A();
}
---
which in compiled form takes 96kb and after raising array size to 10000, compiled execatable takes 135kb. So as number of class declarations grows, object code gets polluted with zeroes.
Comment 1 Koroskin Denis 2009-02-02 15:36:47 UTC
It works as designed. Design, however, can be given another thought.

I agree it is a bad idea of storing T.init as raw data within executable. Instead, T.init could be made a property (function) that returns default-initialized value. T init(ref T value); could be used to initialize value in-place. Both would use dynamic initializer (memset etc) when appropriate.
Comment 2 anonymous4 2009-02-03 02:05:29 UTC
Other possible optimizations:
1. Move zero memory to bss section and zero it at startup. Not only .inits contain zeroes, all static data does.
2. If zero data is invariant, different instances of this data can refer to the same memory location, though, if programmer casts one of these pointers to mutable data and overwrite it, this will cause disaster :)
Comment 3 anonymous4 2009-02-03 02:07:26 UTC
> 1. Move zero memory to bss section and zero it at startup.
This will be efficient in terms of both time and storage requirements.
Comment 4 Don 2009-02-03 03:12:21 UTC
Intializing to zero won't work for floats and char (which init to 0xFF...).
But it'd be a nice optimisation for other cases.
Comment 5 Walter Bright 2009-02-20 21:50:36 UTC
The .init block is never all zero, as the first member is the vtpr initializer.
Comment 6 Koroskin Denis 2009-02-21 03:30:23 UTC
One of the solutions is to introduce non-nullable types (both reference and value-types). In this case T.init would be useless (and thus may be safely removed from language*), because user will *have to* initialize it:

double d; // compare to "double? d;" which is ok to be non-initialized
double dd = 100.0;

Same for structs:

Foo f; // not initialized (CT-error or write-only until initialized)
Foo ff = Foo(); // ok, default-initialized

Note that this is very close to classes (same rule - same syntax):

Bar b; // not initialized (CT-error or write-only until initialized)
Bar bb = new Bar(); // ok, default-initialized

As a general rule for all types,

T t; // is not an initialized value, a compile-time error is raised,
// or just not allowed to be read until initialized (relaxed rule)
T t = initializer_expression; // ok, an initialized value

--
* I don't mind if T.init will be removed from language specs altogether as I never found it useful. It may still be left for some time in compiler internals (to copy T[] prior to calling T.__ctor), just don't expose it to users.
Comment 7 Jarrett Billingsley 2009-02-21 10:56:20 UTC
(In reply to comment #6)
> One of the solutions is to introduce non-nullable types (both reference and
> value-types). In this case T.init would be useless (and thus may be safely
> removed from language*), because user will *have to* initialize it:

So, as much as I like nonnull types, and as much as I like your proposal, there's this .. kind of icky part too.

auto a = new ClassType[10];

How exactly do you allocate an array of nonnull types?  

> * I don't mind if T.init will be removed from language specs altogether as I
> never found it useful. It may still be left for some time in compiler internals
> (to copy T[] prior to calling T.__ctor), just don't expose it to users.

I've found it useful!
Comment 8 Neia Neutuladh 2009-02-22 08:09:22 UTC
It wasn't clear to me that this is talking about ClassInfo.init rather than Type.init.

Non-nullable types do nothing for this problem. Class initialization consists of three stages:
1. allocate appropriate size
2. memcpy ClassInfo.init to allocated space
3. run constructor

With or without explicit initialization requirements, you could put that initialization in the constructors. That is going to be slower than copying bytes in some circumstances. Of course, if there's only a few fields to set, since the allocator already zeroes out allocated memory, that's less work.

In any case, it will be convenient to have ClassInfo.init, for doing various There Be Dragons things.
Comment 9 anonymous4 2009-07-09 02:47:55 UTC
> With or without explicit initialization requirements, you could put that
> initialization in the constructors.
.init can't be moved to constructor, because you won't be able to set vptr correctly.

Possible solution is to generate .init as a static function and inline it (or not) in the user code after call to new. Then call constructor. And trusting malloc to zero memory can be a good optimization.

And I don't think .init for primitive types is copied as an array of bytes.
Comment 10 anonymous4 2009-07-09 02:55:33 UTC
> > With or without explicit initialization requirements, you could put that
> > initialization in the constructors.
> .init can't be moved to constructor, because you won't be able to set vptr
> correctly.
Though setting of main vptr can be done in user code and other fields initialization can be moved to constructor (including interface fixups). Though this hardly makes difference in the case of inlining.
Comment 11 anonymous4 2009-07-09 03:13:28 UTC
oops... *fix
there's no interface fixups in the object, only vptrs.
Comment 12 Walter Bright 2012-01-23 00:13:57 UTC
Not a spec issue.
Comment 13 Mathias LANG 2020-05-15 04:08:34 UTC
There are a few issues in the bug tracker for this, with some up-to-date information.
For example https://issues.dlang.org/show_bug.cgi?id=7319 and https://issues.dlang.org/show_bug.cgi?id=13678 . So marking this as duplicate.

*** This issue has been marked as a duplicate of issue 7319 ***