public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: "Carlos Maniero" <carlos@maniero.me>
To: "Carlos Maniero" <carlos@maniero.me>,
	<~johnnyrichard/olang-devel@lists.sr.ht>
Subject: Re: [PATCH olang] arena: optimization: make arena 8 bits aligned
Date: Tue, 27 Feb 2024 20:50:48 -0300	[thread overview]
Message-ID: <CZG9J8JNNSKA.16EODIDGCQHLD@maniero.me> (raw)
In-Reply-To: <20240221055237.3231555-1-carlos@maniero.me>

There is a lot that I learned since I started this patch. First of all,
this patch has a bug.

> +    arena->offset += size + (size % 8);

Let's say the offset is zero and the size is one, we would expect the
new offset to be 8 after the *arena_alloc*, but this code was producing
1 as the new offset.

Furthermore, 8 is not the right memory alignment for x86_64
architectures according Intel's manual:

> 4.1.1 Alignment of Words, Doublewords, Quadwords, and Double Quadwords
> 
> Words, doublewords, and quadwords do not need to be aligned in memory on
> natural boundaries. The natural boundaries for words, doublewords, and
> quadwords are even-numbered addresses, addresses evenly divisible by
> four, and addresses evenly divisible by eight, respectively. However, to
> improve the performance of programs, data structures (especially stacks)
> should be aligned on natural boundaries whenever possible. The reason
> for this is that the processor requires two memory accesses to make an
> unaligned memory access; aligned accesses require only one memory
> access. A word or doubleword operand that crosses a 4-byte boundary or a
> quadword operand that crosses an 8-byte boundary is considered unaligned
> and requires two separate memory bus cycles for access.
> 
> Some instructions that operate on double quadwords require memory
> operands to be aligned on a natural boundary. These instructions
> generate a general-protection exception (#GP) if an unaligned operand is
> specified. A natural boundary for a double quadword is any address
> evenly divisible by 16. Other instructions that operate on double
> quadwords permit unaligned access (without generating a
> general-protection exception). However, additional memory bus cycles
> are required to access unaligned data from memory.

https://cdrdv2.intel.com/v1/dl/getContent/671200

So ideally we must align arena on 16 bits.

I'll send a v3 with theses two things fixed.

  parent reply	other threads:[~2024-02-27 23:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-21  5:52 Carlos Maniero
2024-02-21  5:53 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-02-21 15:09 ` [PATCH olang] arena: optimization: make arena 8 bits aligned Carlos Maniero
2024-02-27 23:50 ` Carlos Maniero [this message]
2024-02-28 12:33   ` Johnny Richard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CZG9J8JNNSKA.16EODIDGCQHLD@maniero.me \
    --to=carlos@maniero.me \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox