[Mesa-dev] [PATCH 15/15] glsl: use a non-malloc'd storage for short ir_variable names

Marek Olšák maraeo at gmail.com
Sat Oct 8 10:58:39 UTC 2016


From: Marek Olšák <marek.olsak at amd.com>

---
 src/compiler/glsl/ir.cpp                    |  4 ++++
 src/compiler/glsl/ir.h                      | 13 ++++++++++++-
 src/compiler/glsl/lower_packed_varyings.cpp |  8 ++++++--
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/compiler/glsl/ir.cpp b/src/compiler/glsl/ir.cpp
index c5943e5..8e4b382 100644
--- a/src/compiler/glsl/ir.cpp
+++ b/src/compiler/glsl/ir.cpp
@@ -1516,20 +1516,24 @@ ir_variable::ir_variable(const struct glsl_type *type, const char *name,
    assert(name != NULL
           || mode == ir_var_temporary
           || mode == ir_var_function_in
           || mode == ir_var_function_out
           || mode == ir_var_function_inout);
    assert(name != ir_variable::tmp_name
           || mode == ir_var_temporary);
    if (mode == ir_var_temporary
        && (name == NULL || name == ir_variable::tmp_name)) {
       this->name = ir_variable::tmp_name;
+   } else if (name == NULL ||
+              strlen(name) < ARRAY_SIZE(this->name_storage)) {
+      strcpy(this->name_storage, name ? name : "");
+      this->name = this->name_storage;
    } else {
       this->name = ralloc_strdup(this, name);
    }
 
    this->u.max_ifc_array_access = NULL;
 
    this->data.explicit_location = false;
    this->data.has_initializer = false;
    this->data.location = -1;
    this->data.location_frac = 0;
diff --git a/src/compiler/glsl/ir.h b/src/compiler/glsl/ir.h
index 83b810b..95571b6 100644
--- a/src/compiler/glsl/ir.h
+++ b/src/compiler/glsl/ir.h
@@ -592,21 +592,22 @@ public:
 
    inline bool is_interpolation_flat() const
    {
       return this->data.interpolation == INTERP_MODE_FLAT ||
              this->type->contains_integer() ||
              this->type->contains_double();
    }
 
    inline bool is_name_ralloced() const
    {
-      return this->name != ir_variable::tmp_name;
+      return this->name != ir_variable::tmp_name &&
+             this->name != this->name_storage;
    }
 
    /**
     * Enable emitting extension warnings for this variable
     */
    void enable_extension_warning(const char *extension);
 
    /**
     * Get the extension warning string for this variable
     *
@@ -617,20 +618,30 @@ public:
    /**
     * Declared type of the variable
     */
    const struct glsl_type *type;
 
    /**
     * Declared name of the variable
     */
    const char *name;
 
+private:
+   /**
+    * If the name length fits into name_storage, it's used, otherwise
+    * the name is ralloc'd. shader-db mining showed that 70% of variables
+    * fit here. This is a win over ralloc where only ralloc_header has
+    * 20 bytes on 64-bit (28 bytes with DEBUG), and we can also skip malloc.
+    */
+   char name_storage[16];
+
+public:
    struct ir_variable_data {
 
       /**
        * Is the variable read-only?
        *
        * This is set for variables declared as \c const, shader inputs,
        * and uniforms.
        */
       unsigned read_only:1;
       unsigned centroid:1;
diff --git a/src/compiler/glsl/lower_packed_varyings.cpp b/src/compiler/glsl/lower_packed_varyings.cpp
index 1e8cf11..19bbe57 100644
--- a/src/compiler/glsl/lower_packed_varyings.cpp
+++ b/src/compiler/glsl/lower_packed_varyings.cpp
@@ -632,22 +632,26 @@ lower_packed_varyings_visitor::get_packed_varying_deref(
       packed_var->data.location = location;
       packed_var->data.precision = unpacked_var->data.precision;
       packed_var->data.always_active_io = unpacked_var->data.always_active_io;
       unpacked_var->insert_before(packed_var);
       this->packed_varyings[slot] = packed_var;
    } else {
       /* For geometry shader inputs, only update the packed variable name the
        * first time we visit each component.
        */
       if (this->gs_input_vertices == 0 || vertex_index == 0) {
-         ralloc_asprintf_append((char **) &this->packed_varyings[slot]->name,
-                                ",%s", name);
+         ir_variable *var = this->packed_varyings[slot];
+
+         if (var->is_name_ralloced())
+            ralloc_asprintf_append((char **) &var->name, ",%s", name);
+         else
+            var->name = ralloc_asprintf(var, "%s,%s", var->name, name);
       }
    }
 
    ir_dereference *deref = new(this->mem_ctx)
       ir_dereference_variable(this->packed_varyings[slot]);
    if (this->gs_input_vertices != 0) {
       /* When lowering GS inputs, the packed variable is an array, so we need
        * to dereference it using vertex_index.
        */
       ir_constant *constant = new(this->mem_ctx) ir_constant(vertex_index);
-- 
2.7.4



More information about the mesa-dev mailing list