[m-dev.] Why do strings in Mercury need to be word aligned?

Zoltan Somogyi zs at csse.unimelb.edu.au
Thu May 7 16:47:06 AEST 2009


On 06-May-2009, Peter Ross <pro at missioncriticalit.com> wrote:
> Why does a MR_String need to be word-aligned?  What code will ever
> look at the tag bits of a string and hence do the wrong thing?

No code should ever look at the tag bits of a pointer to a string,
since it is not a data type with a tagged representation.

There are two sort-of reasons, both of which are suggestive but not compelling.

First, aligning strings makes accesses to them faster, since statistically,
it reduces the number of cache blocks storing the string and therefore
required to be loaded from memory when the string is processed. This is a good
enough reason for C compilers to align string constants to quite strict
boundaries, even in the absence of alignment requirements.

Second, every other type in Mercury is aligned, so the allocator is
required to return aligned addresses anyway. Therefore allowing the Mercury
implementation to store strings at unaligned addresses wouldn't help;
the allocator would simply never return such addresses anyway.

I suspect the real question you are asking is what would happen if you
lifted the restriction, and allowed MR_Strings to not be aligned.

You would lose the ability to some day use a tagged pointer representation
for a type like this:

:- type maybe_string
	--->	yes(string)
	;	no.

(This is different from instantiating the generic maybe type, which requires
preserving the ability to handle any values inside yes() polymorphically;
this type does not require that.)

There may also be some unintentional dependencies in the runtime and/or the
debugger, but these should be fixable.

As far as I can remember, that's it. Overall, nothing very bad would happen.

Zoltan.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at csse.unimelb.edu.au
Administrative Queries: owner-mercury-developers at csse.unimelb.edu.au
Subscriptions:          mercury-developers-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the developers mailing list