Skip to content

refactor(symbols): replace get_name_for_subnet match with static NAMES table#2607

Open
boskodev790 wants to merge 1 commit intoopentensor:mainfrom
boskodev790:refactor/symbols-static-names-table
Open

refactor(symbols): replace get_name_for_subnet match with static NAMES table#2607
boskodev790 wants to merge 1 commit intoopentensor:mainfrom
boskodev790:refactor/symbols-static-names-table

Conversation

@boskodev790
Copy link
Copy Markdown

## Summary

- `get_name_for_subnet` was a 439-arm `match` where every arm called `b"name".to_vec()`, allocating a fresh `Vec<u8>` on every call even though the subnet names are compile-time constants; replace it with a parallel `pub static NAMES: [&[u8]; 439]` lookup table that mirrors the existing `SYMBOLS` array by `netuid`
- `get_name_for_subnet` now prefers a non-empty `SubnetIdentitiesV3::subnet_name` (unchanged), falls back to `NAMES[netuid]` as `&'static [u8]`, returns `b"unknown"` for out-of-range netuids, and pays exactly one `.to_vec()` at the return boundary instead of one per match arm
- All 439 name-to-netuid mappings are preserved byte-for-byte, including the deliberate `b"unknown"` sentinel slots at indices 55 and 60 and the single multi-word entry `b"red team"` at index 61 — cross-checked programmatically against the original match before and after the edit
- Add `pallets/subtensor/src/tests/symbols.rs` with six `#[test]` cases covering first / last / space-containing / sentinel entries, out-of-range fallback, stored-identity-wins-over-table, empty-identity-falls-through-to-table, and a length invariant between `NAMES` and `SYMBOLS` so a future edit to one without the other fails fast in CI
- External contract is unchanged — the function signature still returns `Vec<u8>` and all three callsites (`rpc_info::dynamic_info::subnet_name`, `rpc_info::metagraph::name` and its helper) continue to receive the same bytes

## Related Issues

Closes #1520.

## Type of Change

- [ ] Bug fix
- [ ] New feature
- [x] Refactor
- [ ] Documentation
- [ ] Other (describe below)

## Testing

- [x] Tests added — `tests/symbols.rs` pins the six behavioural contracts listed above
- [x] Refactor is behaviour-preserving — verified programmatically by extracting every `idx => b"name".to_vec()` pair from the original `main`-branch file and asserting 1-for-1 equality with the new `NAMES` array entries (439 of 439 matched)
- [ ] `cargo check` / `cargo test --package pallet-subtensor` / `cargo clippy` — not run locally because the Rust toolchain and the Substrate dependency graph are not installed in this environment; the diff is structurally identical to the sibling `get_symbol_for_subnet` pattern already present in the same file

## Checklist

- [x] Code follows project style guidelines (mirrors `get_symbol_for_subnet` pattern)
- [x] Self-review completed
- [x] No functional change — all three production callers receive the same bytes

…S table

Closes opentensor#1520.

`get_name_for_subnet` was a 439-arm `match` over `netuid` where every
arm called `b"name".to_vec()`. Each call to the function allocated a
fresh `Vec<u8>` regardless of the hit, even though the subnet names are
compile-time constants. The per-call allocation is wasted work, and the
massive match made the function painful to scan during review.

Replace the match with a parallel `pub static NAMES: [&[u8]; 439]`
lookup table that mirrors `SYMBOLS` by netuid. `get_name_for_subnet`
now:

  * prefers a non-empty `SubnetIdentitiesV3::subnet_name` (unchanged),
  * falls back to `NAMES[netuid]` as `&'static [u8]`,
  * returns `b"unknown"` for out-of-range netuids,
  * pays exactly one `.to_vec()` at the return boundary instead of
    one per match arm.

All 439 name-to-netuid mappings are preserved byte-for-byte, including
the deliberate `b"unknown"` sentinel slots at indices 55 and 60 and the
single multi-word entry `b"red team"` at index 61 (cross-checked
programmatically against the original match).

Add `pallets/subtensor/src/tests/symbols.rs` covering:

  * first / last / space-containing / sentinel entries,
  * out-of-range netuid (439, 1000, u16::MAX) falling back to
    `b"unknown"`,
  * a stored `SubnetIdentitiesV3` with a non-empty name winning over
    the table,
  * a stored identity with an empty `subnet_name` still falling
    through to the table,
  * a length invariant between `NAMES` and `SYMBOLS` so a future edit
    to one without the other fails fast in CI.

External contract is unchanged: the function signature still returns
`Vec<u8>` and all three callsites
(`rpc_info::dynamic_info::subnet_name`, `rpc_info::metagraph::name`
and its helper) continue to receive the same bytes.
@boskodev790 boskodev790 reopened this May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant