Skip to content

Instantly share code, notes, and snippets.

@smcl
Created December 18, 2016 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smcl/3d4785c86720b6643b60367a36b2203f to your computer and use it in GitHub Desktop.
Save smcl/3d4785c86720b6643b60367a36b2203f to your computer and use it in GitHub Desktop.
weird snippet i saw in ironpython - they pre-allocate an cache of 1100 ints-as-objects in an array (in range -100..+10000) and avoid a number of int->object cast by returning the cached version. weird
/// <summary>
/// Gets a singleton boxed value for the given integer if possible, otherwise boxes the integer.
/// </summary>
/// <param name="value">The value to box.</param>
/// <returns>The boxed value.</returns>
public static object Int32ToObject(Int32 value) {
// caches improves pystone by ~5-10% on MS .Net 1.1, this is a very integer intense app
// TODO: investigate if this still helps perf. There's evidence that it's harmful on
// .NET 3.5 and 4.0
if (value < MAX_CACHE && value >= MIN_CACHE) {
return cache[value - MIN_CACHE];
}
return (object)value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment