Skip to contentSkip to conversation

hash: Remove useless init_hash()

PATCH In review Stephen Boyd 5 participants 7 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hash: Remove useless init_hash()

init_hash() is essentially a memset() so just use that.

---
 Documentation/technical/api-hash.txt |    4 ----
 diffcore-rename.c                    |    2 +-
 hash.h                               |    7 -------
 3 files changed, 1 insertions(+), 12 deletions(-)
Documentation/technical/api-hash.txt +0 -4
index e5061e0..7cf64ec 100644
@@ -23,10 +23,6 @@ Data Structures
 Functions
 ---------
 
-`init_hash`::
-
-	Initialize the hash table.
-
 `free_hash`::
 
 	Release memory associated with the hash table.
diffcore-rename.c +1 -1
index df41be5..b355520 100644
@@ -382,7 +382,7 @@ static int find_exact_renames(void)
 	int i;
 	struct hash_table file_table;
 
-	init_hash(&file_table);
+	memset(&file_table, 0, sizeof(file_table));
 	for (i = 0; i < rename_src_nr; i++)
 		insert_file_table(&file_table, -1, i, rename_src[i].one);
 
hash.h +0 -7
index 69e33a4..418be24 100644
@@ -33,11 +33,4 @@ extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table
 extern int for_each_hash(const struct hash_table *table, int (*fn)(void *));
 extern void free_hash(struct hash_table *table);
 
-static inline void init_hash(struct hash_table *table)
-{
-	table->size = 0;
-	table->nr = 0;
-	table->array = NULL;
-}
-
 #endif
Signature · 1 line
1.7.2.19.g9a302
#1 lore raw
Quoted 3 lines · Stephen Boyd

init_hash() is essentially a memset() so just use that.

[...]

Encapsulation is good.

diffcore-rename.c static int find_exact_renames(void) · line 385

Quoted 10 lines
diffcore-rename.c +1 -1
index df41be5..b355520 100644
@@ -382,7 +382,7 @@ static int find_exact_renames(void)
 	int i;
 	struct hash_table file_table;
 
-	init_hash(&file_table);
+	memset(&file_table, 0, sizeof(file_table));

This is IMHO slightly less readable, and doesn't protect against changes in implementation.

Quoted 8 lines
diffcore-rename.c +0 -0
 	for (i = 0; i < rename_src_nr; i++)
 		insert_file_table(&file_table, -1, i, rename_src[i].one);
 
hash.h +0 -0
index 69e33a4..418be24 100644

hash.h extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table · line 41

Quoted 9 lines
hash.h +0 -6
@@ -33,11 +33,4 @@ extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table
 extern int for_each_hash(const struct hash_table *table, int (*fn)(void *));
 extern void free_hash(struct hash_table *table);
 
-static inline void init_hash(struct hash_table *table)
-{
-	table->size = 0;
-	table->nr = 0;
-	table->array = NULL;
-}

*This* could be replaced by memset.

Quoted 5 lines
hash.h +0 -1
-
 #endif
Signature · 1 line
1.7.2.19.g9a302
Signature · 3 lines
Jakub Narebski
Poland
ShadeHawk on #git
#2 lore raw

On Tue, Jul 27, 2010 at 10:30, Jakub Narebski <[email protected]> wrote:

CC-ing Linus since he wrote it (per Documentation/SubmittingPatches).

Quoted 5 lines
Quoted 3 lines · Stephen Boyd

init_hash() is essentially a memset() so just use that.

Quoted 8 lines

[...]

Encapsulation is good.

[..]

This is IMHO slightly less readable, and doesn't protect against changes in implementation.

Agreed.

Quoted 11 lines
Quoted 11 lines

      for (i = 0; i < rename_src_nr; i++) insert_file_table(&file_table, -1, i, rename_src[i].one);

hash.h +0 -0
index 69e33a4..418be24 100644
@@ -33,11 +33,4 @@ extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table
  extern int for_each_hash(const struct hash_table *table, int (*fn)(void *));
  extern void free_hash(struct hash_table *table);
 
Quoted 8 lines

hash.h extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table · line 41

