PHP Mad Notebook

October 21st, 2014

This isn’t a blog. It’s a notebook.

APCu Functions

Arguments in []’s are optional. Cross reference with APC docs. PECL Page. Github.

Iterator functions are omitted, but also available.

The above is a cleaned up version of what’s output by "php --re apcu".

Perl-like ?: Operator

From Tips.

Data Format: Raw PHP variables (var_export)

To serialize something to disk in the fastest way PHP can read it, you make it source code by calling var_export. Whenever the file changes, it should cause a cache miss with OPcache.

To use it:

Alternatively, if you store it a different way:

But this will cause OPcache to miss every time.

Data Format: JSON (decode, encode)

Apparently this is the fastest encoder, as of PHP 5.3. Benchmarks.

json_decode, json_encode, json_last_error

Data Format: Serialize, Unserialize

A faster decoder (slower encoder), and types/classes persist.

serialize, unserialize

Data Format: IGbinary

An alternative, external binary encoder/decoder. According to benchmarks, the fastest.

https://github.com/igbinary/igbinary (PECL)

Smaller too.

Tips and Tricks:http://ilia.ws/files/zendcon_2010_hidden_features.pdf

Data Format: CSV

Reading Only.

http://php.net/manual/en/function.str-getcsv.php

Data Format: XML

Reading Only (there is writing, but it seems more difficult).

If you prefer Array format (like me), here’s a function.

Then simply

Data Format: HTML

Use Simple HTML DOM parser.

http://simplehtmldom.sourceforge.net/ (Manual)

Data Format: String Delimiter

explode, implode

unset

val and type juggling

Shutting Down ludumdare.com/planet!! (Just the planet)

September 4th, 2014

Most people don’t know we have a blog syndication sub-site on Ludum Dare called the Ludum Dare Planet. It’s an extra WordPress blog and site that eats very few resources, but still needs to be regularly updated.

For the sake of security, I’ll be taking the down the planet website. I’m going to wait a few days first, but if nobody responds, I’ll just take it down. If you need time to grab your favorite RSS feeds, come to my blog (http://toonormal.com) and let me know.

EDIT: And the Planet is now gone. Nothing to see here. Move along.

Phat JS Demo

August 21st, 2014

Here’s something different.

Been working on the HTML5 side of my library code, and I kinda made this little demo for fun. Music is Garth Knight’s track Activate the Cage.

Ludum Dare 30 is this weekend. Hoo boy! Running that crazyfest is keeping me sooo busy. :)

Dialogs as Functions

July 16th, 2014

This is just an idea I’ve had. I wanted to write it down in some meaningful way so that I can rediscover it later. This is less a C++’ism, and more a JavaScript/Squirrel’ism.

* * *

In any situation where your user interface presents a popup dialog, that process of presenting the dialog should be a function call. Here is pseudocode:

The general idea here is that there should be 2 ways to use the function: 1. Without arguments, to bring up the entry dialog; 2. With arguments, to bypass the entry dialog (for macro/scripting use). The data in extracted from the function arguments, or the shown dialog, and the action is taken place.

Args should be the language equivalent of an object (JavaScript, Squirrel, etc). Alternatively, an array of objects could work too, but named elements would result in nicer code. Like above, it’s assumed a Yes/No/Cancel dialog would store a boolean named State to tell you which (Yes or No) was picked.

The above assumes a synchronous environment, where it’s safe to give a function “ShowDialog_YesNoCancel” full control.

In an asynchronous environment, you’ll want to use something like a Deferred or Future. If there are no arguments, the ShowDialog function will provide the object you bind your State handling function to. Otherwise, bind it to one that will succeed.

Alternatively, there are probably nice ways to handle this using coroutines or generators.

Checking Symbols GCC and Clang

June 20th, 2014

Just a short one.

Reference: http://stackoverflow.com/questions/4548702/whats-the-equivalent-of-cpp-dd-for-clang

Gives you list of all symbols defined by compilers. It’s interesting, that you have to feed it something (anything blank) to get this output though. /dev/null, or pipe in a blank echo (echo “” | gcc -dM -E -), etc.

Works with NaCl (pnacl-clang), Emscripten (emcc), and pretty much every GCC toolchain known to man.

Setting up NaCL and SDL (on a fresh Ubuntu)

June 6th, 2014

Some notes on getting NaCl working. Of course, things didn’t work out-of-the box.

Install nacl_sdk

Grab and follow the instructions here:

https://developer.chrome.com/native-client/sdk/download

i.e. download and do the following:

Install 32bit Headers

‘cmon, everybody runs 64bit Linux these days (in my case Ubuntu 14.04 64bit). You need the 32bit libraries for anything to build though.

Source: http://stackoverflow.com/questions/23661718/native-client-tutorial-cant-find-libstdc

Follow Tutorial

‘make serve’ will now work.

https://developer.chrome.com/native-client/devguide/tutorial/tutorial-part1

Test2 probably wont run though. I haven’t checked, but I think it may not be a pnacl file (pnacl is necessary to run in a stock Chrome).

EDIT: The problem with Test2 is that newlib is the first Toolchain. If you modify the Makefile by making pnacl the first VALID_TOOLCHAINS (or commenting out the line entirely), and change index.html, the data-tools attribute of the body tag, make pnacl first, it will work correctly.

Now, lets see how far we get compiling SDL NaCL.

Set Environment Variables for SDL

Reference: https://hg.libsdl.org/SDL/file/ae720d61d14d/README-nacl.txt

Do something like the following.

‘readlink -f’ takes a path and converts it in to an absolute path (by following symlinks, ..’s, etc). If you’re on Windows, substitute it for the absolute path to your pepper directory. Mine is /home/mike/Work/Build/nacl_sdk/pepper_35

And of course, edit your pepper version accordingly (35 or whatever).

Build SDL2 in NaCL mode

SDL must be built outside the main SDL directory. I like to call my SDL directory “SDL2″, and do builds in directories like “SDLBuild”. I invoke my build like follows:

Notably, I’m putting the output (make install) inside the SDL subdirectory of the pepper SDK folder.

The Makefile

EDIT: For the makefile to work, I had to make a few changes. Here is the full modified makefile.

The changes are lines 19 and 20. Line 22 is new, as are lines 67 and 68. The last 2 lines are a hack to make “make serve” work the same as it does in the NaCl demos.

Here’s a diff.

For reference, my old notes (before I got it working by changing the makefile):

Okay, this didn’t actually work. I’m not sure exactly how it’s supposed to be configured, but it’s not configured correctly. It should only be used as a reference. Some other notes of things I had to do to get it to work:

naclbuild script

Alternatively, once the environment is set correctly (I think just NACL_SDK_ROOT) it’ll work. In the ‘build-tools’ folder is a script ‘naclbuild’. Run that, and it’ll put some output in the Build folder. Once it finishes, it gives you a string to copy+paste in to your shell. Do it.

Vine Videos: https://vine.co/v/MDnA1x9aPMe, https://vine.co/v/MDn6I6UFWPF

Currently works on Desktop Chrome, and Chrome OS. Will not work on Android (yet).