Skip to content

Fix region threading with MapItemSavedData - #538

Closed
NanaChiyo0721 wants to merge 1 commit into
PaperMC:ver/26.3.xfrom
NanaChiyo0721:ver/26.3.x-patch-1
Closed

NanaChiyo0721 wants to merge 1 commit into
PaperMC:ver/26.3.xfrom
NanaChiyo0721:ver/26.3.x-patch-1

Conversation

@NanaChiyo0721

Copy link
Copy Markdown
Contributor

The hasMapInvisibilityItemEquipped was called in tickCarriedBy, might access another player's inventory without any async catcher and that player might not be on the same tick region of the ticking player

@jpenilla

Copy link
Copy Markdown
Member

This looks like it might work but it's not really the best solution. I'm working on a patch and will open a new PR soon.

@jpenilla jpenilla closed this Nov 18, 2026
jpenilla added a commit that referenced this pull request Nov 19, 2026
* Rework map holder tracking to avoid cross-region access

Vanilla updates every holder of a map from each holder's tick, reading
other players' inventories, positions and equipment, which in Folia may
be owned by other regions. Holders also remove each other, so frame
viewers are constantly dropped and re-added and receive the full map
again on every frame update.

A player is now a holder of a map while the map is in their inventory or
equipment, or while a frame showing it is tracking them, and only ever
updates their own holder entry:
- Carrying is detected from the map being ticked from the player's
  inventory/equipment; maps that were not ticked are removed at the end
  of the player's tick, which also replaces Paper's drop hook
- Frame viewers are subscribed when they start tracking the frame and
  are sent the map immediately; they stay subscribed until tracking
  stops or the frame's map changes, so they no longer get full resends
- Map invisibility equipment hides the player's marker per viewer, the
  wearer still sees themselves
- Players keep a set of their maps, so leaving the world no longer
  scans every loaded map

For a map with N carriers and V frame viewers, approximately:
- Carrier ticks: O(N^2) inventory scans per tick -> O(N)
- Frame updates: O(V * (N + V)) -> O(V), frame markers are updated once
  per frame instead of once per viewer
- World leave: O(loaded maps) lock acquisitions -> O(player's maps)
- Full map resends to frame viewers while the map is carried: up to one
  per viewer per frame update -> only on first subscription

Each map lock is now held for constant work per holder instead of a
loop over all holders, which greatly reduces contention between regions
sharing a map, and no player ever waits on a map lock to update another
player.

Reported in #538

* Don't copy player markers to locked maps

Player markers are live state maintained by each holder's tick, not
part of the map's content. Copying them when locking is inconsistent:
the copy is replaced as soon as that player holds the locked map, but
stays at their lock-time position if they never do, where it also
escapes map invisibility equipment and, once they are offline, vanish
(canSee) filtering.

Treat them as live state only, like on any other map. Frame and banner
markers are still copied with the rest of the map's content.

* Align map holder tracking with Paper PR

Match the code from PaperMC/Paper#14499, so that once it is merged only
the region threading parts remain in Folia:
- Use the Paper PR's layout, comments and marker names (as Folia markers
  until it is merged)
- Keep holders of spectators whose tick is skipped up to date
- getHoldingPlayer throws instead of creating a holder

* Restructure map changes as Paper feature patches

Apply PaperMC/Paper#14499 (rework map holder tracking) and
PaperMC/Paper#14500 (unload unused map data) as feature patches before
Region Threading Base:
- 0001: their file patch changes
- 0002, 0003: their feature patches, unmodified

Region Threading Base only adds the region threading changes on top, so
once the Paper PRs are merged the first three patches can be dropped.

Folia changes for unloading map data:
- Unload unused maps before Folia's periodic map save
- Remove SavedDataStorage#read, map data is now loaded through
  MapDataCache

* Update Paper map patches

Sync PaperMC/Paper#14500 review fixes:
- Call MapInitializeEvent for new maps after registering them, and
  register them atomically, so listeners and other threads see the
  new map instead of looking it up on disk
- Keep maps loaded again when a plugin removes a renderer
- Make the saved data cache concurrent on Paper too; Region Threading
  Base keeps a diff on that line
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.

2 participants