public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Chris Snook <csnook@redhat.com>
To: Maxim Levitsky <maximlevitsky@gmail.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [Slightly off topic] A question about R/B trees.
Date: Fri, 17 Oct 2008 18:15:50 -0400	[thread overview]
Message-ID: <48F90E96.3060800@redhat.com> (raw)
In-Reply-To: <48F904FA.6090102@gmail.com>

Maxim Levitsky wrote:
> I am working on my small project, and I need a fast container to hold a 
> large sparse array.
> Balanced trees seem to fit perfectly.

Balanced trees take O(log n) to perform a great many operations, and traversing 
a binary tree is a particularly bad case for branch prediction.  Hash tables 
will perform much better, unless you get them horribly wrong.

> I decided to implement a red/black tree, and took a look at kernel rb 
> tree for reference,
> and I noticed that tree item has no parent pointer, while it seems that 
> it should have it.
> 
> I know now that it has parent pointer, but it is mixed with current and 
> parent node colour.
> Thus it is assumed that last two bits of this pointer are zero.

Not quite.  Read this:

http://lwn.net/Articles/184495/

> I can see anywhere that this restriction is applied.
> I see that structure is "aligned" but that I think only ensures that 
> compiler places it
> aligned in static data, does the alignment ensures that it will always 
> place it on aligned address in a structure?
> But then, the whole container structure can be misaligned, can't it?

GCC will only misalign the contents of a struct if you explicitly tell it to 
pack the struct.  That's one of those things you only do if you're 100% certain 
it's the right thing, and you're prepared to accept the consequences if you 
screw it up.

> Besides a comment there states that alignment is only for CRIS

I'm not sure this check is still necessary, but CRIS is a rather niche 
architecture.  On most architectures, word-aligning structures boosts 
performance at negligible memory cost, so compilers do it automatically.

> How about a check for misalignment?

The kernel is written in a dialect of C that makes several assumptions about the 
compiler, among them that the compiler won't screw this up unless you tell it 
to.  Any compiler that has alignment problems with the rbtree code is going to 
have similar problems in lots of other places too.  We don't support those 
compilers.

-- Chris

  reply	other threads:[~2008-10-17 22:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-17 21:34 [Slightly off topic] A question about R/B trees Maxim Levitsky
2008-10-17 22:15 ` Chris Snook [this message]
2008-10-17 22:47   ` Maxim Levitsky
2008-10-20 14:54     ` Chris Friesen
2008-10-18  7:53   ` Andi Kleen
2008-10-18  8:45     ` Pekka Enberg
2008-10-20 15:57       ` Maxim Levitsky

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=48F90E96.3060800@redhat.com \
    --to=csnook@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maximlevitsky@gmail.com \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox