Archive for category C#

Error Handling Performance

Tags: , , ,

Having just concluded the series on handling errors without using exceptions, now’s a good time to check up on an assertion I made in the first part: exceptions are slow. A good question to ask is “compared to what?” So let’s put them up against good old error codes and the new Either type I presented in the series. Which is fastest when there is no error? Which is fastest when there is an error? Read on to find out!

Read the rest of this article »

No Comments

Handling Errors Without Exceptions: Part 2

Tags: , , , ,

Last week’s article introduced the Either class as an alternative to exceptions that makes it easy for functions to declare their error results in addition to their success results and for callers of those functions to handle both results. Today we’ll go further by linking together multiple functions to handle all the error cases almost transparently. Read on to learn how to make the most out of Either!

Read the rest of this article »

No Comments

Handling Errors Without Exceptions: Part 1

Tags: , , ,

Exceptions are the de facto way to handle errors in C#, but they have problems. Callers don’t know if the function they’re calling will throw an exception at all or which types of exceptions it’ll throw. Exceptions also introduce an alternative control flow that’s often hard for programmers to follow. They make our code slower too, even when never thrown! Today’s article introduces an alternative to exceptions to help solve all of these issues. Read on to learn a new way to handle errors!

Read the rest of this article »

2 Comments

Event Performance: C# vs. UnityEvent

Tags: , ,

Unity programmers have their choice of two kinds of events. We could use the built-in C# event keyword or Unity’s UnityEvent classes. Which is faster? Which one creates more garbage? Today’s article finds out!

Read the rest of this article »

45 Comments

Moving Methods out of Classes

Tags: , ,

In today’s article I’ll share a technique that can help you reason about your classes (and structs). The core idea is to move some methods out of the class into helper functions. Doing this can really simplify the class and simplify the functions so they’re much more easily understood by readers (including yourself!), more easily written, and more easily extended. Read on to learn more about this technique!

Read the rest of this article »

No Comments

Adding Unions to C#

Tags: ,

C and C++ have a great feature call the “union”. It’s like a struct except it only has one of the fields at a time. C# lacks this feature, but with some trickery it can be added in. Today’s article shows how to do that!

Read the rest of this article »

6 Comments

File I/O Performance Tips

Tags: , , , , , ,

At some point, every project ends up reading or writing to the file system. If you do anything more than storing a single blob of bytes (e.g. JSON text) then you’ll need to be very careful about performance. It’s easy to accidentally write code that takes way longer to read and write than it should and you won’t get any help from the compiler or from Unity. Today’s article reveals some of these traps so you won’t fall into them!

Read the rest of this article »

13 Comments

When Structs Create Garbage

C# has a huge advantage over languages like Java and AS3: the struct. It allows us to create a value type with multiple members and avoid creating garbage every time we create one with new. At least that’s the simple version of the story. In reality, it’s really easy to accidentally create garbage with struct. Today’s article shows how to use struct so that you don’t create any garbage and reveals some of the traps so you won’t fall into them.

Read the rest of this article »

2 Comments

More JSON Performance Benchmarks

Tags: , , ,

Last week’s article benchmarked Unity 5.3’s new JsonUtility class against third-party alternatives LitJSON and Json.NET. JsonUtility came out the clear winner, but the question arose about how JsonUtility would fare with bigger or more complex JSON structures. Today’s article answers that question by benchmarking with more types of JSON documents to find out if JsonUtility can maintain its lead.

Read the rest of this article »

3 Comments

JSON Performance Benchmarks

Tags: , ,

Unity 5.3 came out this week and introduced a new, built-in JSON serializer library. Today’s article compares its performance against some popular third-party JSON serializer libraries to see if Unity’s version is any faster. Read on for the results!

Read the rest of this article »

8 Comments