public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang] arena: optimization: make arena 8 bits aligned
Date: Wed, 21 Feb 2024 02:52:37 -0300	[thread overview]
Message-ID: <20240221055237.3231555-1-carlos@maniero.me> (raw)

Non aligned data structure could have a huge impact on performance. Take
the example bellow:

  int main() {
      void *pointer = malloc(1024);

      int* data = pointer + 1;

      for (int i = 0; i < INT_MAX; i++) {
          *data += i;
      }

      printf("result = %d", *data);
  }

This are the execution results in my machine:

  pointer + 0
  time	0m1.668s
  pointer + 1
  time	0m2.285s
  pointer + 2
  time	0m2.286s
  pointer + 4
  time	0m1.722s
  pointer + 8
  time	0m1.707s

This commit changes the pointers to always return a 8 bit aligned
pointer.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/arena.c             | 2 +-
 tests/unit/arena_test.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/arena.c b/src/arena.c
index ae33e6a..63662e3 100644
--- a/src/arena.c
+++ b/src/arena.c
@@ -35,7 +35,7 @@ arena_alloc(arena_t *arena, size_t size)
         return NULL;
     }
     void *pointer = arena->region + arena->offset;
-    arena->offset += size;
+    arena->offset += size + (size % 8);
     return pointer;
 }
 
diff --git a/tests/unit/arena_test.c b/tests/unit/arena_test.c
index 6310795..7808b64 100644
--- a/tests/unit/arena_test.c
+++ b/tests/unit/arena_test.c
@@ -21,7 +21,7 @@
 static MunitResult
 arena_test(const MunitParameter params[], void *user_data_or_fixture)
 {
-    arena_t arena = arena_new(sizeof(int) * 2);
+    arena_t arena = arena_new(16);
 
     int *a = arena_alloc(&arena, sizeof(int));
     *a = 1;
-- 
2.34.1


             reply	other threads:[~2024-02-21  5:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-21  5:52 Carlos Maniero [this message]
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
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=20240221055237.3231555-1-carlos@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