From 85ced3fbfdf25e95a9dc9df4a2e8e5c015ff2858 Mon Sep 17 00:00:00 2001
From: Jonas Jenwald <jonas.jenwald@gmail.com>
Date: Sat, 4 Jul 2020 14:52:10 +0200
Subject: [PATCH] Allow `BaseLocalCache` to, optionally, only allocate storage
 for caching of references (PR 12034 follow-up)

*Yet another instalment in the never-ending series of things that you think of __after__ a patch has landed.*

Since `Function`s are only cached by reference, we thus don't need to allocate storage for names in `LocalFunctionCache` instances. Obviously the effect of these changes are *really tiny*, but it seems reasonable in principle to avoid allocating data structures that are guaranteed to be unused.
---
 src/core/image_utils.js | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/core/image_utils.js b/src/core/image_utils.js
index c42c5d8e4..b7498d438 100644
--- a/src/core/image_utils.js
+++ b/src/core/image_utils.js
@@ -18,12 +18,14 @@ import { assert, info, shadow, unreachable } from "../shared/util.js";
 import { RefSetCache } from "./primitives.js";
 
 class BaseLocalCache {
-  constructor() {
+  constructor(options) {
     if (this.constructor === BaseLocalCache) {
       unreachable("Cannot initialize BaseLocalCache.");
     }
-    this._nameRefMap = new Map();
-    this._imageMap = new Map();
+    if (!options || !options.onlyRefs) {
+      this._nameRefMap = new Map();
+      this._imageMap = new Map();
+    }
     this._imageCache = new RefSetCache();
   }
 
@@ -92,6 +94,10 @@ class LocalColorSpaceCache extends BaseLocalCache {
 }
 
 class LocalFunctionCache extends BaseLocalCache {
+  constructor(options) {
+    super({ onlyRefs: true });
+  }
+
   getByName(name) {
     unreachable("Should not call `getByName` method.");
   }