Saturday, 30 August 2014

Improve LINQ to SQL queries

While developing the desktop windows client here in LiveBox, I was involved in a problem: how can I improve the performance in linq query ?

In this post, I'd like to recommend you 3 easy tips, that will improve your queries at least 5 times.

Use Compiled Queries


Because LINQ queries are interpreted at runtime and only while using these, this operation could be really hard if in a loop. So, a choice that you can apply is to create CompiledQueries that will prevent the runtime interpretation of the queries. But pay attention, it can be made only on non-mutable data set. 

Step 1: create a static class containing the query


public static class MyCompiledQuery
{
    public static Func<DataContext, string, IQueryable<MyEntity>>
    getEntities = CompiledQuery.Compile((DataContext db, string str)
         => from obj in db.GetTable< MyEntity >()
            where obj.Name == str
            select obj);
}


Step 2: use it 


IQueryable< MyEntity > objEntities = MyCompiledQuery.getEntities(objContext, theName);
foreach (MyEntity obj in objEntities)
{
    Console.WriteLine("Halo {0}", obj.Name);
}

Pretty easy, ah ?

Cache the Mapping Source


When executing large LINQ to SQL queries (those involving many tables), LINQ to SQL's object relational mapper incurs a significant overhead in building the internal metamodel. The metamodel describes how your entity classes map to the underlying tables and columns, and is built automatically via reflection (assuming you're using attributes rather than an XML file, to map tables and columns). Ordinarily, this overhead is incurred every time you instantiate a DataContext, which is not great for performance. You can avoid this by manually specifying a single static MappingSource object for all typed DataContext instances as follows:

public class MyDataContext : DataContext
{
   static MappingSource _sharedMappingSource = new AttributeMappingSource();

   public MyDataContext (IDbConnection cx) : base (cx, _sharedMappingSource) { }
   public MyDataContext (string cxString) : base (cxString, _sharedMappingSource) { }
}


Create indexes where needed


Indexes are really important in speeding your queries. And, besides, is really easy.

CREATE INDEX UX_MyIndex ON dbo.MyTable.MyColumn

In .NET there are different indexes that you can create:

  1. Simple index (showed above)
  2. Full-text index
A Full-text index will help you when, assume, you've VARCHAR(Max) text column containing tons of words. A like query with this kind of index will help you a lot, in terms of performance.

Friday, 29 August 2014

Good C and C++ Books

After writing the Good Algorithms Books post, I really feel that I need to write something about my favorite programming language: C/C++.

Well, learn this languages is really hard. First of all, IMHO, learn C++ does not require you to learn the C language before, and, of course, vice versa.

About C




The book that I grew up with is The C Programming Language (sometimes referred to as K&R), from the author of C,  Ritchie & Kernighan. It's a really awesome book. Clear and easy to read. Less than three hundred pages.

Another good C book that I'm love with, is Expert C Programming.



This book is fantastic because it will bring your attention to what happens under the hood in a very entertaining way. Through numerous bug anecdotes and trivia (mostly NASA based) the reader will be introduced again to integral promotion, subscripting, decaying and many other C marvels. This book is so captivating that you will probably read the 353 pages within a night and be dissapointed it was so short.

About C++


Well, C++ is very powerful. And no one can say the otherwise. This language has changed a lot during the last 15 years. That's because it's currently under maintenance. An example ? During this month (Aug 18, 2014), C++ 14 was release with tons of new features like the:
  1. Generic lambdas
  2. Return type deduction for functions
  3. Extended capturing in lambdas
  4. constexpr variable templates
  5. Much more!
By the way, the C++ books that you absolutely must have in your library are: The C++ Programming Language, written from the creator of C++. Bjarne Stroustrup. An amazing book. 


A second book that I really appreciate is C++ Primer Plus. This is not your complex, sink or swim C++ book but it does cover a lot. The author reckons each chapter can be covered in a week's tuition and it can be used for a class text book or a self teaching manual. As some chapters are 80 pages, I think a week would be pushing it but it's certainly a great book to learn C++ from.

So, like the article I mentioned at the beginning of this post, I'll leave you with a question: What's your favorite books about algorithms ?

How to code a game

Well, first of all, I'm not a professional game developer, as I wrote in this article, but one of my hobby is to write games.

During my developer's life, I've received a lots of questions about how to become a game developer, how to write a game, and more. In this article I'll try to respond you, explaining what you need in order to write a good game and what could be the right way to follow.

