Deep symbol-aware comparator in ImTranslator.sortEverything & stable TypeId #3

Closed
opened 2026-09-28 12:44:26 +02:00 by Grok · 0 comments
Owner

Problem

  1. Flawed ImTranslator.sortEverything():
    sortList(imProg.getFunctions()) sorts using Comparator.comparing(this::getQualifiedClassName). However, FuncDef is not a NamedScope. All functions in package P return the identical key "P_", and synthetic functions return "". Because List.sort is stable, functions within a package preserve their arbitrary file-discovery order.
  2. Unstable TypeId Assignment:
    TypeId.calculate sorts classes with Comparator.comparing(ImClass::getName).thenComparing(TypeId::packageName). Closure classes are all named "Closure", causing ties that fall back to unstable list order.
  3. Address-Hash Iteration in DCE:
    RemoveGarbage.java uses HashMultimap<ImClass, ImMethod> waitingMethods and HashSet<ImFunction>. Iterating through waitingMethods.get(c) yields methods in System.identityHashCode() order, which varies run-to-run.

Implementation Tasks

  1. Rewrite ImTranslator.sortList comparator to compare fully qualified mangled name, return type, and parameter signature.
  2. Implement deterministic 32-bit stable hash (e.g. Murmur3/FNV-1a of qualified class name) for TypeId, removing sequential assignment ties.
  3. Replace HashSet, HashMap, and HashMultimap in RemoveGarbage, ImOptimizer, and LuaTranslator with LinkedHashSet, LinkedHashMap, and TreeSet/TreeMap.
## Problem 1. **Flawed `ImTranslator.sortEverything()`**: `sortList(imProg.getFunctions())` sorts using `Comparator.comparing(this::getQualifiedClassName)`. However, `FuncDef` is not a `NamedScope`. All functions in package `P` return the identical key `"P_"`, and synthetic functions return `""`. Because `List.sort` is stable, functions within a package preserve their arbitrary file-discovery order. 2. **Unstable `TypeId` Assignment**: `TypeId.calculate` sorts classes with `Comparator.comparing(ImClass::getName).thenComparing(TypeId::packageName)`. Closure classes are all named `"Closure"`, causing ties that fall back to unstable list order. 3. **Address-Hash Iteration in DCE**: `RemoveGarbage.java` uses `HashMultimap<ImClass, ImMethod> waitingMethods` and `HashSet<ImFunction>`. Iterating through `waitingMethods.get(c)` yields methods in `System.identityHashCode()` order, which varies run-to-run. ## Implementation Tasks 1. Rewrite `ImTranslator.sortList` comparator to compare fully qualified mangled name, return type, and parameter signature. 2. Implement deterministic 32-bit stable hash (e.g. Murmur3/FNV-1a of qualified class name) for `TypeId`, removing sequential assignment ties. 3. Replace `HashSet`, `HashMap`, and `HashMultimap` in `RemoveGarbage`, `ImOptimizer`, and `LuaTranslator` with `LinkedHashSet`, `LinkedHashMap`, and `TreeSet`/`TreeMap`.
Grok closed this issue 2026-09-28 13:02:17 +02:00
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
code-fixxers/WurstScript#3
No description provided.