473,890 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What would be a faster alternative to boxing/unboxing?

I have a situation where an app writes data of various types (primitives and
objects) into a single dimensional array of objects. (This array eventually
becomes a row in a data table, but that's another story.) The data is
written once and then read many times. Each primitive read requires
unboxing. The data reads are critical to overall app performance. In the
hopes of improving performance, we have tried to find a way to avoid the
unboxing and casting. So far, nothing has worked well.

One solution we tried was to create our own simple object to wrap the
primitives. In this object we used a public field that allowed reading of
the primitive data value without a property or unboxing. However, overall
performance went down. We are still investigating, but our current
speculations are that:
1. the overhead of creating the object was significantly greater than the
overhead of boxing and this offset any gains we might have gotten on the
reads (a guess).
2. wrapping each primitive forced us to introduce a new virtual method call
in place of the boxing/casting. Maybe the overhead of this call is what
caused the performance to go down. (Another guess.)

BTW, performance went down 15%.

Does anyone have any ideas on how to implement the following:

A 1-dimension array (with normal indexing) containing heterogenous strongly
typed data that allows reading/writing without casting and without
boxing/unboxing.

All thoughts/suggestions are appreciated.


Nov 15 '05
43 6926
Thanks, Mountain, but that complexity is why we did not use it. - Frank
Let me know if you care to see any of it. I doubt it contains anything you
would find interesting. It's just a more complex version of what you already have -- and the complexity is stuff I might not even use now -- stuff like
the various wrappers and thread safe code.

Nov 15 '05 #41
Hmmm, I'm not convinced that we can make any real conclusions about the
performance of such an application as this one without having the parameters
nailed down a bit better. For example, I wrote another sample that used
multiple arrays like Frank wisely proposed and it was 80% faster than a
similar object[] sample I wrote. I guess with all of us writing our own
samples to test ideas, we're probably not making the same assumptions about
anything (the proportion of reference types to value types, the *size* of
the objects and value types etc.)

Since I'm so deep in this, MBG, could you post your object array sample that
show the baseline performance that you want to improve?
(Or not, if you have ideas your are happy with already)...

Richard
--
Veuillez m'excuser, mon Français est très pauvre. Cependant, si vous voyez
mauvais C #, c'est mon défaut!
"Mountain Bikn' Guy" <vc@attbi.com > wrote in message
news:mB7xb.1164 01$Dw6.539081@a ttbi_s02...
Hi 100,
See my comments inline.
Regards,
Mountain
To back up my point I did some timing of both techniques esterday. Unboxing
beats the other method off. It is indeed faster. Once again I'm talking
about reading. If we want performance for writing (storing) your method is definitely the winner and I never argue against it.
But as far as I remember Mountains issues were about the performance at
reading or maybe I'm wrong.


Yes, reading occurs more often than writing. The ratio is about 2:1.
So, if I were to implement such a storage I would use your method (or
something similar) if I want to make a storage for general use (since the performance is good for reading and writing) or I would go with unboxing

if
the performance for reading is critical and I don't care for writing

since,
for example, it is done only once (the impression I got from the first
Mountain's post).

If you are curious I can give you the test project.


Yes, I'm interested in taking a look at it. I appreciate you following up

on this in so much depth. Please email it to me.

the first part of my address is "Number_235 "
and the domain is yahoo dot com

Nov 15 '05 #42
Hi Richard,

I certainly would like to reach a conclusion on this, if for no other reason
than for the benefit of anyone reading this thread who may be interested in
general solutions that could improve upon boxing/unboxing when different
types are stored in a (hetereogeneous ) array. I am sending you an email with
more detail (I used the corrected version of <ch*****@yumspa myumYahoo.com>) .

I do not yet have a good solution. Here is the issue I'm thinking through at
the moment. In order to use Franks very nice suggestion, I have to create
yet another index (I think). We already have about 100MB of indexes on this
object[]. Right now, each data item is indexed 0 to N for an array of
object[N+1]. Let's say we use Frank's approach and there are just 3 types:
ints, doubles and a "myObj" type. There will be 3 "pools" as Frank called
them. However, the original index values will have mapped from 0 to N to
three new index sets: 0 to n1, 0 to n2, 0 to n3.

We already have some added complexity of needing a switch statement to
select the "pool". The addition of another index mapping scheme (on top of
100MB of existing indexes) has me discouraged at the moment. And 100's test
seemed to indicate that this solution may not yield strong performance
gains. So I'm brainstorming at the moment. Your test is encouraging and I
would like to explore this until we reach a definitive conclusion (and
hopefully the holiday interruption won't kill momentum).

Regards,
Mountain

"Richard A. Lowe" <ch*****@yumspa myumYahoo.com> wrote in message
news:eQ******** ******@TK2MSFTN GP12.phx.gbl...
Hmmm, I'm not convinced that we can make any real conclusions about the
performance of such an application as this one without having the parameters nailed down a bit better. For example, I wrote another sample that used
multiple arrays like Frank wisely proposed and it was 80% faster than a
similar object[] sample I wrote. I guess with all of us writing our own
samples to test ideas, we're probably not making the same assumptions about anything (the proportion of reference types to value types, the *size* of
the objects and value types etc.)

Since I'm so deep in this, MBG, could you post your object array sample that show the baseline performance that you want to improve?
(Or not, if you have ideas your are happy with already)...

Richard
--
Veuillez m'excuser, mon Français est très pauvre. Cependant, si vous voyez mauvais C #, c'est mon défaut!
"Mountain Bikn' Guy" <vc@attbi.com > wrote in message
news:mB7xb.1164 01$Dw6.539081@a ttbi_s02...
Hi 100,
See my comments inline.
Regards,
Mountain
To back up my point I did some timing of both techniques esterday. Unboxing
beats the other method off. It is indeed faster. Once again I'm talking about reading. If we want performance for writing (storing) your method is
definitely the winner and I never argue against it.
But as far as I remember Mountains issues were about the performance
at reading or maybe I'm wrong.
Yes, reading occurs more often than writing. The ratio is about 2:1.
So, if I were to implement such a storage I would use your method (or
something similar) if I want to make a storage for general use (since the performance is good for reading and writing) or I would go with

unboxing if
the performance for reading is critical and I don't care for writing

since,
for example, it is done only once (the impression I got from the first
Mountain's post).

If you are curious I can give you the test project.


Yes, I'm interested in taking a look at it. I appreciate you following

up on
this in so much depth. Please email it to me.

the first part of my address is "Number_235 "
and the domain is yahoo dot com


Nov 15 '05 #43
100
Hi Richard,
You are right.
There are a lot of factors that may lead to different results. I played a
bit more with my test program and I kind of give up of the conclusion I made
in my previous post. It seems like unbox method suffers
worse performance if a structure has reference type field(s) for example
(which may have some reasonable explanation, but I can't come up with any).
Anyway I'm not sure even of the correctness of the results I got.
So now I'm leaning against the method Frank suggests because even if in some
cases unboxing might be a bit faster the difference wouldn't be that big,
however writing operation using boxing, though, is big times slower.

Mountaing, stick with Frank's idea it is better than unboxing.

B\rgds
100

"Richard A. Lowe" <ch*****@yumspa myumYahoo.com> wrote in message
news:eQ******** ******@TK2MSFTN GP12.phx.gbl...
Hmmm, I'm not convinced that we can make any real conclusions about the
performance of such an application as this one without having the parameters nailed down a bit better. For example, I wrote another sample that used
multiple arrays like Frank wisely proposed and it was 80% faster than a
similar object[] sample I wrote. I guess with all of us writing our own
samples to test ideas, we're probably not making the same assumptions about anything (the proportion of reference types to value types, the *size* of
the objects and value types etc.)

Since I'm so deep in this, MBG, could you post your object array sample that show the baseline performance that you want to improve?
(Or not, if you have ideas your are happy with already)...

Richard
--
Veuillez m'excuser, mon Français est très pauvre. Cependant, si vous voyez mauvais C #, c'est mon défaut!
"Mountain Bikn' Guy" <vc@attbi.com > wrote in message
news:mB7xb.1164 01$Dw6.539081@a ttbi_s02...
Hi 100,
See my comments inline.
Regards,
Mountain
To back up my point I did some timing of both techniques esterday. Unboxing
beats the other method off. It is indeed faster. Once again I'm talking about reading. If we want performance for writing (storing) your method is
definitely the winner and I never argue against it.
But as far as I remember Mountains issues were about the performance
at reading or maybe I'm wrong.
Yes, reading occurs more often than writing. The ratio is about 2:1.
So, if I were to implement such a storage I would use your method (or
something similar) if I want to make a storage for general use (since the performance is good for reading and writing) or I would go with

unboxing if
the performance for reading is critical and I don't care for writing

since,
for example, it is done only once (the impression I got from the first
Mountain's post).

If you are curious I can give you the test project.


Yes, I'm interested in taking a look at it. I appreciate you following

up on
this in so much depth. Please email it to me.

the first part of my address is "Number_235 "
and the domain is yahoo dot com


Nov 15 '05 #44

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
2650
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When you pass a Value-Type to method call ,Boxing and Unboxing would happen,Consider the following snippet: int a=1355; myMethod(a); ......
94
5729
by: Peter Olcott | last post by:
How can I create an ArrayList in the older version of .NET that does not require the expensive Boxing and UnBoxing operations? In my case it will be an ArrayList of structures of ordinal types. Thanks.
161
7914
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my biggest problem with this process is that the terms "value type" and "reference type" mean something entirely different than what they mean on every other platform in every other language. Normally a value type is the actual data itself stored in...
29
2360
by: calvert4rent | last post by:
I need to some sort of data type that will hold a listing of ID's and their counts/frequency. As I do some processing I get an ID back which I need to store and keep an accurate count for how many times that ID is listed. For example I might get a listing of these numbers: 1,4,6,23,6,2,54,1,6,23,2,4,1,6 I will get those numbers one at a time and need to build something like 1,3
0
9814
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10801
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10903
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9616
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6034
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4659
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4257
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.