zed icon indicating copy to clipboard operation
zed copied to clipboard

Load embedded fonts directly from .rdata instead of cloning

Open osiewicz opened this issue 5 months ago • 1 comments

The fonts we embed in Zed binary (Zed Sans & Zed Mono) weigh about 30Mb in total and we are cloning them several times during startup and loading of embedded assets (once explicitly in Zed and then under the hood in font-kit). Moreover, after loading we have at least 2 copies of each font in our program; one in .rdata and the other on the heap for use by font-kit. This commit does away with that distinction (we're no longer allocating the font data) and slightly relaxes the interface of TextSystem::add_fonts by expecting one to pass Cow<[u8]> instead of Arc<Vec<u8>>. Additionally, AssetSource::get now returns Cow<'static, [u8]> instead of Cow<'self, [u8]>; all existing implementations conform with that change.

Note that this optimization takes effect only in Release builds, as the library we use for asset embedding - rust-embed - embeds the assets only in Release mode and in Dev builds it simply loads data from disk. Thus it returns Cow<[u8]> in it's interface. Therefore, we still copy that memory around in Dev builds, but that's not really an issue. This patch makes no assumptions about the build profile we're running under, that's just an intrinsic property of rust-embed.

Tl;dr: this should shave off about 30Mb of memory usage and a fair chunk (~30ms) of startup time.

Release Notes:

  • Improved startup time and memory usage.

osiewicz avatar Jan 28 '24 14:01 osiewicz

/cc @as-cii , would love to get your take on this change.

osiewicz avatar Jan 28 '24 14:01 osiewicz