#weird ass algorithm with zig

1 messages · Page 1 of 1 (latest)

keen owl
spare spade
#

I see an alloc and a nested loop. I don't really know what the question is (was) asking

keen owl
#

oh just this

spare spade
#

are you asking us to debug your code for you, without even knowing if there's a bug?

keen owl
#

basically I have to fill an array with shards every packet is chunked

#

that's the thing

spare spade
#

maybe someone familiar with the algorithm will have more useful feedback. I just don't really get the specific question and problem you're running into. it seems a bit too abstract. like. yeah, that's some init code.

keen owl
#

okay it's like dividing an array into chunks

#

[a,b,c,d,e,f,g] with a concurrency of 3, [[a,b,c],[d,e,f],[g]]

#

then it has some logic to create the ID of the shard

spare spade
#

can you post the text instead of a screenshot? that way I don't have to retype lines

#

and there's a line I think needs correction

keen owl
#

hold on

glossy forum
keen owl
#
/// spawn buckets in order
/// https://discord.com/developers/docs/events/gateway#sharding-max-concurrency
fn spawnBuckets(self: *Self) [][]Shard {
    const concurrency = self.options.info.session_start_limit.?.max_concurrency;

    const range = self.shard_start - self.shard_end;
    const bucket_count = (range + concurrency - 1) / concurrency;

    self.logif("#0 preparing buckets", .{});

    const buckets = try self.allocator.alloc([][]Shard, bucket_count);

    for (buckets, 0..) |bucket, index| {
        const bucket_size = if ((index + 1) * concurrency > range) range - (index * concurrency) else concurrency;

        bucket.* = try self.allocator.alloc([]Shard, bucket_size);

        for (bucket, 0..) |shard, i| {
            shard.* = try self.create(self.shard_start + index * concurrency + i);
        }
    }

    self.logif("{d} buckets created", .{bucket_count});

    return buckets;
}
spare spade
#

thx

keen owl
glossy forum
#

Kinda smells like the "gather/scatter" approach

keen owl
#

basically that

spare spade
#

I am wondering if this line:

const buckets = try self.allocator.alloc([][]Shard, bucket_count);

is allocating [][][]Shard and not [][]Shard?
Is that what you're intending?

keen owl
#

I think that's wrong

spare spade
#

cause allocating a string is try allocator.alloc(u8, string_len);, not try allocator.alloc([]u8, string_len);

keen owl
#

bucket[first index][second index]

#

should be alright

glossy forum
#

You'll want |*bucket| in order to get a pointer

spare spade
#

that seems more like what I'd expect. but I'm not digging deep into the API or expected data set, so maybe I'm wrong

glossy forum
#

No that sounds right

glossy forum
#

Right

#

How many shards do you expect to have here?

keen owl
#

depends on the guild count of the bot

#

I believe every shard may handle like 1000 guilds

#

it's really not that exact

glossy forum
#

Not sure what sort of concurrency you're doing here, but -- Generally, you want to have fairly large partitions, and then give each one their own entirely distinct blob of data which they work on (by which I mean, a separate allocation, or more than 64 bytes in memory away from another thread's blob), only to then recombine at the end once the thread is done with it.
That way you minimize contention, which is the single most important thing with getting all the perf from them.

#

Allocating each 'row' separately as you're doing there works fine, but is inefficient if the bucket size is small, or you have a lot of buckets.

keen owl
glossy forum
#

(If the bucket size is small because it means you'll have many more things being handed between threads; i.e contention)
(And if you have a lot of buckets, because you have to free each one individually - or use an arena)

keen owl
#

so what do I do

#

looks inefficient

#

this startup process usually takes ages