PHP Fat Memory Manager Patch available

June 11, 2008 – 12:44 am

My latest performance bottle-neck in PHP has been the frequent memory allocation/deallocation that happens on every request.  I’ve created a speed freak’s patch to help with this by pre-allocating memory and letting it persist across requests.  This removes the interaction with the kernel memory management, and the rest of the internal emalloc/efree calls are dealt with similar to that of a pool allocator.  It’s horribly memory inefficient in the hopes of making some CPU gains.  The patch for PHP-5.2.6 is available, I’d like to hear feedback about it’s success/failure in different environments.

Where’s George?

June 10, 2008 – 11:22 pm

After 4 Years and 179 days WheresGeorge.com has finally come through and notified me of my wondering bills status.  I always thought it would be funny to distribute some dollar bills at a strip club all marked with wheresgeorge.com and a nice description of where I dropped them off at.

after the longjmp….

May 4, 2008 – 7:42 pm

I swear the machines are just fucking with me sometimes:

Breakpoint 3, _zend_bailout (filename=0x4089c8
"/Users/shire/data/php/git/php/Zend/zend_alloc.c", lineno=1693) at
/Users/shire/data/php/git/php/Zend/zend.c:787
787             longjmp(*EG(bailout), FAILURE);
(gdb) n

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;!!!!!!!!!!!!!!!!!!!!;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;!!!!!!!!!!!!!!!!!!!/////////!!!!!!!!!!!;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;!!!!!!!!!!!!!!!!!!//////>)!*)>>/////!!!!!!!!!!;;;;;;;;;;
;;;;;;;;;;;;;;;!!!!!!!!!!!!!!!!!!////////>>))&H.I%)>>/////!!!!!!!!!!;;;;;;;
;;;;;;;;;;;!!!!!!!!!!!!!!!!!!/////////>>>))|&I,..H&))>>//////!!!!!!!!;;;;;;
;;;;;;;;!!!!!!!!!!!!!!!!/////////>>>>>)))|&H;))))>;I|))>>>>////!!!!!!!!;;;;
;;;;;!!!!!!!!!!!!!!///////////>>>)|&&%&&IIH%.)))),#HII&|))|.)>///!!!!!!!;;;
;;;!!!!!!!!!!!!//////////>>>>>>))|&*!!/:H))))))))))))):**H.%.)>///!!!!!!!;;
!!!!!!!!!!!!/////>>>>>>>>>>))))|&I,*;))))))))))))))))))))):*|)>>///!!!!!!!;
!!!!!!!!!//////>>)IH||||&||||||&H#,))))))))))))))))))))))))#HH)>///!!!!!!!!
!!!!!!//////>>>>)|&%:;>* /#:HHH%|)))))))))))

Bash autocompletion + git = super lazy goodness….

April 30, 2008 – 10:20 pm

Lately I’ve been using some longer, and not very memorable, git branch names. I love bash completion and was hoping I could add items to this auto-completion dynamically on request. It turns out this is really simple to do. Thanks to this fab tutorial, a quick code snippet like this in my .profile and I’m all set.


_complete_git() {
  if [ -d .git ]; then
    branches=`git branch -a | cut -c 3-`
    tags=`git tag`
    cur=”${COMP_WORDS[COMP_CWORD]}”
    COMPREPLY=( $(compgen -W “${branches} ${tags}” — ${cur}) )
  fi
}
complete -F _complete_git git checkout

Now anytime I’m in a path with a .git directory, I can just use ‘git checkout [tab]‘ and I’ll git tag and branch auto-completion goodness.

Thanks Vancouver!

April 17, 2008 – 1:21 am

I just got back from the OpenWeb conference in Vancouver, and I thought it was well run and enjoyable.  As with most conferences it’s difficult to make the wireless internet support so many users, not to mention with the restrictions and prices I hear some locations charge coferences.  (I also think electric outlets should be in much wider abundance).  But it’s really all auxillary anyways, and there where plenty of good talks to listen to.

All the talks where video recorded so I’m looking forward to looking over anything I missed as soon as they are up on the site (like the Google Gears talk).  It was refreshing to attend some other talks of interest such as mobile technologies and open source project research

As always you can find my talks in PDF format on my “talks” page.

Tricky Tricky Refcounts….

April 7, 2008 – 3:04 pm

Occasionally a PHP engineer reports this prolem:

Example Code:
————-
$my_arr = array(1,2,3);
foreach ($my_arr as &$val) {
    var_dump($val);
}
foreach ($my_arr as $val) {
    var_dump($val);
}


Expected Output:
———
int(1)
int(2)
int(3)
int(1)
int(2)
int(3)

Actual Output:
——-
int(1)
int(2)
int(3)
int(1)
int(2)
int(2)

The confusion comes from the expectation that the second loop will print the last element of the array as int(3) rather than int(2).  The initial reaction is usally “this is a PHP bug”, but it really isn’t.  There are two key aspects to this code to watch out for; 1) The scope of foreach variables is not limited to the foreach block. 2) Foreach loops do not unset foreach variables at the start of the block.

With this in mind we can see that at the end of the first loop, $val is a reference to the last element of $my_arr.  Each iteration over the foreach loop can be thought of as an assignment operation, in this case by reference:

$val = &$my_arr[0]
$val = &$my_arr[1]
$val = &$my_arr[2]  
// last iteration $val is a reference to $my_arr[2]


As we step through each iteration of the second foreach loop we see the assignments of $val to each element of the $my_arr (assigned by value this time).

