[phobos] byte alignment for arrays

Jason Spencer spencer8 at sbcglobal.net
Mon Jun 28 12:46:14 PDT 2010


Yes.  What you really want is to know that the address of element 0 is 16-byte aligned.  So, what I assumed you were proposing is to always allocate the array storage as 16-byte aligned, then use 16 bytes for the size (and any other housekeeping you need).  Then element 0 will be 16-bytes behind a 16-byte aligned address, so you're still good.  That's the memory cost of this change--you'll burn (16 - (size of your size storage)) bytes at the beginning.  

It's not an easy choice.  But if this is limited to only those arrays over page-size, then I'm assuming there won't be 1000's of them, and the cost is down around a few hundred bytes at worst for the program.

Jason


----- Original Message ----
> From: Steve Schveighoffer <schveiguy at yahoo.com>
> To: Discuss the phobos library for D <phobos at puremagic.com>
> Sent: Mon, June 28, 2010 12:29:17 PM
> Subject: Re: [phobos] byte alignment for arrays
> 
> This only affects arrays that are PAGE size or larger.

A question then -- 
> let's say you have an array of doubles, which are 8 bytes wide, and you want to 
> use these SSE instructions.  Even if the first one is aligned on a 16-byte 
> boundary, wouldn't every other double be 
> misaligned?

-Steve



----- Original Message ----
> 
> From: Jason Spencer <
> href="mailto:spencer8 at sbcglobal.net">spencer8 at sbcglobal.net>
> To: 
> Discuss the phobos library for D <
> href="mailto:phobos at puremagic.com">phobos at puremagic.com>
> Sent: 
> Mon, June 28, 2010 3:00:10 PM
> Subject: Re: [phobos] byte alignment for 
> arrays
> 
> I would recommend changing it to 16-byte aligned.  
> Lots of SSE instructions 
> won't work or won't work efficiently at 8-byte 
> aligned addresses.  Even 
> without SSE, this makes array access more 
> cache-friendly, and is likely to 
> help.  If we're only talking 
> about arrays larger than page-size, then it's 
> not too much memory 
> overhead.

For less-than-page-sized arrays (or 
> performance-tight 
> code if you DON'T make the change), you'd have to use 
> something like 
> std.c.stdlib or std.<system> _aligned_alloc() to get around 
> 
> this.  Might be worth verifying this is actually available and works (maybe 
> 
> a unit test?. )

Jason



----- Original Message 
> ----
> 
> From: Steve Schveighoffer <
> href="mailto:
> ymailto="mailto:schveiguy at yahoo.com" 
> href="mailto:schveiguy at yahoo.com">schveiguy at yahoo.com">
> ymailto="mailto:schveiguy at yahoo.com" 
> href="mailto:schveiguy at yahoo.com">schveiguy at yahoo.com>
> To: Phobos 
> 
> <
> href="mailto:
> href="mailto:phobos at puremagic.com">phobos at puremagic.com">
> ymailto="mailto:phobos at puremagic.com" 
> href="mailto:phobos at puremagic.com">phobos at puremagic.com>
> Sent: 
> 
> Mon, June 28, 2010 11:36:25 AM
> Subject: [phobos] byte alignment 
> for 
> arrays
> 
> Recently, this bug has surfaced: 
> 
> 
> http://d.puremagic.com/issues/show_bug.cgi?id=4400

In a 
> nutshell, 
> 
> sometimes the byte alignment of arrays is 8 bytes 
> instead 16 
> bytes.

This 
> was caused by my array append 
> patch, because in 
> large arrays, I store the length 
> at the 
> front of the array.  With 
> some queries before I created my patch, 
> I 
> was told that 8 byte 
> alignment was fine.  However, the 
> alignment is easy to 
> change 
> since it's a couple specific 
> functions that determine the padding and 
> 
> alignment.  So 
> changing to 16 bytes is not an issue technically, and 
> 
> 
> functionally, this is only on PAGE sized arrays and larger, so 16 bytes 
> 
> vs. 8 
> bytes isn't likely to cause problems.

Bearophile's main 
> 
> argument stems 
> from this.  I am not a processor or 
> assembly 
> expert, so I have no idea 
> about this at 
> 
> all:

-----------------
The 16 bytes alignment was 
> introduced 
> 
> because instructions like the SSE2 movapd
need 16 byte 
> 
> 
> alignment:
http://en.wikipedia.org/wiki/MOVAPD

I have 
> recently used 
> 
> it 
> 
> 
> here:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=112670

> 
> 
> 
And some other SSE* instructions work with 8 byte alignment too, 
> but they 
> 
> are
slower (future CPUs can remove such alignment 
> requirements, some 
> of it 
> has
being removed already, so in 
> that future the GC can go 
> back giving 8 
> bytes
aligned 
> memory).
-----------------

So 
> should I change 
> 
> it?

-Steve



      
> 
> 
> 
_______________________________________________
phobos mailing 
> 
> list

> ymailto="mailto:
> href="mailto:
> ymailto="mailto:phobos at puremagic.com" 
> href="mailto:phobos at puremagic.com">phobos at puremagic.com">
> ymailto="mailto:phobos at puremagic.com" 
> href="mailto:phobos at puremagic.com">phobos at puremagic.com" 
> 
> 
> href="mailto:
> href="mailto:
> href="mailto:phobos at puremagic.com">phobos at puremagic.com">
> ymailto="mailto:phobos at puremagic.com" 
> href="mailto:phobos at puremagic.com">phobos at puremagic.com">
> 
> ymailto="mailto:
> href="mailto:phobos at puremagic.com">phobos at puremagic.com" 
> 
> href="mailto:
> href="mailto:phobos at puremagic.com">phobos at puremagic.com">
> ymailto="mailto:phobos at puremagic.com" 
> href="mailto:phobos at puremagic.com">phobos at puremagic.com
http://lists.puremagic.com/mailman/listinfo/phobos
_______________________________________________
phobos 
> 
> mailing list

> href="mailto:
> ymailto="mailto:phobos at puremagic.com" 
> href="mailto:phobos at puremagic.com">phobos at puremagic.com">
> ymailto="mailto:phobos at puremagic.com" 
> href="mailto:phobos at puremagic.com">phobos at puremagic.com

> href="
> href="http://lists.puremagic.com/mailman/listinfo/phobos" target=_blank 
> >http://lists.puremagic.com/mailman/listinfo/phobos" target=_blank 
> 
> >
> >http://lists.puremagic.com/mailman/listinfo/phobos


    
>   
_______________________________________________
phobos mailing 
> list

> href="mailto:phobos at puremagic.com">phobos at puremagic.com

> href="http://lists.puremagic.com/mailman/listinfo/phobos" target=_blank 
> >http://lists.puremagic.com/mailman/listinfo/phobos


More information about the phobos mailing list