When in doubt, add another layer of indirection
Posted Jun 1, 2011 21:25 UTC (Wed) by
sethml (subscriber, #8471)
Parent article:
Object-oriented design patterns in the kernel, part 1
Curious question from a non-kernel-hacker: it seems like the kernel could save a lot of memory (particularly on 64-bit systems) by replacing vtable pointers by smaller array indices. For example, every inode in the system contains an inode_operations pointer, occupying 64 bits:
struct inode {
/* RCU path lookup touches following: */
umode_t i_mode;
uid_t i_uid;
gid_t i_gid;
const struct inode_operations *i_op;
struct super_block *i_sb;
...
}
... foo->i_op->bar(...);
It'd be possible to save 52 bits per inode, which could add up if you have a lot of inodes in memory:
struct inode {
...
uint16_t i_op_idx;
...
}
struct inode_operations inode_ops[MAX_INODE_OPS];
... inode_ops[foo->i_op_idx].bar(...);
The same technique could be used for a lot of structure members which aren't vtable pointers - for example,
i_sb seems like a potential candidate.
Disadvantages: need to set an upper limit on the number of inode_operations structures, a bit slower (extra indirection), complicates the code. Given appropriate infrastructure, the code complication could be centralized in some helper functions/macros.
Thoughts? Could this be worthwhile? Are the potential memory gains too minimal to care about?
(
Log in to post comments)