Do you want to be a game developer ?


If you want to be a game developer, you need to read a lot about algorithms (I wrote an article about algorithms books that I liked), and, of course, a mathematical background is needed.

But, assuming you've those kind of skills, what about next ? Well, most games are written using a not-too-much-high-level programming-language, like, of course, C & C++.

Ok, assuming you know C and C++ well enough, the next step is to:
  1. Write an engine yourself directly with OpenGL
  2. <--- OR --->
  3. Choose a good Game Engine that will help you while writing the game

Choosing a good game engine


Well, choose a good game engine is not ease. As far as I'm concerned, it depends on what you expect from it, and, primarily what kind of operating system would you like to support.

The best that I can recommend you are:
  1. Cocos2D (>= v3.x)
  2. Unity3D
Don't be fooled by the naming; believe me, 2D or 3D does not mean what it can do. They support 2D and 3D programming either (assuming you're using the last version of both).


Unity3D


It's a framework, a game engine, an IDE and make you a coffe when you're tired. Shortly: it's what you need assuming that you're ready to pay (yes, it's not free at all!) to write your first game and you know that your games will bring you money.

If you would like to proceed in this way, learn more with this book. Pretty easy.

Cocos2D


This is my favorite game engine and framework. But there are different version of Cocos2D. So, which kind of version is better rather than the others ?
  1. Cocos2D-x: You can code your games using C++
  2. Cocos2D-js: As the name suggests you, it support javascript as the main language. Think as web games.
  3. Cocos2D-swift: The Objective C version of Cocos2D-x, only for iOS and OSX.

I personally use Cocos2D-x because it's open source, free-to-use without pay nothing, and it give you the possibility to easily support:
  1. Windows (Intel + ARM)
  2. Linux
  3. OSX
  4. iOS
  5. Android
About Cocos2D-x, I can recommend you some books:
  1. Cocos2d-x Game Development Essentials
  2. Cocos2d-x by Example Beginner's Guide
But be careful: these books are a little bit old because we're now at the version 3.x of the framework and what I linked above are about the version 2. Many changes was be made between these two versions, and tons of methods and classes are been deprecated, so probably the best reference you may found around the web is this, the official wiki page.

Write a game engine using OpenGL


This is probably the hardest way you may choose.  This topic needs an article. I'll write it later durint this week as a part-2 of this one.

Thursday, 28 August 2014

Good Algorithms Books

During the previous post, I wrote about my interests. One of them are algorithms. I read tons of books about that, and in this article I'll give you which books I've really appreciate and, of course, that I'd like to recommend you.

The Algorithms Library


First of all, imho, the best (must have) books about algorithms are:
  1. The Algorithm Design Manual by Steven S Skiena. I think this is a masterpiece, with a fair amount of mathematical notation and few illustations. Skiena war-stories are amazing and so is the HitchHiker's Guide.
  2. Algorithms by Robert Sedgewick and Kevin Wayne. It covers many topic, features many drawings, no mathematics notation and variable traces to help follow each algorithms chronology. 
  3. Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein. From the MIT Press, and, if I remember correctly, this one is used by a lot of university. 


The last but not the least:

  1. The Art of Computer Programming by Donald E. Knuth. This is a collection of amazing books that covers almost all of what you need to know about algorithms, explaining them a really strict way, using Mathematics notions. It's pretty hard to read, but guys, it's awesome.

What about you ? What's your favorite books about algorithms ? I'm glad to read your preferences.

Hello, World!



Hey, you!

I really don't like to write blog posts. And much more to maintain blog's stuff. Argh.
This is another blog that I'm trying to maintain. Ah. Ah. Once again.


About me


As a developer, my tech skill is about Java, C++, Objective C and C#. These are my starting point, but I'm really interested in learning more. Plus, I really love reading about algorithms and mathematical stuff.

And these will be the articles I'll write here. Yeah.

Actually, I'm working as a Project Manager and Senior Software Engineer at LiveBox from one year. Since when I started here, I worked on different platforms, like iOS, OSX. Currently I'm working with the Windows platform (and, of course, the *ckin .NET).

About LiveBox




LiveBox is a private file sharing. With LiveBox you can store, sync and share your data in a new, better and secure way. It is available on premises with its Private Cloud Platform or hosted by our Partners As a Service. (cit. official site)

This kind of platform is very interesting, and, of course, I'm very glad to work with an amazing team of really talented developers.