2018-11-01

Hex to C# Colors Converter

This is C# version for https://openglcolor.mpeters.me that I've been using a lot lately. Paste your colors one per line using RRGGBB or RGB format, with or without hash/pound sign (#).


The result will appear here (Ctrl/Cmd+A, Ctrl/Cmd+C to copy paste):

Techempower Framework Benchmark Round 17

The new result is up, as usual the most important thing is database update benchmark


Top rankers for this time is Rust, Java, C, C#, Go, and C++, the rest has some error or ranked lower than that. Here's some result of multiple queries benchmark:


2018-09-13

String Associative Array Benchmark 2

Redoing old string associative benchmark (3 years ago) but now on Macbook Pro MJLQ2, here's the result:

alias time='/usr/local/bin/gtime -f "\nCPU: %Us\tReal: %es\tRAM: %MKB"'

$ java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
$ time javac hash_map.java
CPU: 1.22s Real: 0.60s RAM: 67540KB
$ time java hash_map
6009354 6009348 611297
36186112 159701682 23370001
CPU: 274.83s Real: 53.47s RAM: 3946268KB

$ go version
go version go1.10.2 darwin/amd64

$ go build map.go
CPU: 0.09s Real: 0.14s RAM: 16160KB
$ time ./map
6009354 6009348 611297
36186112 159701682 23370001
CPU: 29.55s Real: 22.12s RAM: 2425316KB

$ dart --version
Dart VM version: 2.0.0 (Fri Aug 3 10:53:23 2018 +0200) on "macos_x64"

$ time dart ./map.dart
6009354 6009348 611297
36186112 159701682 23370001
CPU: 59.29s Real: 60.08s RAM: 1763952KB

$ node --version
v10.10.0
$ time node object.js # ERROR: TOO SLOW
^CCommand terminated by signal 2 
CPU: 855.68s Real: 763.40s RAM: 1517576KB

$ php -version
PHP 7.1.16 (cli) (built: Mar 31 2018 02:59:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
$ time php -d memory_limit=4G assoc.php
6009354 6009348 611297
36186112 159701682 23370001
CPU: 26.76s Real: 27.89s RAM: 1297824KB

$ mcs --version
Mono C# compiler version 5.10.1.0
$ time mcs dictionary.cs
CPU: 0.21s Real: 0.32s RAM: 42564KB
$ time mono ./dictionary.exe
6009354 6009348 611297
36186112 159701682 23370001
CPU: 33.69s Real: 33.64s RAM: 1609576KB

$ ruby --version
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin17]
$ time ruby hash.rb
time ruby hash.rb
6009354 6009348 611297
36186112 159701682 23370001
CPU: 100.57s Real: 103.63s RAM: 3012148KB

$ jruby --version
jruby 9.2.0.0 (2.5.0) 2018-05-24 81156a8 Java HotSpot(TM) 64-Bit Server VM 25.172-b11 on 1.8.0_172-b11 +jit [darwin-x86_64]
$ time jruby hash.rb # ERROR: TOO MEMORY CONSUMING
Error: Your application used more memory than the automatic cap of 3641MB.
Specify -J-Xmx####M to increase it (#### = cap size in MB).
Specify -w for full java.lang.OutOfMemoryError: GC overhead limit exceeded stack trace
Command exited with non-zero status 1
CPU: 1616.55s Real: 257.91s RAM: 4439172KB

$ python3 --versionPython 3.7.0
$ time python3 dictionary.py
6009354 6009348 611297
36186112 159701682 23370001
CPU: 117.59s Real: 121.63s RAM: 3923812KB

