This is an experiment to recreate the basics of Sokpop’s n-body game, in HTML5 canvas using, the new Gemini 2.0 Flash. The game is a simple physics-based game where you shoot a comet towards a target while other stars or suns attract the comet you shoot. The game is simple but has a nice feel to it. I tried to do this purely by talking to the LLM. I didn’t look at the code until the end. I wanted…
As a programmer working on an Unreal game I sometimes need simple textures to design effects. Seeing as I’m really bad with photoshop and other image editing software I always prefer to generate the textures programatically. In the following screenshot you can see a simple material that generates a beam texture. You can make the beam wider or narrower by playing with the exponent in the Power…
While creating weapon effects for our game I needed quite dynamic particle effects that look random but still depend on the range and splash range of the weapons. Using Distribution Vector Param and dynamically setting the range of the particle effects works for some cases but doesn’t work if you have multiple particles that need to have random values. To fix this I created the missing…
Google uses tpc.googlesyndication.com for its adserving. They basically allow anything to run on this domain. There is a container html file hosted on this domain which reads the name of the iframe and injects the name content into the page. We can use this to run anything we want on this google domain. Example php code: <?php $html = '<script…
For anyone like me wanting to relieve some stress on the garbage collector and wondering if they should use sync.Pool or use a buffered chan. Wonder no more, here is a simple benchmark to show the difference: BenchmarkNoPool-4 3000000000 21.4 ns/op 8 B/op 1 allocs/op BenchmarkSyncPool-4 3000000000 25.9 ns/op 0 B/op 0 allocs/op BenchmarkChanPool-4 300000000 266 ns/op 0 B/op 0 allocs/op As you can…
SO_REUSEPORT is a relatively new kernel feature which allows multiple processes to bind to the same socket:port combination. Before SO_REUSEPORT this was only possible either by forking the original process and inheriting the file descriptors or sending the file descriptors over a unix domain socket using send2. Having multiple processes listen on the same port can be very useful in high…
Most browsers support the iframe sandbox attribute in some form: Browser Version IE 10+ ( msdn ) Chrome 5+ ( blog ) Firefox 17+ ( mdn ) Opera 15+ ( msdn ) Safari 5+ Commonly supported tokens are: Token allow-forms allow-orientation-lock allow-pointer-lock allow-same-origin allow-scripts allow-top-navigation Propagation When an iframe opens a new window through target=_blank or window.open() , some…
When using multiple goroutines you often want to have shared access of some read only resource. There are multiple ways to implements this. Three of them are; sync.RWMutex, atomic.Value and unsafe.Pointer. sync.RWMutex is a whole mutex so I would expect that to be the slowest. atomic.Value basically stores an interface{} value and makes sure that once assigned the type is never allowed to change.…
Ever wonder if it’s faster to use a slice or map to represent a set in go? I wrote the following program to find out: package main import ( "fmt" "math/rand" "time" ) const maxSize = 26 const tests = 20000000 const findMult = 9999999999 // 100 = 1% hit rate, 1 = 100% hit rate. type lst [] int func ( l lst ) has ( xxxx int ) bool { for _ , i := range l { if i == xxxx { return true } } return false…
Well of course it’s thread safe. All syscalls are thread safe. My real question is if calls to write() from multiple threads would interleave the data or write it one after the other. To test this I wrote a simple program: #include <unistd.h> #include <fcntl.h> #include <pthread.h> #include <stdlib.h> const int bs = 1024 * ( 1024 * 3 + 1 ); int fd ; void foobar ( char * x ) { // Put this on the…
The Golang JSON module doesn’t support marshaling maps with keys that are anything else than a string. I needed to serialize a map with integers as keys, so I wrote one. I’m just posting this here in case it’s useful for anyone else: type Intmap map [ int ] int func ( i Intmap ) MarshalJSON () ([] byte , error ) { x := make ( map [ string ] int ) for k , v := range i { x [ strconv . FormatInt (…
According to RFC 3492 Unicode domains are encoded using Punycode . So http://日本語.jp is actually http://xn–wgv71a119e.jp I was interested in when browsers would use Unicode and when they would use Punycode. Here are the results of my tests: browser location bar location.href dom referrer http referrer firefox 36 Unicode Unicode ASCII ASCII safari 8.0.3 Unicode ASCII ASCII ASCII chrome 41 ASCII…
For an internal project I did some research into how reliable UDP really is. In our case each new packets would overwrite any old ones so missing a couple of packets would be no issue. With UDP it’s technically also possible for packets to arrive in a different order. This would easily be detected by having an incrementing version counter and only accepting the packet if it has a higher version…
In my previous post I tested the implementation of a queue in Go. As I noted in the post I was a bit surprised at how in Go you don’t need a ring buffer to implement a queue. Today for comparison I also implemented the same queue using a ring buffer in Go. The result surprised me. The ring version seems to be more than twice as fast and uses less memory: BenchmarkSliceAdd 20000000 42.2 ns/op 41…
Coming from a C background looking at the following Go implementation of a queue immediately raised a big question for me. type Intqueue [] int func ( q * Intqueue ) Add ( i int ) { * q = append ( * q , i ) } func ( q * Intqueue ) Remove () ( int , bool ) { if len ( * q ) == 0 { return 0 , false } else { i := ( * q )[ 0 ] * q = ( * q )[ 1 : ] return i , true } } The Add function just appends…
To increase the session duration in phpMyAdmin you have to change 2 different settings. I’m mostly writing this post for myself because I always forget which two. In phpmyadmin/config.inc.php you have to add: <? $cfg [ 'LoginCookieValidity' ] = 60 * 60 * 24 ; // 1 day. And in /etc/php5/fpm/php.ini you have to change: session.gc_maxlifetime = 86400 If you don’t change this setting it will be 1440…
When working on a project I was wondering if all browsers would accept cookies set in 204 No Content http responses. I wrote a small test page: dubbelboer.com/204cookie.php What I found is that all major browsers , IE (6, 7, 8, 9, 10, 11), FF (6 - 29), Safari (5 - 7), Opera (12 - 22) on both windows and Mac, set cookies on 204 http responses . 204cookie.php <? if ( isset ( $_GET [ 'show' ])) { ?>…
When developing web applications I very often use ports different than 80. Mostly because port 80 is already taken. When using Chrome there are actually some ports you want to avoid because they are blocked by default. Here is a list of those ports and what runs on them normally: 1, // tcpmux 7, // echo 9, // discard 11, // systat 13, // daytime 15, // netstat 17, // qotd 19, // chargen 20, // ftp…
When writing some code that should be as small as possible you don’t want to include jQuery, MooTools or any other library. These two functions are the functions that I miss most in this case, and I actually think these should implemented natively by the browsers. getParameterByName(name) returns the value of the named url parameter. getCookieByName(name) returns the value of the cookie with the…
Today I was wondering how many 302 redirects browsers would follow before stopping. There are some posts about this but I couldn’t find a source which was up to date and included all browsers. So I decided to make my own: Browser Redirects Firefox 6 - 37 20 Chrome 22 - 23 20 Chrome 41 20 but will automatically retry after a couple of seconds Opera 12 20 Opera Next 29 20 Safari 6 - 8 16 IE 6 100 IE…
For my work I needed to blur parts of an image. PHP has several methods to do this which all yield different results. This post shows the output of most of these methods. Normal image Gaussian filter Gaussian filter times 20 This time I applied the gaussian filter 20 times in a row. This can be quite slow. Smooth 0 The IMG_FILTER_SMOOTH filter with a smoothness level of 0. Smooth -2 The…
Recently I needed a simple LRU cache . Since the standard library doesn’t provide one I decided to write one myself. The cache needed a simple interface to get and set items and needed to be at least logarithmic in complexity. Something like: template < class KeyType , class ItemType > class LRUCache { LRUCache ( size_t capacity ); size_t Capacity (); void Resize ( size_t capacity ); ItemType Get…
When working on a project I was wondering if all browsers would accept cookies set in http redirects. After searching for a bit I found some contradicting articles and bug reports indicating that this might not always be the case. So I decided to test this in a lot of browsers and post the results online for others to find. I wrote a small test page: dubbelboer.com/302cookie.php What I found is…
Brainfuck is an esoteric programming language with a very minimal amount of instructions. This makes it an easy candidate to write interpreters and compilers for. As simple exercise I have written brainfuck interpreters in many languages. This time I tried a different approach. I have written a program to compile brainfuck to native instructions and then execute them. I’m using GNU lightning for…
This function allows you to execute another process but with a timeout on how long it’s allowed to run. Update 2018-01-15 Made the function actually work by changing stderr to non-blocking as well. Before it never worked :) Thanks to danny23 for pointing this out. Update 2012-08-28 Changed the code to use exec inside proc_open . This causes proc_terminate to work correctly. One disadvantage is…
While writing some PHP I ran into the following undocumented behaviour. When calling a getter for a property recursively PHP will always return NULL on the recursive calls. <? class Test { private $data = array (); public function __set ( $name , $value ) { $this -> data [ $name ] = $value ; } public function __get ( $name ) { if ( isset ( $this -> data [ $name ])) { return $this -> data [ $name…
A very useful javascript function that not many people seem to know is bind() bind() allows you to set the this variable a function is executed in. When using a more object oriented approach this can be very useful. Another thing you can do with bind() is pre assign some of the arguments provided to the function. This can be used to pass additional arguments to your event handlers. See this simple…
For one of our company websites we have an HTML file upload utility. <input type= file id= input onchange= "handleFiles(this.files)" > The utility uses an XMLHttpRequest to post the files to our server. function handleFiles ( files ) { var xhr = new XMLHttpRequest ; xhr . open ( ' post ' , handler . url , true ); xhr . setRequestHeader ( ' X-File-Name ' , files [ 0 ]. fileName ); xhr .…
I have learned recently that V8, the javascript engine behind nodejs can’t optimize inside a try/catch block. Just look at the running times of the next two examples: function test () { try { for ( var i = 0 ; i < 1000000000 ; ++ i ) { if ( i % 2 ) { ; } } } catch ( e ) { } } test (); This example takes on average 9 seconds to complete. function test () { for ( var i = 0 ; i < 1000000000 ; ++ i )…
SYN cookies So one day I noticed /var/log/syslog on one of our servers was filled with the following message: TCP: Possible SYN flooding on port 80. Sending cookies. This message can come a from a SYN DDOS , but in our case it was because of the amount of new connections one of our application was receiving. The syslog message is emitted when the SYN backlog of a socket is full. The kernel…
Anonymous functions As of 5.3.0 PHP has support for anonymous functions. There are two well known and one lesser known way to pass variables to your anonymous functions. Using the global scope. Passing them as arguments (optionally as reference if you want to be able to modify them). Using the use keyword. The use keyword The use keyword allows you to introduce local variables into the local scope…
If you are writing some really high performance C code. Or if you just like to do some premature C optimizations. Here’s a gcc compiler buildin you can use: integer __builtin_expect (( integer variable ),( expected value )) Since CPU’s prefetch instructions in a sequential way, jumps to other instructions will flush all prefetched instructions. Because of this it is better to only jump in the…
For my company we have an ad-server which handles hundreds of connections per second. Many of those are HTML iframes for ads. It doesn’t really make sense for those pages to have a favicon but we noticed that many browses still request favicon.ico. This only ads load to our already busy server so we place the following code in our iframe HTML: <link rel= "shortcut icon" href=…
Since 5.3 PHP supports Blowfish hashing. This is commonly seen as the most secure way to hash a password. The main advantage is that the function can be set to be really slow which makes brute forcing useless. Here’s an example implementation: <? class BlowfishPassword { const strength = 13 ; // On our machine this makes it slow but not so slow as to annoy the user (less then 1 second). private…
A feature of PHP that many people don’t seem to be aware of is the .user.ini file. This per directory configuration file can be very useful in combination with the following settings auto_prepend_file If your application always includes a common include file you can also add it like this. In our applications we use a auto_prepend_file = "autoloader.php" which registers a class autoloader. This way…
Looking at the new Google Analytics live view I noticed they use the multipart/x-mixed-replace content type to push live updates to the webbrowser. Using this special Content-type you can replace the contents of a page. You can find this example here: dubbelboer.com/multipart.php . <? // Make sure PHP isn't buffereing anything. ob_end_clean (); // Sending this header will prevent nginx from…
While writing some object oriented php at my job we ran into the following strange php behaviour. <? // We want to use exceptions to catch non existing class errors. spl_autoload_register ( function ( $classname ) { throw new Exception (); }); Now we can try to instantiate some class that doesn’t exist. <? try { new UnknownClass (); } catch ( Exception $err ) { } This works as it should. It will…