#weird ass algorithm with zig
1 messages · Page 1 of 1 (latest)
I see an alloc and a nested loop. I don't really know what the question is (was) asking
oh just this
are you asking us to debug your code for you, without even knowing if there's a bug?
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.
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
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
hold on
I believe the keyword is "partition".
/// 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;
}
thx
Kinda smells like the "gather/scatter" approach
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?
I think that's wrong
cause allocating a string is try allocator.alloc(u8, string_len);, not try allocator.alloc([]u8, string_len);
oh crap
OK what about now
😭
You'll want |*bucket| in order to get a pointer
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
No that sounds right
like thiz
depends on the guild count of the bot

I believe every shard may handle like 1000 guilds
it's really not that exact
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.
it is everything explained here https://discord.com/developers/docs/events/gateway#sharding-max-concurrency
(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)
