Skip to contentSkip to conversation

mm/zswap: add minor const/conditional optimizations

PATCH NAK Joey Pabalinas 2 participants 5 messages Jan 2, 2018 – Jan 8, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover mm/zswap: add minor const/conditional optimizations

Make a couple minor short-circuiting order and const changes
- Since the pointed-to objects are never modified through these pointers, const-qualify type and compressor variables.
- Test boolean before calling strcmp() to take advantage of short-circuiting.

Joey Pabalinas (2):
mm/zswap: make type and compressor const mm/zswap: move zswap_has_pool to front of if ()

 mm/zswap.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
Signature · 7 lines
2.15.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw

PATCH 1/2 mm/zswap: make type and compressor const

The characters pointed to by zswap_compressor, type, and compressor aren't ever modified. Add const to the static variable and both parameters in zswap_pool_find_get(), zswap_pool_create(), and __zswap_param_set()

 1 file changed, 6 insertions(+), 4 deletions(-)
mm/zswap.c +6 -4
index d39581a076c3aed1e9..a4f2dfaf9131694265 100644
@@ -90,7 +90,7 @@ module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644);
 
 /* Crypto compressor to use */
 #define ZSWAP_COMPRESSOR_DEFAULT "lzo"
-static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
+static const char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
 static int zswap_compressor_param_set(const char *,
 				      const struct kernel_param *);
 static struct kernel_param_ops zswap_compressor_param_ops = {
@@ -475,7 +475,8 @@ static struct zswap_pool *zswap_pool_last_get(void)
 }
 
 /* type and compressor must be null-terminated */
-static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
+static struct zswap_pool *zswap_pool_find_get(const char *type,
+					      const char *compressor)
 {
 	struct zswap_pool *pool;
 
@@ -495,7 +496,8 @@ static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
 	return NULL;
 }
 
-static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
+static struct zswap_pool *zswap_pool_create(const char *type,
+					    const char *compressor)
 {
 	struct zswap_pool *pool;
 	char name[38]; /* 'zswap' + 32 char (max) num + \0 */
@@ -658,7 +660,7 @@ static void zswap_pool_put(struct zswap_pool *pool)
 
 /* val must be a null-terminated string */
 static int __zswap_param_set(const char *val, const struct kernel_param *kp,
-			     char *type, char *compressor)
+			     const char *type, const char *compressor)
 {
 	struct zswap_pool *pool, *put_pool = NULL;
 	char *s = strstrip((char *)val);
Signature · 7 lines
2.15.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw

Re: [PATCH 1/2] mm/zswap: make type and compressor const

Quoted 5 lines · Joey Pabalinas

The characters pointed to by zswap_compressor, type, and compressor aren't ever modified. Add const to the static variable and both parameters in zswap_pool_find_get(), zswap_pool_create(), and __zswap_param_set()

Nak.

Those variables are not const; they are updated in __zswap_param_set(). They aren't modified in pool_find_get() or pool_create(), but they certainly aren't globally const.

Quoted 46 lines
 1 file changed, 6 insertions(+), 4 deletions(-)
mm/zswap.c +6 -5
index d39581a076c3aed1e9..a4f2dfaf9131694265 100644
@@ -90,7 +90,7 @@ module_param_cb(enabled, &zswap_enabled_param_ops, &zswap_enabled, 0644);
 
 /* Crypto compressor to use */
 #define ZSWAP_COMPRESSOR_DEFAULT "lzo"
-static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
+static const char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT;
 static int zswap_compressor_param_set(const char *,
                                      const struct kernel_param *);
 static struct kernel_param_ops zswap_compressor_param_ops = {
@@ -475,7 +475,8 @@ static struct zswap_pool *zswap_pool_last_get(void)
 }
 
 /* type and compressor must be null-terminated */
-static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
+static struct zswap_pool *zswap_pool_find_get(const char *type,
+                                             const char *compressor)
 {
        struct zswap_pool *pool;
 
@@ -495,7 +496,8 @@ static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
        return NULL;
 }
 
-static struct zswap_pool *zswap_pool_create(char *type, char *compressor)
+static struct zswap_pool *zswap_pool_create(const char *type,
+                                           const char *compressor)
 {
        struct zswap_pool *pool;
        char name[38]; /* 'zswap' + 32 char (max) num + \0 */
@@ -658,7 +660,7 @@ static void zswap_pool_put(struct zswap_pool *pool)
 
 /* val must be a null-terminated string */
 static int __zswap_param_set(const char *val, const struct kernel_param *kp,
-                            char *type, char *compressor)
+                            const char *type, const char *compressor)
 {
        struct zswap_pool *pool, *put_pool = NULL;
        char *s = strstrip((char *)val);
--
Quoted 2 lines

2.15.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw

PATCH 2/2 mm/zswap: move `zswap_has_pool` to front of `if ()`

In reply to Joey Pabalinas

zwap_has_pool is a simple boolean, so it should be tested first to avoid unnecessarily calling strcmp(). Test zswap_has_pool first to take advantage of the short-circuiting behavior of && in __zswap_param_set().

 1 file changed, 1 insertion(+), 1 deletion(-)
mm/zswap.c +1 -1
index a4f2dfaf9131694265..dbf35139471f692798 100644
@@ -672,7 +672,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp,
 	}
 
 	/* no change required */
-	if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool)
+	if (zswap_has_pool && !strcmp(s, *(char **)kp->arg))
 		return 0;
 
 	/* if this is load-time (pre-init) param setting,
Signature · 7 lines
2.15.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw

Re: [PATCH 2/2] mm/zswap: move `zswap_has_pool` to front of `if ()`

Quoted 14 lines · Joey Pabalinas

zwap_has_pool is a simple boolean, so it should be tested first to avoid unnecessarily calling strcmp(). Test zswap_has_pool first to take advantage of the short-circuiting behavior of && in __zswap_param_set().

 1 file changed, 1 insertion(+), 1 deletion(-)
mm/zswap.c +0 -0
index a4f2dfaf9131694265..dbf35139471f692798 100644

mm/zswap.c static int __zswap_param_set(const char *val, const struct kernel_param *kp, · line 675

Quoted 5 lines
mm/zswap.c +1 -1
@@ -672,7 +672,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp,
        }
 
        /* no change required */
-       if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool)
+       if (zswap_has_pool && !strcmp(s, *(char **)kp->arg))

Nak.

This function is only called when actually changing one of the zswap module params, which is extremely rare (typically once per boot, per parameter, if at all). Changing the ordering will have virtually no noticeable impact on anything.

Additionally, !zswap_has_pool is strictly an initialization-failed temporary situation (until the compressor/zpool params are be set to working implementation values), and in all "normal" conditions it will be true, meaning this reordering will actually
*add* time - the normal path is for this check to *not* be true, so keeping the strcmp first bypasses bothering with checking zswap_has_pool.

Quoted 6 lines
mm/zswap.c +0 -1
                return 0;
 
        /* if this is load-time (pre-init) param setting,
--

2.15.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>