List:Ways to optimize a kernel!

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
LindusSystem
Member
Member
Posts: 63
Joined: Sat Apr 28, 2012 9:41 am
Location: Earth -> Asia

List:Ways to optimize a kernel!

Post by LindusSystem »

How can I optimize a kernel?
Can you list what all you could to improve a kernel?

I have my list:
1]Using special keywords such as 'register' and 'inline' ,etc
2]Not making too many function calls and jumps


Have one more add it!
Anyone has a idea of making a ntfs bootsector?if yes PM me , plz.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: List:Ways to optimize a kernel!

Post by Solar »

Coding smartly.

Serious, though. If you need a checklist, you need much more than just a checklist. Checklists don't cut it, because there are always exceptions to the rule. If you have the kind of userspace experience that is somewhat required for kernel work, you know how to write code efficiently...
Every good solution is obvious once you've found it.
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: List:Ways to optimize a kernel!

Post by Griwes »

What you are thinking of is generally called premature optimization. First step of optimization: implement everything, run it, profile it, find bottlenecks. Second step: try to improve algorithm used by the bottleneck (as opposed to "optimize algorithm's code"). Then, and only if it didn't worked, optimize it at source level.
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: List:Ways to optimize a kernel!

Post by Solar »

There are some things that aren't premature, but should be second nature to good coders. MOst of them can be summarized as "avoid doing things unnecessarily". Unnecessary variables (e.g. those that are used but once), unnecessary copying (but be aware of things like compiler RVO), stuff like that.
Every good solution is obvious once you've found it.
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: List:Ways to optimize a kernel!

Post by Griwes »

@Solar: of course, but I wouldn't call "not making too many function calls" "avoiding unnecessary things"; if it becomes bottleneck, it can be optimized, but it's no use to pack as much code as possible in single function to "avoid making too many functions calls", because, in the end, it may be fastest part of the module being optimized.
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: List:Ways to optimize a kernel!

Post by Combuster »

Still not ready for OSdev you are.
First, learn about algorithmic complexity, you must.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: List:Ways to optimize a kernel!

Post by gerryg400 »

Here's the list.

1) Use well designed algorithms.

- end of list -
If a trainstation is where trains stop, what is a workstation ?
Hoozim
Member
Member
Posts: 53
Joined: Fri Jul 23, 2010 8:26 am

Re: List:Ways to optimize a kernel!

Post by Hoozim »

Optimize hardware access (eg USB mass storage).
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: List:Ways to optimize a kernel!

Post by bluemoon »

LindusSystem wrote:1]Using special keywords such as 'register' and 'inline' ,etc
Never abuse keywords, they can mess up compiler's optimization logic.
LindusSystem wrote:2]Not making too many function calls and jumps
Functions can be inline by optimizer, for complex software like a kernel, maintainability is way more important.
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: List:Ways to optimize a kernel!

Post by iansjack »

Got to agree with that last comment. Well-written, commented, legible code is far more important than worrying about ultimate optimization. If you really want a highly optimized kernel write it all in assembler (but you'll have a tougher time maintaining it). Unless you are dealing with a RTOS speed shouldn't, IMO, be your priority.

This doesn't detract from the fact that some functions are so frequently called that a small amount of optimization can have big effects. Good algorithms and judicious use of assembler are called for in such cases. But don't waste time optimizing a bootloader or initialization routine; it's only going to be called once.

Once you have your system up and running, and relatively bug free, you might want to look at it to see if there are any obvious bottlenecks. But, until then, I would even turn off compiler optimization which can make debugging a nightmare. Study the code produced by your compiler and be sure you understand exactly what various keywords are doing. The more generic you can keep it, the better as far as I am concerned.
Fanael
Member
Member
Posts: 38
Joined: Fri Oct 16, 2009 9:20 am

Re: List:Ways to optimize a kernel!

Post by Fanael »

Solar wrote:MOst of them can be summarized as "avoid doing things unnecessarily". Unnecessary variables (e.g. those that are used but once)
I assure you decent compilers are able to cope with variables used only once way better than people think they do. Not to mention that binding a subexpression to an identifier can improve readability.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: List:Ways to optimize a kernel!

Post by Solar »

It's not the compiler I am woried about, it's readability. Yes, a temporary variable can improve readability, if the subexpression is so complex that taking it out of the main equation outweights the introduction of another identifier, and if the scope of that one-shot identifier is kept small (i.e., something between "the line directly in front of the actual use" and "the same screenful of source", maximum).

But frequently, I see one of these:
  • Variable bound to subexpression in one place, with the only use being somewhere else entirely (if C/C++, subexpression in the header, use in the implementation file).
  • Variable bound to subexpression to "explain" the subexpression through the name of the variable. (Comments, anyone?)
  • Variable bound to subexpression without meaningful variable name or comment.
  • Local variable bound to passed-by-value function parameter, with the function parameter not being used for anything else.
My favourite so far was:

Code: Select all

const int sizeof_int = sizeof( int );
Real-life production code. #-o I quote: "Do you know how expensive a function call is?"
Every good solution is obvious once you've found it.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: List:Ways to optimize a kernel!

Post by gerryg400 »

"Do you know how expensive a function call is?"
Perhaps 3ns on a modern desktop.
If a trainstation is where trains stop, what is a workstation ?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: List:Ways to optimize a kernel!

Post by Combuster »

typically ten times the nominal value if you're rdos :mrgreen:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: List:Ways to optimize a kernel!

Post by NickJohnson »

Design your program such that there is minimal duplication of functionality. Then, when you optimize one algorithm, the maximal amount of code is affected. Also, depend on the properties of specifics algorithms as little as possible. Then you can optimize those algorithms more aggressively without breaking existing code. These of course are good general rules for writing code.
Post Reply