The bug
Every time somebody goes through a nether portal or an entity is sent to another dimension the whole server freezes for 1-2 seconds. The search algorithm for portals seems to be really inefficient.
How to reproduce
Video demonstrating the issue: https://gfycat.com/FriendlyInfiniteGreyhounddog
Related issues
is duplicated by
Attachments
Comments


casually going with my llama caravan trough a portal from the nether to the overworld already lags my singleplayer pretty hard 😞

Could be partwise caused by MC-116279. Is there a portal in the overworld at the corresponding position?
Additionally the fact that portal entries are removed from the cached portal position map after not being used for 300 ticks (if world time % 100 == 0
) is probably not beneficial at all.

There was a portal on the overworld side. We notice it all the time during normal gameplay. Somebody goes through a portal -> lag spike for 1-2 seconds.

This is a really terrible performance issue. I really hope there will be a fix soon.
If I understand it correctly the main issue is when going from the nether to the overworld.
1) The caching of portal positions that are done uses the destination coordinates as key for the cache. But since the floating point values from the nether position is multiplied with 8, the resulting destination may be 8 different value in the overworld from each portal block in the nether. The cache values are deleted after 300-400 ticks if not reused. My suggestion: the cache key needs to be based on the portal block that is entered rather than the destination. Increase cache time by a factor of 2 or 3, or only update the cache when portals are created/destroyed.
2) When there is no cache hit, a full search is performed from -128 to +128 on both x and z axis and the full height of the world. That is a lot of blocks to scan for a portal. My suggestion is to improve the scanning by searching in order of distance to the center point. Once a portal block has been found, the search can be terminated and thus not all blocks needs to be scanned.
If you look into the code via MCP, the relevant class is net.minecraft.world.Teleporter and methods placeInExistingPortal() and removeStalePortalLocations().

Nicklaus Ekstrand is right, I think. But there is my solution:
1) Cache the coordinates of destination portal as long as chunk is loaded. This will already resolve laggy portal issues.
2) Search for nether portal blocks in order of distance. This can be made by first looking in chunk where estimated point is and loading more chunks if needed. This will lower initial lag.
3) Clear cache of neighbor portals at destination dimension when new portal is created/destroyed at source dimension. This will resolve leftover issues.

