<p>Consider something like the following for webgl (untested but should work):</p>

<pre><code>const lookup = new Map();
const isEqual3fv = (v1, v2) =&gt; {
 return v1[0] === v2[0] &amp;&amp; v1[1] === v2[1] &amp;&amp; v1[2] === v2[2];
}

const setUniform3fv = (location, value) =&gt; {
  if(!lookup.has(location) || !isEqual3fv(lookup.get(location), value)) {
    gl.uniform3fv(location, value);
    lookup.set(location, value);
  }
}
</code></pre>

<p>Would this be an optimization in most cases? Is caching the values and comparing for equality before setting the uniform generally a worthwhile optimization - or is the difference pretty negligible?</p>

CC BY-SA 3.0
2018-03-12T15:07:48.133Z
Hell, I sometimes tend to not even check the uniform locations and just let `glUniform` silently abort on -1. ;-)
Christian Rau
CC BY-SA 3.0
2018-03-27T14:03:58.747Z
As with most things CG (and most things programming, even), it is probably a tradeoff, and should be evaluated on a case-by-case basis. Run some benchmarks. If the memory used by your cache is limiting something else in your program then it might become an issue, you will have to think very hard to decide if the extra speed is worth the memory overhead
Sebastián Mestre
<webgl>
Is it worth caching uniform values?
davidkomer
# Is it worth caching uniform values?

(Asked by: username_0 on 2018-03-12)

Consider something like the following for webgl (untested but should work):

```
const lookup = new Map();
const isEqual3fv = (v1, v2) => {
 return v1[0] === v2[0] && v1[1] === v2[1] && v1[2] === v2[2];
}

const setUniform3fv = (location, value) => {
  if(!lookup.has(location) || !isEqual3fv(lookup.get(location), value)) {
    gl.uniform3fv(location, value);
    lookup.set(location, value);
  }
}

```

Would this be an optimization in most cases? Is caching the values and comparing for equality before setting the uniform generally a worthwhile optimization - or is the difference pretty negligible?

**Tags:** webgl

> **Comments:**
> **username_1**: Hell, I sometimes tend to not even check the uniform locations and just let `glUniform` silently abort on -1. ;-)  
> **username_2**: As with most things CG (and most things programming, even), it is probably a tradeoff, and should be evaluated on a case-by-case basis. Run some benchmarks. If the memory used by your cache is limiting something else in your program then it might become an issue, you will have to think very hard to decide if the extra speed is worth the memory overhead  

---

## 0 answers

