<commit_before>class Inline::Language::ObjectKeeper {
    has @!objects;
    has $!last_free = -1;

    method keep(Any:D $value) returns Int {
        my $index = $!last_free;
        if $index != -1 {
            $!last_free = @!objects.AT-POS($index);
            @!objects.ASSIGN-POS($index, $value);
            $index
        }
        else {
            @!objects.push($value);
            @!objects.end
        }
    }

    method get(Int $index) returns Any:D {
        @!objects[$index];
    }

    method free(Int $index --> Nil) {
        @!objects.ASSIGN-POS($index, $!last_free);
        $!last_free = $index;
    }
}
<commit_msg>Speed up ObjectKeeper by using lower level data structures<commit_after>class Inline::Language::ObjectKeeper {
    has IterationBuffer $!objects;
    has $!last_free;

    submethod BUILD() {
        $!objects := IterationBuffer.new;
        $!last_free := -1;
    }

    method push($value is raw) {
        my $objects := $!objects;
        $objects.push($value);
        $objects.elems - 1
    }

    method keep(Any:D $value is raw) returns Int {
        my $index := $!last_free;
        if $index == -1 {
            self.push($value)
        }
        else {
            my $objects := $!objects;
            $!last_free := $objects.AT-POS($index);
            $objects.BIND-POS($index, $value);
            $index
        }
    }

    method get(Int $index) returns Any:D {
        $!objects.AT-POS($index);
    }

    method free(Int $index --> Nil) {
        $!objects.BIND-POS($index, $!last_free);
        $!last_free := $index;
    }
}

{'fraction_non_alphanumeric': 0.15379786566227244, 'fraction_numerical': 0.003138731952291274, 'mean_word_length': 1.8566308243727598, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6942961', 'n_tokens_mistral': 531, 'n_tokens_neox': 516, 'n_words': 108}