Traveling through portals overall isn't optimized and it is very laggy / resource hungry sometimes, especially in multiplayer servers.
The whole server notices clearly when any entity teleports through portal, even if the chunks are pre-generated and saved (you've traveled many times before and loaded the terrain around)
This is very big and annoying "leak" / issue, in fact.
I think "some sort of" solution to this can be saving the portals info (The Nether Portal's coordinates to the current chunk/s where the Overworld Portal is and the same thing on the other side). This may be tricky, but it can be twisted / modified in some way so that the bug / issue can be fixed more-reliably.

Suggested fix: Store output xyz of nether portal in blocknbt data - like end-gateways. If exit portal block is broken - search for new exit. Lagfriendly - search only for one block most of the time. Also useful for mapmakers and serveradmins - setting output xyz far away with commandblocks.

Does this still affect the current versions of Minecraft? (1.12.2, 1.13-pre3). If so, please update the ticket accordingly.

Confirmed for 1.13.1.
Can confirm for 18w44a with even more severe lag than for stable versions.
Â
Can confirm for 18w45a too. The server can take several seconds before finding a linked portal, although not sure if it is the cause of global lag or if the lag is caused by the constant busy-waiting CPU load as described in https://bugs.mojang.com/browse/MC-138071.
Still an issue for 18w46a and for 1.14 snapshot this might be related to https://bugs.mojang.com/browse/MC-138114.
Also when mobs are going through any Nether portal it causes https://bugs.mojang.com/browse/MC-138919.

Can confirm for 18w47a
This issue does not occur when the chunk on the other side of the portal is marked for force loading (and after having actually loaded the force loaded chunk once). Although forceloading is not a solution, but it helps to understand what is going on.
Can confirm for 1.14-pre2.
Here are simple steps on how to reproduce:
Make a superflat world
Create a Nether portal
Go to the Nether, let the chunks generate
Go back, and wait for 1 minute
Now try to go through this portal
You will notice the tremendous amount of lag, which is only caused by the portal. If I tp to the Nether using /execute in the_nether command, I don't have this issue.
I followed those steps on a 8x3.5Ghz with 16Gb allocated and made a debug profiling to track the cause of the lag: I joined the whole report but here are the highlights:// This is approximately 9.43 ticks per second. It should be 20 ticks per second [00] tick - 58.99%/58.99% [01] | connection - 91.24%/53.85% [02] | | entityBaseTick - 99.59%/53.63% [03] | | | portal - 99.97%/53.62% [04] | | | | placing - 99.91%/53.57% […] [00] nextTickWait - 41.01%/41.01%
So this means I took some idling time to start/stop the profiler (nextTickWait), and the rest of the time I was stuck in the loading terrain screen. The main consequence of this issue is one player stuck a portal transition will make the server stops ticking for any other player who is playing on the same world at the same time.
Can confirm for 1.14-pre3.

I was told my ticket (https://bugs.mojang.com/browse/MC-148438) was a duplicate of this one that was created all the way back with version 1.11.2
I play on a lot of servers, this problem is FAR worse in 1.14 snapshots and pre-releases. That's why it took me this long to report it, as I'm used to basic lag when people switch dimensions using a portal. It reduces the TPS quite a bit, but only for 1-2 seconds.
Now in 1.14, it not only drops the TPS, but stops it out right. Now, once a player finishes switching dimensions, everyone else on the server will revert to 2-3 seconds ago, full on rubber banding. Blocks all get replaced, and players return to the position they were at when the player entering the portal first started switching dimensions. I run a lot of servers, I've never seen anything this bad before.
Can confirm for 1.14-pre4.
[18:40:42] [Server thread/WARN]: Something's taking too long! 'root.tick.connection.entityBaseTick.portal.placing'
Can confirm for 1.14-pre5.
The fix to Nether portals only fixes the transportation of several entities at the same time with the same portal, but a random transportation can still freeze the server for times from 2 to 8 seconds.
The same profiling as described above still applies.

Video covering this topic:

Possible solution: adding blockdata of exit coords to portal blocks. Entering portal block without exit coords or when exit coords are no longer valid (destroyed portal) will cause searching for new exit location. This way only portal changing will cause lags.
Â
Another solution: Keeping nether portal block coords similar to POI of villagers - in separate structure. This way only those files need to be checked and searching would be sped up significantly.
Can confirm for 1.14.

In 1.14, this only appears to happen when entities are going from the nether to the overworld, not the other way around...
@unknown That's probably because the height limit in the Nether is twice less as the Overworld, so the game has to check twice as less blocks for finding portal position. But the lag is present in both ways at a different scale.

I've been kicked from Realms going both ways through nether portals and it happens around 40-50% of the time.
Can confirm for 1.14.1-pre1.
Can confirm for 1.14.1-pre2.

Confirmed for 1.14.1

I can confirm this as well, for 1.14.1. It's bad enough where the lag will cause the server to completely kick everybody until I restart it.Â

Can confirm for 1.14.1. Like someone else said, it's sometimes so bad that the server crashes, with a best case scenario being 5 full seconds of complete suspension of ticks processing. It's gotten to the point where we've written a datapack to automatically warn everyone on the server when someone walks into a portal.
Worth noting that end portals work almost instantly. It's only nether portals that have this lag.

Not sure if related but we did have a few instances of "extra" portals being generated in the nether. IE a player would go through a existing portal from the over world and a new portal would be generated a block or two away from the existing lit portal. It has happened multiple times with different portals.Â

Able to replicated in 1.14.1
This issue is still present in 1.14.2 pre-release 3, but the time the server stays frozen during portal finding has lowered compared to 1.14.1. Typical duration is now 2-4 seconds per tick. On same host, it was 4-8 seconds before.

Was able to replicate the bug for 1.14.2 pre-release 3, same issues still occurs but slightly less effective spikes

Issue is still occurring in public release of 1.14.2Â

Can confirm on 1.14.2.

Is there a reason this is still unassigned? This is a huge issue for servers. Entering the nether or exiting causes huge lag spikes – upwards of 10 full seconds for my server running on an overclocked 7700K.Â
@unknown This is a more complex issue that couldn't be solved by just assigning someone and starting working on an afternoon. Issue is known but involves more than just dealing with Nether portals themselves.
Progress on this issue has already been made on 1.14.2 pre-releases where the cost for loading already generated chunks has been significantly decreased.

Can confirm on 1.14.3. May be somehow related to MC-149705 ?

Still happens in 1.14.4 except now it permanently deadlocks the entire world in singleplayer and only a force-close can end it.

Confirmed vanilla 1.14.4
[media]
Our server has had constant 1-3 second freezes constantly and didn't understand until I tracked it to this issue, it occurs Very often because of zombie pigmen wandering in and out of portals.

Isn't there the possibility to keep a global (or region file based) record of portal blocks? Then, portals only need to check for the positions in that list instead of searching through millions of blocks. Portals are pretty rare blocks, so that list doesn't even need to be that large. That should be way more efficient than searching for essentially a needle in a haystack.
(I understand that it would be very hard to "upgrade" existing worlds to this feature, but I guess it could improve portal performance by a lot.)

As of 1.14.x, this issue appears to have two distinct causes: (1) Slow search algorithm for portals, and (2) Slow loading of Nether chunks. Fixing both causes is needed to restore good performance.

In 1.14 I can´t cross de nether portal because the "Loading Terrain" screen is infnite

same thing going on 1.16.1

@Max, please make a new bugreport for your problem with portals, as this bug was resolved almost a year ago and I can still confirm it being fixed. Also try to add your bug reproduction steps if you can.