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

Code before:
                    continue;
                }

                if (node is IMethodBodyNode methodNode)
                {
                    methodSignatures.Add(_wasmAbiContext.GetSignature(methodNode.Method));
                    methodBodies.Add(methodNode);
                    // TODO: record relocations and attached data
                }
            }

            Dictionary<WasmFuncType, int> signatureMap = new();
            int signatureIndex = 0;
            for (int i = 0; i < methodSignatures.Count; i++)
            {
                if (!signatureMap.ContainsKey(methodSignatures[i]))
                {
                    signatureMap[methodSignatures[i]] = signatureIndex++;
                }
            }

            WasmFuncType[] uniqueSignatures = new WasmFuncType[signatureMap.Count];
            foreach (var kvp in signatureMap)
            {
                uniqueSignatures[kvp.Value] = kvp.Key;
            }

            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(IReadOnlyCollection<IMethodBodyNode> methodNodes, IReadOnlyCollection<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.ElementAt(i);
                var methodSignature = methodSignatures.ElementAt(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

Review comment (suggestion):
nit (i may be drawn and quartered for suggesting this) you could do all of this in a one liner sort of like `methodSignatures.Distinct().Select((index, signature) => (index, signature)).ToDictionary()` if I'm not mistaken. From what I saw `Distinct` is about as fast as using a `HashSet`. If we need determinism we can just `OrderBy` the distinct set before `Select`ing.

Code after:
                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)
        {
            WasmSection functionSection = new WasmSection(WasmSectionType.Function, new SectionData(), "function");
            SectionWriter writer = functionSection.Writer;
            // Write the number of functions
            writer.WriteULEB128((ulong)allFunctionSignatures.Count);
            for (int i = 0; i < allFunctionSignatures.Count; i++)
            {
                WasmFuncType signature = allFunctionSignatures[i];
                writer.WriteULEB128((ulong)signatureMap[signature]);
            }
            return functionSection;
        }

        private void EmitSection(Func<WasmSection> writeFunc, Stream outputFileStream, Logger logger)
        {
            WasmSection section = writeFunc();
            Span<byte> headerBuffer = stackalloc byte[section.HeaderSize];