$ pypy --version
Python 2.7.13 (ab0b9caf307db6592905a80b8faffd69b39005b8, Jun 24 2018, 08:19:27)[PyPy 6.0.0 with GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
$ time pypy dictionary.py
(6009354, 6009348, 611297)
(36186112, 159701682, 23370001)
CPU: 28.82s Real: 31.55s RAM: 3993764KB

$ lua -v
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
$ time lua table.lua
6009354 6009348 611297
36186112 159701682 23370001
CPU: 89.05s Real: 91.35s RAM: 2523332KB

$ luajit -v
LuaJIT 2.0.5 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
$ time luajit table.lua # ERROR: CANNOT ALLOC MEMORY
PANIC: unprotected error in call to Lua API (not enough memory)
Command exited with non-zero status 1CPU: 30.57s Real: 31.56s RAM: 1132648KB

This time PHP7 became the best performing programming language implementation, also the least memory consumption (I'm amazed with what they did in version 7). And as usual LuaJIT limited to 1-2GB so they failed, NodeJS too slow, and I'm unwilling to wait more than 10 minutes, so I terminate the app, JRuby hogging RAM too much.


LanguageVersionCompile DurationCompile RAMRuntime DurationRuntime RAM2015's Duration2015's RAMDuration ImprovRAM Improv
C#5.10.1.00.3242,56433.641,609,57640.981,690,22417.91%4.77%
Dart2.0.060.081,763,952133.352,538,22054.95%30.50%
Go1.10.20.1416,16022.122,425,31624.102,748,7848.22%11.77%
Java1.8.0_1720.6067,54053.473,946,268103.544,119,38848.36%4.20%
Lua5.3.591.352,523,332101.733,079,33610.20%18.06%
PHP7.1.1627.891,297,824failedfailed
Pypy6.0.031.553,993,76479.874,034,95660.50%1.02%
Python33.7.0121.633,923,812157.714,335,56822.88%9.50%
Ruby2.3.7p456103.633,012,148107.913,031,8723.97%0.65%

2018-07-01

Introduction to gitflow

GitFlow is one way to manage git repository, where master branch forked into develop branch, and each person will create their own branch when they develop a feature, merged back to develop branch. See this video for the detailed explanation. Also this cheatsheet for the comparison between git-flow command and normal git command.

When you are using Gitflow without git-flow program, you will have to type this long git commands:


Or you can use Git GUIs.

2018-06-12

Costajob HTTP Benchmark

Learning about Pony, a new (at least for me) programming language that designed to be elixir-python-golang like (imho), got me to this page:


It's interesting to see the memory usage of those language-framework, also Crystal, despite inability to utilize multicore still could perform as good as Golang's default muxer (which is not the best).

2018-06-01

Hybrid ECS in Unity

Previously we tried the Pure ECS to create a Pong animation, this article will show example snippet of how to use Hybrid ECS. With hybrid ECS we can use normal game object to act as the entity. What you need to do is:
  • create a prefab, 
  • drag-and-drop your custom Component script (MonoBehavior, not a struct that inherited from IComponentData, so must be exactly one class per file) to the prefab, 
  • then add the GameObjectEntity script to the prefab. 
  • Next, you can instantiate the prefab as usual. 
  • Use a class inherited from ComponentSystem to manipulate the values of those components.
To create a System that uses those GameObjectEntity, you can use injected property or method:

class SomeSystem1 : ComponentSystem {
  ComponentGroup group;
  protected override void OnUpdate() {
    if(group == null || group.CalculateLength() < 1) {
      group = GetComponentGroup(typeof(Direction2D), typeof(Coord2D), typeof(Health)); // must be cached as data member    }
    var dirs = group.GetComponentArray<Coord2D>(); 
    // do something with it, 
    // note that if you want to use Job/Schedule, 
    // you must use [Inject]
  }
}

class SomeSystem2 : ComponentSystem {
  struct Data {

    public GameObjectArray gameObjects;
    public ComponentArray<Position2D> Positions; 
    public ComponentArray<Heading2D> Headings;
    public ComponentArray<Health> Healths;
  }
  [Inject] Data data;
  protected override void OnUpdate() {
    // do something with data.*
    // inherit from JobComponentSystem 
    // if you want to use Job/Schedule
    // also read this
  }
}

One of the biggest benefit of using Hybrid ECS than Pure ECS is that you don't have to wait/implement missing component/system (Animation for example) that not yet implemented by Unity to make stuff works correctly.

This is the example the difference between creating hybrid ECS way and standard/normal way (btw this is not using a correct composition of component, just minimal conversion between those two):


Difference between prefab in Hybrid ECS way and in normal way:


Example code for the system:


For more info, see this slide Unity ECS Intro

Update 2019-05-07: see this video for better understanding how to use DOTS (the ECS new name on unity 2019)

2018-05-10

Rendering Sprite using ECS and JobSystem in Unity

Old style Unity programming, known for slow performance when number of GameObjects grows, that can be solved by managing the update call ourself. Object pooling known to be used to minimize GC overhead (that can cause stutter). ECS (Entity Component System), is a architectural pattern, known best for simplifying a complex system, that popularized and mostly used in Games. In ECS, composition are preferred than inheritance (like Golang :3 yay).
  • Component is a structure that consist of one or more computable element, for example: Position, Sprite, Velocity, Input, AI, etc. 
  • Entity is collection of Components, for example: 
    • Player (consist of Position, Velocity, Sprite, Input), 
    • Enemy (consist of Position, Velocity, Sprite, AI), 
    • Tree/Pillar (consist of Position, Sprite), etc.
  • System is a function that process components in a batch, for example: 
    • Movement (uses Position, Velocity),
    • Render (uses Position, Sprite),
    • PlayerControl (uses Player.Velocity, Input)
    • Bot (uses Enemy.Velocity, Player.Position)
In Unity, the latest stable version (2018.1.0, 2018-05-02) at the time of this article written have built in support for ECS. The benefit of using built-in ECS is the new Job System (utilizing multi-core and cache-friendly, automatic scheduling, prevent race condition) and Burst Compiler that able to utilize SIMD. There's another alternative for ECS that known long time ago if you don't want to use Unity's, such as Entitas framework (which has best documentation among others), Artemis (C# version), Leopotam, and EgoECS.

To use the Unity ECS, there's two method that can be used:
  • Hybrid, to create hybrid ECS, all you need to do is add an EntityGameObject script on the prefab.
  • Pure, you must create an entity manually.
To start any type of ECS project, all you must do is edit Packages/manifest.json, update to something like this:

{
    "dependencies": {
        "com.unity.incrementalcompiler": "0.0.38", 
        "com.unity.entities": "0.0.12-preview.1"
    }, 
    "registry": "https://staging-packages.unity.com", 
    "testables": [
        "com.unity.collections", 
        "com.unity.entities", 
        "com.unity.jobs"
    ]
}

Then you must go to Edit > Project Settings > Player, Other Settings, Scripting Runtime Version to 4.x. Also it's better to use il2cpp than Mono for the scripting backend.
To start a pure ECS, first you must decompose all Component to structs, for example:


Then start create the bootstrap code (one that spawns the entity):


Then create a system to manipulate the values:


Add the GameBootstrap to the main camera or the canvas (don't forget to add the canvas). Set these values on your Unity:
  • Main camera > projection to "perspective"
  • Canvas 
    • Canvas Scaler reference resolution to 800 x 600 (or whatever you like)
    • Screen match ratio to 0.5
    • X rotation to 90, render mode to "screen space - camera"
    • Drag your main camera to the "Render Camera"
  • Create a directory "Resources/" inside "Assets/" and add a ball.png image with transparency, in my case I used 600x600 image.
  • Install or git clone SpriteInstanceRenderer component to render the sprite using ECS. This component requires TransformMatrix, Heading2D and Position/Position2D component to render the sprite.
That's it, now you have created a simple pong animation with ECS, it would show something like this (I add a text on each corner to make sure the coordinate system is right):



To debug the entities, since it would not shown on the Hierarchy panel, click the Window > Entity Debugger. It would show something like this:


When you click one of the entity, you watch the current component values on Inspector panel, like this:


An the core utilization on the profiler:



2018-03-07

Cross Platform Mobile App Development

There's some new alternative for near native performance out there today:

  1. Flutter Beta1 (Dart),
    pros: developed by Google, hot reload
    cons: the Dart programming language have a bit awkward syntax, standard library a bit incomplete compared to other programming languages
  2. NativeScript Vue or Vue-Native (Javascript)
    pros: no angular :3, Javascript!
    cons: Javascript! very early
  3. GoMatcha (Go)
    pros: Go!
    cons: very early

Sorry but i didn't intend to add ReactNative, personal reason: the error message is too freakin ugly. Also there are some others like Unity3D (for games, inefficient =battery-drain if you use it for static UIs), Xamarin (.NET), Scade (Swift), CodenameOne (Java), Kotlin/Native (Kotlin), Qt (C++), LiveCode (Transcript) and others (especially game engines like: Marmalade with C++/Lua, Corona with Lua) and of course you can choose other Hybrid or HTML5-based ones.

Update 2019-05-07: https://wails.app/ (not native, it's Golang + Javascript)
Update 2020-01-22: SvelteNative, FuseOpen
Update 2020-01-29: UnoPlatform

2018-02-15

TechEmpower Framework Benchmark Round 15

As usual, the only one matters are data updates and multiple queries.

Top ranker languages are C, C++, Java, C#, Dart, Python, Go, Perl, Scala, Javascript.


Top ranker language for multiple queries: Dart, Java, C++, C, Kotlin, Rust, Ur, Go.

Dart seems to be getting more and more popularity, since a framework for cross platform mobile app: Flutter is very usable.