$val = $my_arr[0]
$val = $my_arr[1]
$val = $my_arr[2]

But if we you recall $val is really a reference to the last element of $my_arr because it carries over from the first foreach loop, so the actual assignment looks more like:

$my_arr[2] = $my_arr[0]
$my_arr[2] = $my_arr[1]
$my_arr[2] = $my_arr[2]

Thus we end up with $my_array being set as such on each iteration:

// (array(1,2,1))  first element is set to value of last
$my_arr[2] = $my_arr[0] 

// (array(1,2,2))  second element is set to value of last
$my_arr[2] = $my_arr[1] 

// (array(1,2,2))  last element is set to value of itself

$my_arr[2] = $my_arr[2] 

Note that the last assignment is really assigning the last element to itself!

Because PHP5 handles variables with a copy on write algorithm, it’s typically not necessary to do any assignmnents by reference with performance gains in mind (as was the case with a lot of PHP4 code).  The above code can be made to function as the expected case by placing an unset($var) between the foreach loops, or not iterating over references and instead assigning the values of $my_arr explicitly by index or key values.  References should be used by care  and only when necessary.  When code like this is present in global scope or large functions it may affect future code in seemingly unpredictable ways. 

Tags: , , ,

Delivery Date Rescheduled…

March 19, 2008 – 1:01 pm

I love UPS tracking, but I’ve never really seen anything go wrong either…. until now…..  my package has been marked as rescheduled for two days later than expected.  Not too bad considering:

UPS: Tracking Information

Vim Diff

February 21, 2008 – 9:00 pm

For a long time now I’ve wanted to have the current diff of my working copy highlighted in vim. This would help a lot when you’re working in complicated code and want your modifications to pop out in case you’ve missed something. Unfortunately none of the existing tools do exactly what I want, so with the advice of some co-workers I’ve hacked up the svndiff vim script and made it work with git just like I wanted. Now I can turn on or refresh the highlights with Ctl-D and diff against a different branch with “:D <branch>”.

http://tekrat.com/images/vimdiff.jpg


gitdiff.vim
—————
if exists(”loaded_gitdiff”) || &cp
finish
endif
let loaded_gitdiff = 1

map <C-d> :call <SID>Gitdiff()<CR>
map <C-g> :set nodiff<CR>
noremap <unique> <script> <plug>Dh :call <SID>Gitdiff(”h”)<CR>
com! -bar -nargs=? D :call s:Gitdiff(<f-args>)

let g:gitdiff_rev = ”

function! s:Gitdiff(…)
if a:0 == 1
if a:1 == “none”
let g:gitdiff_rev = ”
else
let g:gitdiff_rev = a:1
endif
endif

let ftype = &filetype
let tmpfile = tempname()
let cmd = “cat ” . bufname(”%”) . ” > ” . tmpfile
let cmd_output = system(cmd)
let tmpdiff = tempname()
let cmd = “git diff ” . g:gitdiff_rev . ” ” . bufname(”%”) . ” > ” . tmpdiff
let cmd_output = system(cmd)
if v:shell_error && cmd_output != “”
echohl WarningMsg | echon cmd_output
return
endif

let cmd = “patch -R -p0 ” . tmpfile . ” ” . tmpdiff
let cmd_output = system(cmd)
if v:shell_error && cmd_output != “”
echohl WarningMsg | echon cmd_output
return
endif

if exists(”s:killbuffs”)
2,9999 bdelete
endif
let s:killbuffs = 1
if a:0 > 0 && a:1 == “h”
exe “diffsplit” . tmpfile
else
exe “vert diffsplit” . tmpfile
endif

exe “set filetype=” . ftype

hide
set foldcolumn=0
set foldlevel=100
set diffopt= ” removed filler so we don’t show deleted lines
highlight DiffAdd ctermbg=black ctermfg=DarkGreen
highlight DiffChange ctermbg=black ctermfg=DarkGreen
highlight DiffText ctermbg=black ctermfg=DarkGreen cterm=underline
highlight DiffDelete ctermbg=red ctermfg=white

endfunction

“autocmd CursorHold * call s:Gitdiff()

—————

Tags:

Open Web Vancouver 2008

February 18, 2008 – 2:19 pm

It looks like my talk has been accepted at the Open Web Vancouver 2008 conference. I’m really looking forward to this as this will be my first conference that isn’t strictly PHP. There will be a wider range of topics including Ruby on Rails, Python, Javascript, and even some mobile technology.

I’m really impressed with the site organization so far, I’ve got an account that looks like it lets me update my talk summary and tags. The conference is also really reasonably priced at only $150 (+$20 for a t-shirt)! My talk will be the “APC @ Facebook” talk that I’ve given in the past, but of course I’m hoping to update it with some recent changes and hopefully some new features that I’ve yet to complete.

My name has also apparently made it under the title “Some of the big names you can expect to hear”, although my name is the only one where the hyperlink is on “Facebook” rather than my name. I guess I’m riding in on the coat tails so to speak. ;-)

Update:  Turns out somebody does read my blog and the “coat tailing” is no more, thanks!

Tags:

Forum PHP Paris 2007

November 26, 2007 – 1:10 pm

I had an excellent time at the Forum PHP conference, many thanks to the conference organizers and attendees at the Forum PHP Paris conference. This year I had the honor of getting to speak to this group of people and share how Facebook uses APC to improve site stability and performance over the last couple years, you can check out the slides here.