Skip to content

Instantly share code, notes, and snippets.

View rtheunissen's full-sized avatar

Rudi Theunissen rtheunissen

View GitHub Profile
def prefetched_related(instance, name, default=None):
"""
Attempts to find and return prefetched data on a model.
Usage:
values, prefetched = prefetched_related(instance, 'name')
values : A list of prefetched values for the field.
prefetched : True if the requested field was prefetched on the instance.
"""
@rtheunissen
rtheunissen / php_bug63217_benchmark.php
Last active June 30, 2018 20:06
PHP Bug #63217 Benchmark Script
<?php
// See https://github.com/php/php-src/pull/3351
$arr = [
"foo" => true,
"foobar" => 3,
"elePHPant" => "qux",
"long_key_name" => "something contrived",
"123" => 76,
@rtheunissen
rtheunissen / gmpi_dec_bench.php
Created September 29, 2018 17:07
Basic benchmark for gmpi vs decimal, using bcmath as reference
<?php
ini_set('memory_limit', -1);
srand(0);
$length = 100000;
$maxInt = 10 ** 10;
$values = [];
for ($i = 0; $i < $length; $i++) {
$int = (rand() % $maxInt);
@rtheunissen
rtheunissen / benchmark.php
Created October 28, 2018 18:35
Benchmark between BC Math and Decimal
<?php
use Decimal\Decimal;
/* Number of times to perform each calculation. */
$iterations = 1000000;
/* Candidates */
define("DECIMAL", "decimal");
define("BC_MATH", "bcmath");
@rtheunissen
rtheunissen / copy_on_write_lazy_map.php
Last active February 5, 2019 21:42
A concept for lazy collections using copy-on-write (I think)
<?php
interface Collection
{
/**
* @return static
*/
function map(callable cb): Collection;
// filter
// reduce
@rtheunissen
rtheunissen / decimal_refcount.php
Created February 19, 2019 00:52
Summary of refcount shortcuts for ext-decimal
<?php
$a = "1";
$b = "2";
/**
* This should reuse the decimal created for A as the result.
*/
$c = \Decimal\Decimal::valueOf($a)->add($b);
debug_zval_dump($c);
// object(Decimal\Decimal)#1 (2) refcount(2){
#pragma once
#pragma warning(push)
#pragma warning(disable:4996)
#include <cassert>
#include <memory>
#include <vector>