Quoted 6 lines
hash.h +0 -6
-static inline void init_hash(struct hash_table *table)
-{
-     table->size = 0;
-     table->nr = 0;
-     table->array = NULL;
-}

*This* could be replaced by memset.

No it couldn't? The second argument to memset is just an int, so setting the memory area to 0 isn't portable to systems where the representation of NULL isn't "0".

(It's early so I may be misremembering my C..)

#3 lore raw
Quoted 9 lines · Ævar Arnfjörð Bjarmason
Quoted 8 lines

hash.h extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table · line 41

Quoted 6 lines
hash.h +0 -6
-static inline void init_hash(struct hash_table *table)
-{
-     table->size = 0;
-     table->nr = 0;
-     table->array = NULL;
-}

*This* could be replaced by memset.

Quoted 5 lines

No it couldn't? The second argument to memset is just an int, so setting the memory area to 0 isn't portable to systems where the representation of NULL isn't "0".

(It's early so I may be misremembering my C..)

You're remembering your C correctly. It isn't portable, but it is so unlikely on modern machines that we simply don't care (and you will see memsets zero-ing pointers like this all through the git code, so this is certainly not introducing anything new).

That being said, I agree with the comments that removing init_hash actually makes the code _less_ readable. You could just replace these three lines with a memset, but why? It's just code churn.

-Peff

#4 lore raw
Quoted 15 lines · Jeff King
Quoted 13 lines · Ævar Arnfjörð Bjarmason
Quoted 8 lines

hash.h extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table · line 41

Quoted 6 lines
hash.h +0 -6
-static inline void init_hash(struct hash_table *table)
-{
-     table->size = 0;
-     table->nr = 0;
-     table->array = NULL;
-}

*This* could be replaced by memset.

No it couldn't? The second argument to memset is just an int, so setting the memory area to 0 isn't portable to systems where the representation of NULL isn't "0".

Quoted 6 lines
Quoted 1 line

(It's early so I may be misremembering my C..)

You're remembering your C correctly. It isn't portable, but it is so unlikely on modern machines that we simply don't care (and you will see memsets zero-ing pointers like this all through the git code, so this is certainly not introducing anything new).

Thanks for the confirmation. I was aware that NULL != 0 only occured on long-dead architechtures, but I suspected that some compiler out there would whine if it could statically determine that you were reading in a memzero'd area and using it as NULL. Evidently not, or at least nobody's complained.

Quoted 3 lines

That being said, I agree with the comments that removing init_hash actually makes the code _less_ readable. You could just replace these three lines with a memset, but why? It's just code churn.

Yeah, and for the record it also missed this part in hash.c:

    void free_hash(struct hash_table *table)
    {
    	free(table->array);
    	table->array = NULL;
    	table->size = 0;
    	table->nr = 0;
    }

Have fun everyone :)

#5 lore raw
Quoted 14 lines · Ævar Arnfjörð Bjarmason
Quoted 4 lines

That being said, I agree with the comments that removing init_hash actually makes the code _less_ readable. You could just replace these three lines with a memset, but why? It's just code churn.

Yeah, and for the record it also missed this part in hash.c:

     void free_hash(struct hash_table *table)
     {
     	free(table->array);
     	table->array = NULL;
     	table->size = 0;
     	table->nr = 0;
     }
Quoted 1 line

Have fun everyone :)

Ok, seems like nobody thinks this is a good idea so I'm fine with dropping it. The minimal savings (if at all) doesn't seem worth the code churn. One less patch in the queue ;-)

#6 lore raw
In reply to Stephen Boyd
Quoted 2 lines · Stephen Boyd

init_hash() is essentially a memset() so just use that.

I disagree.

Yes, technically right now the initialization just zeroes out all the fields, and memset() does the same. But there is no advantage to using memset(), since init_hash() will create the same or better code, and using init_hash() is way more readable.

Also, it's not at all the case that init_hash() is always going to be a memset(). Imagine a threaded hash-table with a lock associated with it or something. Now, admittedly that's not necessarily something we'd ever do in git, but I still think it's simply a good idea to have a clear "initialize this" routine, rather than depending on the fact that zeroing it out is sufficient.

Linus