Skip to content
This repository was archived by the owner on Oct 14, 2025. It is now read-only.

feat(engine): entitylist count performance for scalebyplayercount fn#979

Merged
Pazaz merged 3 commits into2004Scape:mainfrom
ultraviolet-jordan:fix/entitylistcount
Nov 4, 2024
Merged

feat(engine): entitylist count performance for scalebyplayercount fn#979
Pazaz merged 3 commits into2004Scape:mainfrom
ultraviolet-jordan:fix/entitylistcount

Conversation

@ultraviolet-jordan
Copy link
Copy Markdown
Contributor

scaleByPlayerCount was created which calls into getTotalPlayers to get the current number of players in the World. The way getTotalPlayers is implemented is by manually counting the slots every time:

    get count(): number {
        let count: number = 0;
        for (const _ of this[Symbol.iterator]()) {
            count++;
        }
        return count;
    }

which the [Symbol.iterator] is a loop itself:

    *[Symbol.iterator](): ArrayIterator<T> {
        for (const index of this.ids) {
            if (index === -1) {
                continue;
            }
            const entity: T | undefined = this[index];
            if (typeof entity === 'undefined') {
                continue;
            }
            yield entity;
        }
    }

Since scaleByPlayerCount fn in World is used by removeNpc and removeObj, it is performing a double loop every time an npc or obj is removed from the world.

Refactor the count fn as so:

    get count(): number {
        return Math.max(this.length - this.free.size, 0);
    }

::addobj2 64 adds an obj to every tile in a 64x64 square area, effectively adding and then removing 4096 objs.
image

The screenshots below are before & after when the 4096 objs despawn and shows the time taken.

Before:

image

After:

image


I also added some extra info into the world cycle messages & refactored some other bits of code to simplify them.

@ultraviolet-jordan ultraviolet-jordan changed the title feat: entitylist count performance for scalebyplayercount fn feat(engine): entitylist count performance for scalebyplayercount fn Nov 3, 2024
@Pazaz Pazaz merged commit 26828fa into 2004Scape:main Nov 4, 2024
Pazaz added a commit that referenced this pull request Nov 11, 2024
## Content Changes

### Features

* #973
* #985

### Fixes

* #966
* #969
* #971
* #981
* Smithing daggers level-up typo
* Item spawns update

## Engine Changes

### Features

* #972
* #979
* #986

### Fixes

* #975
* #977
* #978 - movement bug
* #983
* #987
* Don't send UNSET_MAP_FLAG when standing still
* Soundscape song crash
* Authentic Loc category config code
* Authentic Obj tradeable config code
* InvType safety in player save
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants