C
C#2y ago
zylog

✅ [[QUESTION]] HOW TO CALCULATE THE Rent(int) property for System.Buffers.ArrayPool<byte> ?

Hey, I have a question about System.Buffers.ArrayPool<byte> I tried to read the document many times but I didn't get it. what is the Rent(int) property of System.Buffers.ArrayPool<byte> ? How do I know how many bytes I need for the buffer? If I need a string of 5 characters, Rent(5) is enough ... I know that and it works. but what if I need : 1 - String(5 character) 2 - bool 3 - Int(1 digit ) (1-5) 4 - Int(1 digit ) (1-5) 5 - Int(1 digit ) (1-5) 6 - Int(1 digit ) (1-5) How do I calculate the Rent value I need?
54 Replies
ero
ero2y ago
5 bytes is not enough for a string of 5 characters sizeof(bool), sizeof(int) for the others But this seems like an incorrect use of ArrayPool It'd be better if you just told is what you're doing
zylog
zylogOP2y ago
@Ero here is an example, if I used Rent(5) with just name it works , if I added everything else it raises an error
ero
ero2y ago
That's hardly an examplr
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
tebeco#0205
REPL Result: Success
sizeof(char)
sizeof(char)
Result: int
2
2
Compile: 302.978ms | Execution: 22.345ms | React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
gerard
gerard2y ago
The parameter of Rent(int) is the minimal size that the array should have. So note that the array can be larger than requested
How do I calculate the Rent value I need?
By adding the lengths together.
5 bytes is not enough for a string of 5 characters
What they are trying to say that string.Length doesn't equal to the amount of bytes it requires. For example, a string with an emoji ("🤔") is 4 bytes. You can get the amount of bytes you need with:
Encoding.UTF8.GetByteCount("🤔")
Encoding.UTF8.GetByteCount("🤔")
That said, you can add everything together to see how much bytes you need.
var size = Encoding.UTF8.GetByteCount(input) + sizeof(bool) + sizeof(int) * 4; // or sizeof(byte), since they are 1-5
var size = Encoding.UTF8.GetByteCount(input) + sizeof(bool) + sizeof(int) * 4; // or sizeof(byte), since they are 1-5
You don't need to worry about the + and *, C# will optimize these in release mode to 16.
zylog
zylogOP2y ago
sorry I accidentally deleted it -.-
public public ArrayPool<byte> _sendBuffers;
public string someName;
public bool someBool;
public int int1;
public int int2;
public int int3;
public int int4;

int pos = 0;
byte[] sendBuffer = _sendBuffers.Rent(50);

sendBuffer.WriteString(ref pos, someName);
sendBuffer.WriteBool(ref pos, someBool);
sendBuffer.WriteInt(ref pos, int1);
sendBuffer.WriteInt(ref pos, int2);
sendBuffer.WriteInt(ref pos, int3);
sendBuffer.WriteInt(ref pos, int4);
Program.comms.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
public public ArrayPool<byte> _sendBuffers;
public string someName;
public bool someBool;
public int int1;
public int int2;
public int int3;
public int int4;

int pos = 0;
byte[] sendBuffer = _sendBuffers.Rent(50);

sendBuffer.WriteString(ref pos, someName);
sendBuffer.WriteBool(ref pos, someBool);
sendBuffer.WriteInt(ref pos, int1);
sendBuffer.WriteInt(ref pos, int2);
sendBuffer.WriteInt(ref pos, int3);
sendBuffer.WriteInt(ref pos, int4);
Program.comms.ServerSend(clientId, 0, new ArraySegment<byte>(sendBuffer, 0, pos));
@Ero @TeBeConsulting here is an Example that is the problem
The parameter of Rent(int) is the minimal size that the array should have. So note that the array can be larger than requested
If that is the case, then why it doesn't work when I add lower value than 20 ??
gerard
gerard2y ago
What's the problem with that? What do you mean with "it doesn't work"?
zylog
zylogOP2y ago
it raises and error in the Try and catch method
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
zylog
zylogOP2y ago
the program shuts down
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
zylog
zylogOP2y ago
doesnt finish the request
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
zylog
zylogOP2y ago
@TeBeConsulting Where ? if I add more buffer , it works , minimum 18 I think
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
zylog
zylogOP2y ago
its being incrementing inside
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
gerard
gerard2y ago
Oh you mean that the array isn't large enough for the data. That's what you mean with Rent(20) doesn't work, which makes sense. Again, you can calculate the length you need to rent. What's also common is that when the array isn't large enough, you return the array and make a larger one. But that's on you how you want to solve that.
zylog
zylogOP2y ago
@TeBeConsulting its very hard to share and run the code, because its a large communication between clients and server that goes back and furth multiple time from the same request but it breaks at that point
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
zylog
zylogOP2y ago
but arent we defining the Minimum length not the maximum? why do I need to increase the minimum length ?
gerard
gerard2y ago
Well yes, it's a minimum length. But if you don't know what the string holds and you're guessing the length then you're asking for problems.
zylog
zylogOP2y ago
true , I understand I cant share the entire server just to debug that, no one has the time , Im just fishing for what could be the common causes
gerard
gerard2y ago
I don't understand what the problem is, you don't want to calculate the minimal length and want to put a constant value in? What's wrong with pre-calculating the array length you want to rent?
zylog
zylogOP2y ago
should I not include the rent part of the code and let the program ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
tebeco#0205
REPL Result: Success
sizeof(char)
sizeof(char)
Result: int
2
2
Compile: 278.301ms | Execution: 21.153ms | React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
gerard
gerard2y ago
True, we don't know what WriteString does. It can write UTF-16 bytes (then it's 6 bytes) or UTF-8 (then it's 3 bytes).
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
zylog
zylogOP2y ago
@gerard im just updating a program that isnt mine the original code had only string and it was Rent(5) == > that was working very well. but not I've added a bool and 4 other Integers ( to be converted to Enum on the client side later ) I couldnt leave Rent(5) as its because of the error , and when I raise it above 18 (aka: Rent(18) ) it works what could be the issue ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
zylog
zylogOP2y ago
there is no repro, its private contract.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
zylog
zylogOP2y ago
fair
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
zylog
zylogOP2y ago
I think adding a bigger minimum value for Rent() should be enough hint to the problem , maybe there is buffer hanging somewhere that hasnt returned yet ?
gerard
gerard2y ago
No clue, without code it's hard to say if the buffer hasn't returned. But that has nothing to do with the minimal length
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
gerard
gerard2y ago
If your array isn't large enough for the amount of data you're trying to send, it makes sense it throws.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
zylog
zylogOP2y ago
oh , but I thought we are defining the minimum not the maximum value of the array, isnt that right ?
gerard
gerard2y ago
An array is a fixed length. I don't think you understand how the array pool works. .Rent(16) will always be a length of 16 or higher. So for example: byte[16] but it can also return byte[32]
zylog
zylogOP2y ago
maybe that is the issue, the current array that im renting from isnt big enough maybe.
gerard
gerard2y ago
Because if the array pool would return the exact length for every array (e.g. 16, 17, 18), the pool would have to create a lot of arrays that matches that length. So it makes sense it gets something that's minimal that amount you're trying to get.
zylog
zylogOP2y ago
I think I get what u meant. I dont wanna bother u any longer , let me do few debugs and try to make sense of what exactly in the buffer and how long is it and I'd return with an update if i figured it out , but you said kinda make sense but also I feel I need to learn how pool array works in depth @gerard Thanks a lot 🙂 @TeBeConsulting Thanks 🙂 I'll return with an update if I got anything useful oh shit I got it ... Rent() has fixed values , and @TeBeConsulting @gerard was right , its just not large enough for the data I was trying to send but the fixed value part was the surprise for me Example Rent(1), Rent(2), Rent(3), Rent(4), Rent(5), ... Rent(16) all have the same array length (16) and Rent(17), Rent(18), Rent(19), Rent(20), Rent(21), ... Rent(32) all have the same array length (32) and Rent(33), Rent(34), Rent(35), Rent(36), Rent(37), ... Rent(64) all have the same array length (64)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
zylog
zylogOP2y ago
yeah , so my data needed 30+ so I lied in the middle Rent(32) so basically, Rent(17-32) are the same .. they should have made it that simple
gerard
gerard2y ago
It doesn't have to tho. If you rent a lot of .Rent(128) and return them to the pool, it's possible that .Rent(32) will now also return a byte[128] (since all of the 32-length arrays have been disposed). There is a limit on how much the pool holds.
zylog
zylogOP2y ago
I see, so we got to be aware of the length of the array with send and make sure that we return it otherwise, we would have some performance impact. yeah that make sense now :D, thanks a lot 🙂 but I think they should have made it more like Rent(16) , Rent(32) , Rent(64) ... etc instead of Rent(int) >>> this sends the wrong message that we could send an array of length 14 or 15 which is not true
gerard
gerard2y ago
That's why the parameter name is called "minimumLength". Not "length" 😜
zylog
zylogOP2y ago
true 😄
Want results from more Discord servers?
Add your server