Skip to contentSkip to conversation

net core: optimize netdev_create_hash()

PATCH In review Wei Yang 2 participants 3 messages Mar 15, 2013 – Mar 16, 2013 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 net core: optimize netdev_create_hash()

netdev_create_hash() is divded into two steps:
1. allocate space for hash_head
2. initialize hash_head->first to NULL for each hash_head

This patch merge the two steps into one step.

When allocating the space for hash_head, it will use kzalloc() instead of kmalloc(). Then hash_head->first is set to NULL during the allocation step, which means it is not necessary to call INIT_HLIST_HEAD() for each hash_head.

This will:
1. reduce the code size
2. reduce the run time of iteration on initializing hash_head array

---
 net/core/dev.c |   12 +++++++-----
 1 files changed, 7 insertions(+), 5 deletions(-)
net/core/dev.c +7 -5
index f64e439..79f0666 100644
@@ -6564,13 +6564,15 @@ EXPORT_SYMBOL(netdev_increment_features);
 
 static struct hlist_head *netdev_create_hash(void)
 {
-	int i;
 	struct hlist_head *hash;
 
-	hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
-	if (hash != NULL)
-		for (i = 0; i < NETDEV_HASHENTRIES; i++)
-			INIT_HLIST_HEAD(&hash[i]);
+	hash = kzalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
+
+	/*
+	 * hash[i]->first is set to NULL in kzalloc()
+	 *
+	 * INIT_HLIST_HEAD(&hash[i]) is not necessary now
+	 */
 
 	return hash;
 }
Signature · 1 line
1.7.5.4
maintainer
#1 lore raw

From: Wei Yang <[email protected]>
Date: Sat, 16 Mar 2013 00:32:11 +0800

Quoted 6 lines

netdev_create_hash() is divded into two steps:
1. allocate space for hash_head
2. initialize hash_head->first to NULL for each hash_head

This patch merge the two steps into one step.

Quoted 7 lines

When allocating the space for hash_head, it will use kzalloc() instead of kmalloc(). Then hash_head->first is set to NULL during the allocation step, which means it is not necessary to call INIT_HLIST_HEAD() for each hash_head.

This will:
1. reduce the code size
2. reduce the run time of iteration on initializing hash_head array

Please do not ever post changes like this.

This makes assumtions about the list head implementation that we should not make. If someone adds a debugging facility that causes lists to be initialized differently, it will be broken for this hash table now.

The reason the abstractions exist is so that people do not do things like you are trying to do.

If you want to add an interface in the generic list facilities which performs this simplifcation, fine. Implement it and post such a suggestion to linux-kernel, and then once such a facility is present in Linus's tree we can start using it in the networkng code.

A message that is not in the archive (sent off-list or not yet received): 20130316000051.GA5941@richard.(null)

author
#3 lore raw

[resend for syntax error in Message-ID]

David,

Thanks for pointing out the error, it really helps.

I guess last mail is still blocked by the mail server, so I resend it. Hope it works now.

On Sat, Mar 16, 2013 at 08:00:51AM +0800, Wei Yang wrote:

Quoted 17 lines · David Miller

From: Wei Yang <[email protected]>
Date: Sat, 16 Mar 2013 00:32:11 +0800

Quoted 6 lines

netdev_create_hash() is divded into two steps:
1. allocate space for hash_head
2. initialize hash_head->first to NULL for each hash_head

This patch merge the two steps into one step.

Quoted 7 lines

When allocating the space for hash_head, it will use kzalloc() instead of kmalloc(). Then hash_head->first is set to NULL during the allocation step, which means it is not necessary to call INIT_HLIST_HEAD() for each hash_head.

This will:
1. reduce the code size
2. reduce the run time of iteration on initializing hash_head array

Quoted 6 lines

Please do not ever post changes like this.

This makes assumtions about the list head implementation that we should not make. If someone adds a debugging facility that causes lists to be initialized differently, it will be broken for this hash table now.

Yes, this is based on the assumtion.
And agree, this will break the behavior.

Quoted 8 lines

The reason the abstractions exist is so that people do not do things like you are trying to do.

If you want to add an interface in the generic list facilities which performs this simplifcation, fine. Implement it and post such a suggestion to linux-kernel, and then once such a facility is present in Linus's tree we can start using it in the networkng code.

Thanks for your suggestion and kindness.

Signature · 6 lines
Richard Yang
Help you, Help me

-- 
Richard Yang
Help you, Help me