[core.reflect] Detect Padding in structs or classes

Stefan Koch uplink.coder at googlemail.com
Sat Oct 2 20:16:28 UTC 2021


On Friday, 1 October 2021 at 17:35:24 UTC, Stefan Koch wrote:
> There has been a PR to introduce `-vpadding` into dmd which 
> warns on padding in structs.
> This can actually be trivially done by core reflect.
>
And there are some results.
```
diff --git a/std/file.d b/std/file.d
index 741656d..0d1c17c 100644
--- a/std/file.d
+++ b/std/file.d
@@ -4578,7 +4578,7 @@ version (Posix) @system unittest
  /**
   * Dictates directory spanning policy for $(D_PARAM dirEntries) 
(see below).
   */
-enum SpanMode
+enum SpanMode : ubyte
  {
      /** Only spans one directory. */
      shallow,
@@ -4635,15 +4635,15 @@ enum SpanMode
  private struct DirIteratorImpl
  {
    @safe:
-    SpanMode _mode;
+    DirEntry _cur;
+    DirHandle[] _stack;
+    DirEntry[] _stashed; //used in depth first mode
      // Whether we should follow symlinked directories while 
iterating.
      // It also indicates whether we should avoid functions which 
call
      // stat (since we should only need lstat in this case and it 
would
      // be more efficient to not call stat in addition to lstat).
      bool _followSymlink;
-    DirEntry _cur;
-    DirHandle[] _stack;
-    DirEntry[] _stashed; //used in depth first mode
+    SpanMode _mode;
```

This patch to phobos saves 4 bytes or so ;)
It's not much but it comes practically for free :-)

With my `core.reflect` extension it was just a questing of asking 
in between which fields the padding was.

You could save another byte by folding `_followSymlink` into the 
SpanMode flags.
But that and the PR to Phobos are left as an exercise for the 
reader.



More information about the Digitalmars-d mailing list