3 ms·
This isn't the only bug/bad practice that the git-copilot page shows off. Here's the memoization for js suggestion on the main page: const memoize = fn => {
by tynpeddler 5y ago
This isn't the only bug/bad practice that the git-copilot page shows off. Here's the memoization for js suggestion on the main page:
const memoize = fn => {
const cache = {};
return (...args) => {
const key = JSON.stringify(args);
return (cache[key] = cache[key] || fn(...args));
};
}
It uses js falsyness to figure out whether it can return from the cache or if it needs to invoke the wrapped function. However, js falsy is pretty dangerous. "cache[key]" will return undefined if there's no value in the cache for those arguments, but undefined is not the only falsy value. Here's the full list: https://developer.mozilla.org/en-US/docs/Glossary/Falsy https://developer.mozilla.org/en-US/docs/Glossary/Falsy
Many of those values are reasonable function return values meaning your cache will simply not work for some function outputs.
The key generation is also a little problematic. Stringifying the input may produce huge strings which are then kept in memory for an indefinite period of time which creates a memory leak.
Here's the bottom line on git co-pilot. It's a huge step forward and I think everyone is going to be using tools like it in the future. There's no doubt to me that it will make good programmers way more productive. However, not-so-good programmers will become way more destructive since copilot will let them write bad, unoptimized code faster than every before.
- bpeebles 5y agoThe Python version is also goofy since it doesn't support keyword arguments for the wrapped function: def memoize(func): cache = {} def wrapper(*args): if args in cache: return cache[args] else: cache[args] = func(*args) return cache[args] return wrapper and why wouldn't you just use # or @functoolscache (3.9+). @functools.lru_cache(maxsize=None) def f(*args, **kwargs): pass