[Kstars-devel] [kstars] kstars/skyobjects: Save 16 bytes per sky object.

Henry de Valence hdevalence at hdevalence.ca
Sat Dec 21 23:16:27 UTC 2013


Git commit 76a92d4c7ae98b7ef43d030a5d670cf69e15b027 by Henry de Valence.
Committed on 21/12/2013 at 22:54.
Pushed by hdevalence into branch 'master'.

Save 16 bytes per sky object.

In practice, the `long double` type has 16 byte size and alignment.
We can inspect the memory layout of some class inheriting SkyPoint using
clang [1]:

    *** Dumping AST Record Layout
       0 | class StarObject
       0 |   class SkyObject (primary base)
       0 |     class SkyPoint (primary base)
       0 |       (SkyPoint vtable pointer)
       0 |       (SkyPoint vftable pointer)
      16 |       long double lastPrecessJD
      32 |       class dms RA0
      32 |         double D
         |       [sizeof=8, dsize=8, align=8
         |        nvsize=8, nvalign=8]
     ...(snipped)...
     184 |   float B
     188 |   float V
         | [sizeof=192, dsize=192, align=16
         |  nvsize=192, nvalign=16]

The vtable takes up only 8 bytes (on 64-bit), but we waste 8 bytes on
padding. Moreover, we then take up 16 bytes to store lastPrecessJD.
Using a program like the following:

    #include <stdio.h>
    #include <math.h>

    int main()
    {
        double jd2000 = 2451545.0;
        double delta = nextafter(jd2000,jd2000+1) - jd2000;
        printf("delta: %.30f\n", delta);
        return 0;
    }

we can compute that at J2000, the minimum time step at double precision
is approximately 40 microseconds, so it's not clear that we gain
anything by using 80-bit long doubles instead of 64-bit doubles.
Changing the `long double` to `double` (and placing it last) results in
memory layout like so:

    *** Dumping AST Record Layout
       0 | class SkyPoint
       0 |   (SkyPoint vtable pointer)
       0 |   (SkyPoint vftable pointer)
       8 |   class dms RA0
       8 |     double D
         |   [sizeof=8, dsize=8, align=8
         |    nvsize=8, nvalign=8]

      16 |   class dms Dec0
      16 |     double D
         |   [sizeof=8, dsize=8, align=8
         |    nvsize=8, nvalign=8]

      24 |   class dms RA
      24 |     double D
         |   [sizeof=8, dsize=8, align=8
         |    nvsize=8, nvalign=8]

      32 |   class dms Dec
      32 |     double D
         |   [sizeof=8, dsize=8, align=8
         |    nvsize=8, nvalign=8]

      40 |   class dms Alt
      40 |     double D
         |   [sizeof=8, dsize=8, align=8
         |    nvsize=8, nvalign=8]

      48 |   class dms Az
      48 |     double D
         |   [sizeof=8, dsize=8, align=8
         |    nvsize=8, nvalign=8]

      56 |   double lastPrecessJD
         | [sizeof=64, dsize=64, align=8
         |  nvsize=64, nvalign=8]

This also has the benefit that the SkyPoint data fits in a single cache
line, though I don't think this really makes a difference given the
inefficiencies in the rest of the code. A before/after test showed a
drop in memory usage of about 6%.

[1]: http://eli.thegreenplace.net/2012/12/17/dumping-a-c-objects-memory-layout-with-clang/

CCMAIL: kstars-devel at kde.org

M  +13   -7    kstars/skyobjects/skypoint.h

http://commits.kde.org/kstars/76a92d4c7ae98b7ef43d030a5d670cf69e15b027

diff --git a/kstars/skyobjects/skypoint.h b/kstars/skyobjects/skypoint.h
index 566a18e..9f0d4cd 100644
--- a/kstars/skyobjects/skypoint.h
+++ b/kstars/skyobjects/skypoint.h
@@ -54,9 +54,11 @@ public:
     	*@param r Right Ascension
     	*@param d Declination
     	*/
-    SkyPoint( const dms& r, const dms& d ) :
-    lastPrecessJD( J2000 ), RA0(r), Dec0(d),
-            RA(r),  Dec(d)
+    SkyPoint( const dms& r, const dms& d ) : RA0(r)
+                                           , Dec0(d)
+                                           , RA(r)
+                                           , Dec(d)
+                                           , lastPrecessJD( J2000 )
     {}
 
     
@@ -67,8 +69,11 @@ public:
      *@note This also sets RA0 and Dec0
      */
     //FIXME: this (*15.0) thing is somewhat hacky.
-    explicit SkyPoint( double r, double d ) :
-    lastPrecessJD( J2000 ), RA0(r*15.0), Dec0(d), RA(r*15.0),  Dec(d)
+    explicit SkyPoint( double r, double d ) : RA0(r*15.0)
+                                            , Dec0(d)
+                                            , RA(r*15.0)
+                                            , Dec(d)
+                                            , lastPrecessJD( J2000 )
     {}
     
     /**
@@ -502,13 +507,14 @@ protected:
     	*/
     void precess(const KSNumbers *num);
 
-    long double   lastPrecessJD; // JD at which the last coordinate update (see updateCoords) for this SkyPoint was done
-
 private:
     dms RA0, Dec0; //catalog coordinates
     dms RA, Dec; //current true sky coordinates
     dms Alt, Az;
     static KSSun *m_Sun;
+protected:
+    double   lastPrecessJD; // JD at which the last coordinate update (see updateCoords) for this SkyPoint was done
+
 };
 
 #endif


More information about the Kstars-devel mailing list