Skip to content

(perf) SLOT optimisation #488

@MasonD

Description

@MasonD

I understand that looping through all of an inventory's slots and checking contains() is actually a decent solution considering the number of slots you can reasonably expect inventories to have, but I think we can still do even better. From testing, using a method like this on NumberRangeSet doesn't have any effect on performance when SLOT isn't specified, and can improve performance quite noticeably for inventories as small as double chests when a single small range of SLOTS is specified. This was just to see if the idea had merit, I can likely expand the top logic for multiple number ranges and not have to default back to contains() in such cases. I'll throw together a PR later so you can have a go at comparing performance, if you're interested.

    public void forEachSlot(int inventorySlotCount, IntConsumer consumer) {
        if (this.ranges.length == 1 && this.ranges[0].end() < Integer.MAX_VALUE) {
            int max = (int) Math.min(this.ranges[0].end(), inventorySlotCount - 1);
            int min = (int) this.ranges[0].start();
            
            for (int i = min; i <= max; i++) {
                consumer.accept(i);
            }
        } else {
            for (int i = 0; i < inventorySlotCount; i++) {
                if (contains(i)) {
                    consumer.accept(i);
                }
            }
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions