✅ [[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
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@Ero here is an example, if I used
Rent(5)
with just name
it works , if I added everything else it raises an errorThat's hardly an examplr
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
tebeco#0205
REPL Result: Success
Result: int
Compile: 302.978ms | Execution: 22.345ms | React with ❌ to remove this embed.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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 charactersWhat 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:
That said, you can add everything together to see how much bytes you need.
You don't need to worry about the +
and *
, C# will optimize these in release mode to 16
.sorry I accidentally deleted it -.-
@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 requestedIf that is the case, then why it doesn't work when I add lower value than 20 ??
What's the problem with that?
What do you mean with "it doesn't work"?
it raises and error in the Try and catch method
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
the program shuts down
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
doesnt finish the request
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
@TeBeConsulting Where ?
if I add more buffer , it works , minimum 18 I think
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
its being incrementing inside
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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.@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•2y ago
Message Not Public
Sign In & Join Server To View
but arent we defining the Minimum length not the maximum? why do I need to increase the minimum length ?
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.
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
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?
should I not include the rent part of the code and let the program ?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
tebeco#0205
REPL Result: Success
Result: int
Compile: 278.301ms | Execution: 21.153ms | React with ❌ to remove this embed.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
@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•2y ago
Message Not Public
Sign In & Join Server To View
there is no repro, its private contract.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
fair
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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 ?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•2y ago
Message Not Public
Sign In & Join Server To View
If your array isn't large enough for the amount of data you're trying to send, it makes sense it throws.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
oh , but I thought we are defining the minimum not the maximum value of the array, isnt that right ?
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]
maybe that is the issue, the current array that im renting from isnt big enough maybe.
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.
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•2y ago
Message Not Public
Sign In & Join Server To View
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 simpleIt 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.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 trueThat's why the parameter name is called "minimumLength". Not "length" 😜
true 😄