Click here to Skip to main content
15,905,229 members
Home / Discussions / C#
   

C#

 
AnswerRe: URL Regular Expression f... Pin
AhsanS28-Nov-08 20:36
AhsanS28-Nov-08 20:36 
GeneralRe: URL Regular Expression f... Pin
jas0n2328-Nov-08 20:40
jas0n2328-Nov-08 20:40 
AnswerRe: URL Regular Expression f... Pin
Guffa28-Nov-08 20:36
Guffa28-Nov-08 20:36 
AnswerRe: URL Regular Expression f... Pin
Garth J Lancaster28-Nov-08 23:49
professionalGarth J Lancaster28-Nov-08 23:49 
QuestionConverting byte[] to a stream Pin
darkzangel28-Nov-08 18:17
darkzangel28-Nov-08 18:17 
GeneralRe: Converting byte[] to a stream Pin
Luc Pattyn28-Nov-08 18:32
sitebuilderLuc Pattyn28-Nov-08 18:32 
GeneralRe: Converting byte[] to a stream Pin
darkzangel29-Nov-08 9:32
darkzangel29-Nov-08 9:32 
GeneralRe: Converting byte[] to a stream Pin
Luc Pattyn29-Nov-08 10:35
sitebuilderLuc Pattyn29-Nov-08 10:35 
Hi,

1.
your code contains two copy operations that add to the RAM footprint and the CPU load:
first you copy data from buffer to mem, then from mem to the final byte array.
(I checked with Reflector: MemoryStream.ToArray creates a whole new array and copies all the data)

2.
there is no need for a memory stream at all. You could:
- create a list of byte arrays;
- allocate a new byte[1024] array inside the while loop, and add these buffers to the a list;
- when that while loop finishes, add together all the sizes of the partial buffers, allocate a new byte[] of total size, then copy each partial buffer directly into the final buffer. You might use Array.Copy to do that, again inside a loop (I suggest a foreach).
Doing so eliminates half of the copy operations without costing more RAM footprint.

3.
Do you really need the possibly huge single byte array as a result? if not, you could forgo all copying. You might return the list of partial byte arrays, or an object that allows to enumerate the data. It all depends on the consumer of RecvAsBytes().

4.
If you really want the total byte array: if the data is originating from a web server, you may know the total size of the data beforehand; have a look at WebResponse.ContentLength; so you could allocate the final array at once, and fill it in your existing while loop.

5.
BTW: in that case you also can get a data stream directly (WebResponse.GetResponseStream). If your data is not coming from a WebResponse object, maybe you should consider modeling it after WebResponse.

6.
Your "fixed" statement and the GCHandle I suggested basically serve the same purpose; your solution is quite acceptable; the main difference is GCHandle does not require the "unsafe" switch, fixed does.

Cheers.

Luc Pattyn [Forum Guidelines] [My Articles]

Fixturized forever. Confused | :confused:


Questiontime-division multiplexing Pin
Member 391904928-Nov-08 7:26
Member 391904928-Nov-08 7:26 
AnswerRe: time-division multiplexing Pin
User 665828-Nov-08 11:05
User 665828-Nov-08 11:05 
AnswerRe: time-division multiplexing Pin
Guffa28-Nov-08 11:10
Guffa28-Nov-08 11:10 
AnswerRe: time-division multiplexing Pin
Nicholas Butler28-Nov-08 13:22
sitebuilderNicholas Butler28-Nov-08 13:22 
GeneralRe: time-division multiplexing Pin
Dave Kreskowiak28-Nov-08 15:36
mveDave Kreskowiak28-Nov-08 15:36 
GeneralRe: time-division multiplexing Pin
PIEBALDconsult28-Nov-08 16:11
mvePIEBALDconsult28-Nov-08 16:11 
QuestionCan different processes share memory? Pin
Member 391904928-Nov-08 7:25
Member 391904928-Nov-08 7:25 
AnswerRe: Can different processes share memory? Pin
J. Dunlap28-Nov-08 9:50
J. Dunlap28-Nov-08 9:50 
Questionc# source code and produces the source code in S-style(Banner Style) indentation Pin
sp198428-Nov-08 6:48
sp198428-Nov-08 6:48 
AnswerRe: c# source code and produces the source code in S-style(Banner Style) indentation Pin
EliottA28-Nov-08 6:57
EliottA28-Nov-08 6:57 
AnswerRe: c# source code and produces the source code in S-style(Banner Style) indentation Pin
Thomas Weller28-Nov-08 6:57
Thomas Weller28-Nov-08 6:57 
AnswerRe: c# source code and produces the source code in S-style(Banner Style) indentation Pin
Ashfield28-Nov-08 8:48
Ashfield28-Nov-08 8:48 
GeneralRe: c# source code and produces the source code in S-style(Banner Style) indentation Pin
Wendelius28-Nov-08 8:59
mentorWendelius28-Nov-08 8:59 
AnswerRe: c# source code and produces the source code in S-style(Banner Style) indentation Pin
Christian Graus28-Nov-08 16:15
protectorChristian Graus28-Nov-08 16:15 
QuestionKeyboard shortcuts Pin
nlowdon28-Nov-08 3:06
nlowdon28-Nov-08 3:06 
AnswerRe: Keyboard shortcuts Pin
Pedram Behroozi28-Nov-08 3:26
Pedram Behroozi28-Nov-08 3:26 
AnswerRe: Keyboard shortcuts Pin
Kevin McFarlane28-Nov-08 4:03
Kevin McFarlane28-Nov-08 4:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.