http://d.puremagic.com/issues/show_bug.cgi?id=8873
Summary: Some class field reordering for emplacing? Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: DMD AssignedTo: nob...@puremagic.com ReportedBy: bearophile_h...@eml.cc --- Comment #0 from bearophile_h...@eml.cc 2012-10-22 15:14:21 PDT --- According to the specs D classes are free to reorder their fields: >The D compiler is free to rearrange the order of fields in a class to >optimally pack them in an implementation-defined manner.< So in this program maybe Bar1 should reorder its fields as Bar3, to save 4 bytes for each instance on 32 bit systems: class Bar1 { void Hello() {} float f; double d; } class Bar2 { void Hello() {} align(4) float f; align(4) double d; } class Bar3 { void Hello() {} double d; float f; } void main() { pragma(msg, __traits(classInstanceSize, Bar1)); // 24 bytes pragma(msg, __traits(classInstanceSize, Bar2)); // 20 bytes pragma(msg, __traits(classInstanceSize, Bar3)); // 20 bytes } This benchmark shows that if you allocate the class instances on the heap one at a time the total amount of memory used is the same for the various Bar (because of the GC, it allocates 32 bytes for each class instance of Bar1, Bar2 or Bar3), so that optimization is useful for emplace() only and similar in-place allocations: class Bar1 { void Hello() {} float f; double d; } class Bar2 { void Hello() {} align(4) float f; align(4) double d; } class Bar3 { void Hello() {} double d; float f; } int main() { pragma(msg, __traits(classInstanceSize, Bar1)); // 24 pragma(msg, __traits(classInstanceSize, Bar2)); // 20 pragma(msg, __traits(classInstanceSize, Bar3)); // 20 //-------------- //auto arr = new Bar1[1_000_000]; // 38.2 MB //auto arr = new Bar2[1_000_000]; // 38.2 MB auto arr = new Bar3[1_000_000]; // 38.2 MB foreach (ref a; arr) a = new typeof(arr[0])(); int count; foreach (i; 0 .. 500_000_000) count++; return count; } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------