Repository: dotnet/runtime
File: src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWriter.cs (C#)

Code before:
        {
            SectionData data = new();
            SectionWriter headerWriter = new(this, 0, data);
            headerWriter.WriteLittleEndian(WasmMagicNumber);
            headerWriter.WriteLittleEndian(WasmVersion);

            data.GetReadStream().CopyTo(outputFileStream);
        }

        public override void EmitObject(Stream outputFileStream, IReadOnlyCollection<DependencyNode> nodes, IObjectDumper dumper, Logger logger)
        {
            ArrayBuilder<WasmFuncType> methodSignatures = new();
            ArrayBuilder<IMethodBodyNode> methodBodies = new();
            foreach (DependencyNode node in nodes)
            {
                if (node is IMethodBodyNode methodNode)
                {
                    methodSignatures.Add(_wasmAbiContext.GetSignature(methodNode.Method));
                    methodBodies.Add(methodNode);
                    // TODO: record relocations and attached data
                }
            }

            var uniqueSignatures = methodSignatures.ToArray().Distinct();
            var signatureMap = uniqueSignatures.Select((sig, i) => (sig, i)).ToDictionary();

            EmitWasmHeader(outputFileStream);
            EmitSection(() => WriteTypeSection(uniqueSignatures, logger), outputFileStream, logger);
            EmitSection(() => WriteFunctionSection(methodSignatures.ToArray(), signatureMap, logger), outputFileStream, logger);
            EmitSection(() => WriteExportSection(methodBodies.ToArray(), methodSignatures.ToArray(), signatureMap, logger), outputFileStream, logger);
            EmitSection(() => WriteCodeSection(methodBodies.ToArray(), logger), outputFileStream, logger);
        }

        private WasmSection WriteExportSection(IReadOnlyList<IMethodBodyNode> methodNodes, IReadOnlyList<WasmFuncType> methodSignatures,
            Dictionary<WasmFuncType, int> signatureMap, Logger logger)
        {
            WasmSection exportSection = new WasmSection(WasmSectionType.Export, new SectionData(), "export");
            SectionWriter writer = exportSection.Writer;
            // Write the number of exports
            writer.WriteULEB128((ulong)methodNodes.Count);
            for (int i = 0; i < methodNodes.Count; i++)
            {
                var methodNode = methodNodes[i];
                var methodSignature = methodSignatures[i];
                string exportName = methodNode.GetMangledName(_nodeFactory.NameMangler);

                var length = Encoding.UTF8.GetByteCount(exportName);
                writer.WriteULEB128((ulong)length);
                writer.WriteUtf8StringNoNull(exportName);
                writer.WriteByte(0x00); // export kind: function
                writer.WriteULEB128((ulong)i);

Review comment (refactor):
Here and elsewhere: there seems to be a lot of use of interfaces / abstracter collection types, but this is private code. If we have `ArrayBuilder` (or even simply `List<T>`) we can use that in these private methods and save both working set and time.

Code after:
                {
                    logger.LogMessage($"Emitting export: {exportName} for function index {i}");
                }
            }

            return exportSection;
        }

        private WasmSection WriteCodeSection(ArrayBuilder<ObjectData> methodBodies, Logger logger)
        {
            WasmSection codeSection = new WasmSection(WasmSectionType.Code, new SectionData(), "code");
            SectionWriter writer = codeSection.Writer;

            // Write the number of functions
            writer.WriteULEB128((ulong)methodBodies.Count);
            for (int i = 0; i < methodBodies.Count; i++)
            {
                ObjectData methodBody = methodBodies[i];
                writer.WriteULEB128((ulong)methodBody.Data.Length);
                writer.EmitData(methodBody.Data);
            }

            return codeSection;
        }

        private WasmSection WriteTypeSection(IEnumerable<WasmFuncType> functionSignatures, Logger logger)
        {
            SectionData sectionData = new();
            WasmSection typeSection = new WasmSection(WasmSectionType.Type, sectionData, "type");
            SectionWriter writer = typeSection.Writer;

            // Write the number of types
            writer.WriteULEB128((ulong)functionSignatures.Count());

            IBufferWriter<byte> buffer = sectionData.BufferWriter;
            foreach (WasmFuncType signature in functionSignatures)
            {
                if (logger.IsVerbose)
                {
                    logger.LogMessage($"Emitting function signature: {signature}");
                }

                int signatureSize = signature.EncodeSize();
                signature.Encode(buffer.GetSpan(signatureSize));
                buffer.Advance(signatureSize);
            }

            return typeSection;
        }

        private WasmSection WriteFunctionSection(ArrayBuilder<WasmFuncType> allFunctionSignatures, Dictionary<WasmFuncType, int> signatureMap, Logger logger)