SlideShare a Scribd company logo
1 of 36
Computer Science in
WordPress
Taylor Lovett
My name is Taylor Lovett
- Senior Strategic Web Engineer at 10up
- Core Contributor
- Plugin Author (Safe Redirect Manager)
- Plugin Contributor
- BS in Computer Science from the University
of Maryland, College Park
What is Computer Science?
- It can mean a lot of things. It is really the
study of computational theory, computer
software, and hardware.
Theory of Computation
- General Mathematics (Calculus, linear
algebra, general computational theory,
statistics)
- Algorithms (a method to solve a problem)
- Data structures (which data structure will
allow us to access our data the quickest?)
- Graph theory
Computer Software
- Programming techniques and design patterns
(i.e a singleton class)
- Concurrent design patterns (data races)
- Mobile software development
- Operating system software
- Web development
- Databases
- Networking
- Benchmarking
Computer Hardware
- Motherboards
- Memory types (solid state, RAM, etc.)
- Benchmarking (processor execution time)
- Pipelining
- Processors
Big-Oh Notation
- "Big O notation is used to classify algorithms by how
they respond (e.g., in their processing time or working
space requirements) to changes in input size." --
Wikipedia
- Very useful to describe how performant your code
may or may not be
- Big-Oh usually describes the upper bound of a
function (worst-case)
Big-Oh Notation (cont.)
- Big-Oh notation is concerned with measuring the rate
of growth of the amount of processing that your code
might do on an unknown input size
- In Big-Oh we are only concerned about how a our
code performs as the input size approaches infinity.
Mathematically speaking, this means we only care
about the highest order term:
i.e. O(3n2 + 5n) = O(n2) since as n approaches infinity
the only thing that matters is the n2
Let's look at some
examples!
// $fruits contains a non-empty array of strings
function contains_orange( $fruits = array() ) {
for ( $i = 0; $i < count( $fruits ); $i++ ) {
if ( 'orange' == $fruits[$i] ) return true;
}
return false;
}
Best Case Scenario: Loop executes once,
orange is found, and it returns.
Worst Case Scenario: Loop executes n times
(where n is the number of elements in $fruits)
Performance: contains_orange() is in O(n)
Remember!
- With Big-Oh we are only concerned with what
happens in the worst case. Sometimes knowing
what happens in the best case is useful, but we
are mostly worried about the performance hit
our code could take in the worst possible
situation.
// $fruits contains a non-empty array of strings. For educational
// purposes, $fruits is guaranteed to have at least one duplicate.
function contains_duplicate_fruit( $fruits = array() ) {
for ( $i = 0; $i < count( $fruits ); $i++ ) {
for ( $z = 0; $z < count( $fruits ); $z++ ) {
if ( $i != $z && $fruits[$z] == $fruits[$i] )
return true;
}
}
return false;
}
What does everyone think?
Best Case Scenario: Outer loop executes
once, inner loop executes twice, duplicate is
found, function returns
Worst Case Scenario: Outer loop executes n -
1 times (where n is the size of $fruits), inner
loop executes n times for each outer loop
execution... n * (n -1) = n2 - n
Performance: contains_duplicate_fruit is in
O(n2 - n) = O(n2)
An important reminder
- We dropped the (-n) from our final Big-Oh
evaluation because, as n approaches infinity,
n2 dominates and (-n) becomes insignificant.
But seriously... How is
this useful?
Big-Oh Notation and Databases
- Big-Oh notation is used a lot in conjunction
with SQL operations.
- We've all heard that indexing a column in
MySQL makes search on that column faster.
- But why? What does that actually mean?
MySQL Indexes
- An index is a data structure that speeds up
search time for information.
- Without an index, searching for a specific
column value is O(n) because in the worst case
scenario every single row in the table must be
examined.
MySQL Indexes
- When a column is indexed, MySQL takes the data
across all of the rows in that column and stores
references to that data in a B-tree (this structure is
used for the majority of index types).
- A B-tree is just what it sounds like: A tree of data that
speeds up search time. The worst case scenario for
the amount of items to be processed in a B-tree is log
n. A log is a mathematical function such that:
n2 > n > log n
http://en.wikipedia.org/wiki/B-tree
Post Meta Queries
- The full Big-Oh analysis of a post meta query is
pretty complex because of the join operation and
therefore is outside the scope of this talk.
- For our purposes, searching for posts based on a
meta key is O(n) where n is the number of posts that
have that key.
- Let's frame this in terms of featured posts. Featured
posts refers to the situation where a website needs to
mark select posts as featured and query for them.
Featured Posts Solution #1
On post update:
if ( isset( $_POST['meta_box_feature'] ) )
update_post_meta( $post_id, 'featured', 1 );
else
update_post_meta( $post_id, 'featured', 0 );
Query:
$args = array(
'meta_key' => 'featured',
'meta_value' => 1,
);
$featured_posts = new WP_Query( $args );
Solution #1 Analysis
- Using this code, every time a post is saved, it will have
post meta attached to it such that 'featured' = 1 or 0. This
will create a ton of unnecessary post meta rows.
- Remember searching for posts based on a meta key is
O(n) where n is the number of posts that have that key.
Therefore saving meta when a post is not featured is not
only unnecessary but will really slow us down. This would
result in O(m) performance where m is the number of
posts!
Featured Posts Solution #2
On post update:
if ( isset( $_POST['meta_box_feature'] ) )
update_post_meta( $post_id, 'featured', 1 );
else
delete_post_meta( $post_id, 'featured' );
Query:
$args = array(
'meta_key' => 'featured',
'meta_value' => 1,
);
$featured_posts = new WP_Query( $args );
Solution #2 Analysis
- This solution is a major improvement over our first
one. This will result in O(n) search time where n is the
number of featured posts.
- However, we can still do better.
Featured Posts Solution #3
Let's create a tag called 'featured' and attach it to all our featured
posts:
On init:
$args = array( ... );
register_taxonomy( 'featured', 'post', $args );
Query:
$args = array(
'post_tag' => 'featured'
);
$featured_posts = new WP_Query( $args );
Solution #3 Analysis
- For our purposes, searching for posts based on a tag
is O(log n) since there is an index on the tag id
column.
The full Big-Oh analysis of our tag solution is pretty
complex due to SQL join operations and therefore is
beyond the scope of this talk.
Concurrency
- In Computer Science concurrency is a
property describing the event where multiple
computations are executed simultaneously,
sometimes interacting with each other.
Concurrency
- With concurrent programming we can, among
other things, force each core in a computer to
process a piece of a larger problem or handle
separate tasks. This is extremely powerful.
- When not properly account for, Concurrency
can sometimes result in unexpected bugs that
are difficult to reproduce.
Concurrency in WordPress
- Concurrency takes a slightly different form in
WordPress. We don't solve problems by
starting new threads/processes. However,
since behind the scenes servers can run
multiple processes at the same time and thus
multiple users can execute the same code
simultaneously, issues surrounding
concurrency can arise.
Tracking Postviews in WordPress
- A common request in WordPress is to display the
number of views for each post on the frontend.
- There are many different ways to approach this
problem; the most common is to increment an
integer stored in post meta each time a post is
viewed, then to display this number for each post.
- This implementation can lead to data races.
Here is the code that executes on
each post request
$views = get_post_meta( $id, 'views', true );
$views++;
update_post_meta( $id, 'views', $views );
Data Races
- A data race is the situation where two or more
threads access a shared memory location, at
least one of those accesses is a write, and the
order of the accesses is unknown (meaning
there are no explicit locking mechanisms used).
- Think of each page request as a thread on the
server. If two users request a post at the same
time, a data race for pageviews occurs since
both accesses are writing to the postmeta
table.
A Possible Ordering of Events
Code executed for User A is in red and User B in blue
$views = get_post_meta( $id, 'views', true ); // $views = 0
$views++; // $views = 1
update_post_meta( $id, 'views', $views ); // _views = 1
$views = get_post_meta( $id, 'views', true ); // $views = 1
$views++; // $views = 2
update_post_meta( $id, 'views', $views ); // _views = 2
In this ordering of events, $views ends up with a value of 2
which is what we want. However, these events could occur
in any order...
Another Ordering of Events
$views = get_post_meta( $id, 'views', true ); // $views = 0
$views = get_post_meta( $id, 'views', true ); // $views = 0
$views++; // $views = 1
$views++; // $views = 1
update_post_meta( $id, 'views', $views ); // _views = 1
update_post_meta( $id, 'views', $views ); // _views = 1
In this ordering of events, $views ends up with a value of 1
which is NOT what we want.
Conclusion:
This algorithm won't work!
Solution to Pageview Problem?
Solution 1: Jetpack plugin. We can install
Jetpack and leverage it's stats API to query
information on specific posts.
Solution 2: Google Analytics. Using a websites
Google Analytics account, we can set custom
variables on a post-to-post basis and query the
API based on those variables.
Questions?

More Related Content

What's hot

Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesNina Zakharenko
 
Ch7(publishing my sql data on the web)
Ch7(publishing my sql data on the web)Ch7(publishing my sql data on the web)
Ch7(publishing my sql data on the web)Chhom Karath
 
[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018Adam Tomat
 
Introduction to apache lucene
Introduction to apache luceneIntroduction to apache lucene
Introduction to apache luceneShrikrishna Parab
 
Modern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsModern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsMike North
 
Data models in Angular 1 & 2
Data models in Angular 1 & 2Data models in Angular 1 & 2
Data models in Angular 1 & 2Adam Klein
 
Apache Lucene: Searching the Web and Everything Else (Jazoon07)
Apache Lucene: Searching the Web and Everything Else (Jazoon07)Apache Lucene: Searching the Web and Everything Else (Jazoon07)
Apache Lucene: Searching the Web and Everything Else (Jazoon07)dnaber
 
Integrating the Solr search engine
Integrating the Solr search engineIntegrating the Solr search engine
Integrating the Solr search engineth0masr
 
How to automate all your SEO projects
How to automate all your SEO projectsHow to automate all your SEO projects
How to automate all your SEO projectsVincent Terrasi
 
High-Performance Hibernate Devoxx France 2016
High-Performance Hibernate Devoxx France 2016High-Performance Hibernate Devoxx France 2016
High-Performance Hibernate Devoxx France 2016Vlad Mihalcea
 
Introducing Amplify
Introducing AmplifyIntroducing Amplify
Introducing AmplifyappendTo
 
Elasticsearch a real-time distributed search and analytics engine
Elasticsearch a real-time distributed search and analytics engineElasticsearch a real-time distributed search and analytics engine
Elasticsearch a real-time distributed search and analytics enginegautam kumar
 
Custom Database Queries in WordPress
Custom Database Queries in WordPressCustom Database Queries in WordPress
Custom Database Queries in WordPresstopher1kenobe
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPressTareq Hasan
 

What's hot (20)

Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
 
Ch7(publishing my sql data on the web)
Ch7(publishing my sql data on the web)Ch7(publishing my sql data on the web)
Ch7(publishing my sql data on the web)
 
Recursive Query Throwdown
Recursive Query ThrowdownRecursive Query Throwdown
Recursive Query Throwdown
 
[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018
 
Introduction to apache lucene
Introduction to apache luceneIntroduction to apache lucene
Introduction to apache lucene
 
Introduction to Apache Solr
Introduction to Apache SolrIntroduction to Apache Solr
Introduction to Apache Solr
 
Modern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsModern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.js
 
Data models in Angular 1 & 2
Data models in Angular 1 & 2Data models in Angular 1 & 2
Data models in Angular 1 & 2
 
Mentor Your Indexes
Mentor Your IndexesMentor Your Indexes
Mentor Your Indexes
 
Apache Lucene: Searching the Web and Everything Else (Jazoon07)
Apache Lucene: Searching the Web and Everything Else (Jazoon07)Apache Lucene: Searching the Web and Everything Else (Jazoon07)
Apache Lucene: Searching the Web and Everything Else (Jazoon07)
 
Integrating the Solr search engine
Integrating the Solr search engineIntegrating the Solr search engine
Integrating the Solr search engine
 
Apache Solr Workshop
Apache Solr WorkshopApache Solr Workshop
Apache Solr Workshop
 
Introduction To Apache Lucene
Introduction To Apache LuceneIntroduction To Apache Lucene
Introduction To Apache Lucene
 
How to automate all your SEO projects
How to automate all your SEO projectsHow to automate all your SEO projects
How to automate all your SEO projects
 
High-Performance Hibernate Devoxx France 2016
High-Performance Hibernate Devoxx France 2016High-Performance Hibernate Devoxx France 2016
High-Performance Hibernate Devoxx France 2016
 
Introducing Amplify
Introducing AmplifyIntroducing Amplify
Introducing Amplify
 
How Solr Search Works
How Solr Search WorksHow Solr Search Works
How Solr Search Works
 
Elasticsearch a real-time distributed search and analytics engine
Elasticsearch a real-time distributed search and analytics engineElasticsearch a real-time distributed search and analytics engine
Elasticsearch a real-time distributed search and analytics engine
 
Custom Database Queries in WordPress
Custom Database Queries in WordPressCustom Database Queries in WordPress
Custom Database Queries in WordPress
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 

Viewers also liked

Best Practices for WordPress
Best Practices for WordPressBest Practices for WordPress
Best Practices for WordPressTaylor Lovett
 
Best Practices for WordPress in Enterprise
Best Practices for WordPress in EnterpriseBest Practices for WordPress in Enterprise
Best Practices for WordPress in EnterpriseTaylor Lovett
 
JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPressTaylor Lovett
 
Modernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with ElasticsearchModernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with ElasticsearchTaylor Lovett
 
You Got React.js in My PHP
You Got React.js in My PHPYou Got React.js in My PHP
You Got React.js in My PHPTaylor Lovett
 
Wordpress search-elasticsearch
Wordpress search-elasticsearchWordpress search-elasticsearch
Wordpress search-elasticsearchTaylor Lovett
 
Isomorphic WordPress Applications with NodeifyWP
Isomorphic WordPress Applications with NodeifyWPIsomorphic WordPress Applications with NodeifyWP
Isomorphic WordPress Applications with NodeifyWPTaylor Lovett
 

Viewers also liked (8)

Fluxible
FluxibleFluxible
Fluxible
 
Best Practices for WordPress
Best Practices for WordPressBest Practices for WordPress
Best Practices for WordPress
 
Best Practices for WordPress in Enterprise
Best Practices for WordPress in EnterpriseBest Practices for WordPress in Enterprise
Best Practices for WordPress in Enterprise
 
JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPress
 
Modernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with ElasticsearchModernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with Elasticsearch
 
You Got React.js in My PHP
You Got React.js in My PHPYou Got React.js in My PHP
You Got React.js in My PHP
 
Wordpress search-elasticsearch
Wordpress search-elasticsearchWordpress search-elasticsearch
Wordpress search-elasticsearch
 
Isomorphic WordPress Applications with NodeifyWP
Isomorphic WordPress Applications with NodeifyWPIsomorphic WordPress Applications with NodeifyWP
Isomorphic WordPress Applications with NodeifyWP
 

Similar to Computer Science Concepts for WordPress Developers

Questions On The Code And Core Module
Questions On The Code And Core ModuleQuestions On The Code And Core Module
Questions On The Code And Core ModuleKatie Gulley
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNicole Gomez
 
Building an Analytic Extension to MySQL with ClickHouse and Open Source
Building an Analytic Extension to MySQL with ClickHouse and Open SourceBuilding an Analytic Extension to MySQL with ClickHouse and Open Source
Building an Analytic Extension to MySQL with ClickHouse and Open SourceAltinity Ltd
 
Building an Analytic Extension to MySQL with ClickHouse and Open Source.pptx
Building an Analytic Extension to MySQL with ClickHouse and Open Source.pptxBuilding an Analytic Extension to MySQL with ClickHouse and Open Source.pptx
Building an Analytic Extension to MySQL with ClickHouse and Open Source.pptxAltinity Ltd
 
Simplified Data Processing On Large Cluster
Simplified Data Processing On Large ClusterSimplified Data Processing On Large Cluster
Simplified Data Processing On Large ClusterHarsh Kevadia
 
Microsoft azure data fundamentals (dp 900) practice tests 2022
Microsoft azure data fundamentals (dp 900) practice tests 2022Microsoft azure data fundamentals (dp 900) practice tests 2022
Microsoft azure data fundamentals (dp 900) practice tests 2022SkillCertProExams
 
Martin Chapman: Research Overview, 2017
Martin Chapman: Research Overview, 2017Martin Chapman: Research Overview, 2017
Martin Chapman: Research Overview, 2017Martin Chapman
 
Using R on Netezza
Using R on NetezzaUsing R on Netezza
Using R on NetezzaAjay Ohri
 
Understanding and building big data Architectures - NoSQL
Understanding and building big data Architectures - NoSQLUnderstanding and building big data Architectures - NoSQL
Understanding and building big data Architectures - NoSQLHyderabad Scalability Meetup
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01shaziabibi5
 
Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Rajeev Rastogi (KRR)
 
Go Faster With Native Compilation
Go Faster With Native CompilationGo Faster With Native Compilation
Go Faster With Native CompilationPGConf APAC
 
Architectural anti-patterns for data handling
Architectural anti-patterns for data handlingArchitectural anti-patterns for data handling
Architectural anti-patterns for data handlingGleicon Moraes
 
Next.ml Boston: Data Science Dev Ops
Next.ml Boston: Data Science Dev OpsNext.ml Boston: Data Science Dev Ops
Next.ml Boston: Data Science Dev OpsEric Chiang
 
Fnt Software Solutions Pvt Ltd Placement Papers - PHP Technology
Fnt Software Solutions Pvt Ltd Placement Papers - PHP TechnologyFnt Software Solutions Pvt Ltd Placement Papers - PHP Technology
Fnt Software Solutions Pvt Ltd Placement Papers - PHP Technologyfntsofttech
 
Lessons Learned While Scaling Elasticsearch at Vinted
Lessons Learned While Scaling Elasticsearch at VintedLessons Learned While Scaling Elasticsearch at Vinted
Lessons Learned While Scaling Elasticsearch at VintedDainius Jocas
 

Similar to Computer Science Concepts for WordPress Developers (20)

Questions On The Code And Core Module
Questions On The Code And Core ModuleQuestions On The Code And Core Module
Questions On The Code And Core Module
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
Building an Analytic Extension to MySQL with ClickHouse and Open Source
Building an Analytic Extension to MySQL with ClickHouse and Open SourceBuilding an Analytic Extension to MySQL with ClickHouse and Open Source
Building an Analytic Extension to MySQL with ClickHouse and Open Source
 
Building an Analytic Extension to MySQL with ClickHouse and Open Source.pptx
Building an Analytic Extension to MySQL with ClickHouse and Open Source.pptxBuilding an Analytic Extension to MySQL with ClickHouse and Open Source.pptx
Building an Analytic Extension to MySQL with ClickHouse and Open Source.pptx
 
Simplified Data Processing On Large Cluster
Simplified Data Processing On Large ClusterSimplified Data Processing On Large Cluster
Simplified Data Processing On Large Cluster
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Microsoft azure data fundamentals (dp 900) practice tests 2022
Microsoft azure data fundamentals (dp 900) practice tests 2022Microsoft azure data fundamentals (dp 900) practice tests 2022
Microsoft azure data fundamentals (dp 900) practice tests 2022
 
Martin Chapman: Research Overview, 2017
Martin Chapman: Research Overview, 2017Martin Chapman: Research Overview, 2017
Martin Chapman: Research Overview, 2017
 
Using R on Netezza
Using R on NetezzaUsing R on Netezza
Using R on Netezza
 
Understanding and building big data Architectures - NoSQL
Understanding and building big data Architectures - NoSQLUnderstanding and building big data Architectures - NoSQL
Understanding and building big data Architectures - NoSQL
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01
 
Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2
 
Go Faster With Native Compilation
Go Faster With Native CompilationGo Faster With Native Compilation
Go Faster With Native Compilation
 
Architectural anti-patterns for data handling
Architectural anti-patterns for data handlingArchitectural anti-patterns for data handling
Architectural anti-patterns for data handling
 
Next.ml Boston: Data Science Dev Ops
Next.ml Boston: Data Science Dev OpsNext.ml Boston: Data Science Dev Ops
Next.ml Boston: Data Science Dev Ops
 
Why Our Code Smells
Why Our Code SmellsWhy Our Code Smells
Why Our Code Smells
 
Fnt Software Solutions Pvt Ltd Placement Papers - PHP Technology
Fnt Software Solutions Pvt Ltd Placement Papers - PHP TechnologyFnt Software Solutions Pvt Ltd Placement Papers - PHP Technology
Fnt Software Solutions Pvt Ltd Placement Papers - PHP Technology
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Lessons Learned While Scaling Elasticsearch at Vinted
Lessons Learned While Scaling Elasticsearch at VintedLessons Learned While Scaling Elasticsearch at Vinted
Lessons Learned While Scaling Elasticsearch at Vinted
 

Recently uploaded

Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingShane Coughlan
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencessuser9e7c64
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 

Recently uploaded (20)

Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conference
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 

Computer Science Concepts for WordPress Developers

  • 2. My name is Taylor Lovett - Senior Strategic Web Engineer at 10up - Core Contributor - Plugin Author (Safe Redirect Manager) - Plugin Contributor - BS in Computer Science from the University of Maryland, College Park
  • 3. What is Computer Science? - It can mean a lot of things. It is really the study of computational theory, computer software, and hardware.
  • 4. Theory of Computation - General Mathematics (Calculus, linear algebra, general computational theory, statistics) - Algorithms (a method to solve a problem) - Data structures (which data structure will allow us to access our data the quickest?) - Graph theory
  • 5. Computer Software - Programming techniques and design patterns (i.e a singleton class) - Concurrent design patterns (data races) - Mobile software development - Operating system software - Web development - Databases - Networking - Benchmarking
  • 6. Computer Hardware - Motherboards - Memory types (solid state, RAM, etc.) - Benchmarking (processor execution time) - Pipelining - Processors
  • 7. Big-Oh Notation - "Big O notation is used to classify algorithms by how they respond (e.g., in their processing time or working space requirements) to changes in input size." -- Wikipedia - Very useful to describe how performant your code may or may not be - Big-Oh usually describes the upper bound of a function (worst-case)
  • 8. Big-Oh Notation (cont.) - Big-Oh notation is concerned with measuring the rate of growth of the amount of processing that your code might do on an unknown input size - In Big-Oh we are only concerned about how a our code performs as the input size approaches infinity. Mathematically speaking, this means we only care about the highest order term: i.e. O(3n2 + 5n) = O(n2) since as n approaches infinity the only thing that matters is the n2
  • 9. Let's look at some examples!
  • 10. // $fruits contains a non-empty array of strings function contains_orange( $fruits = array() ) { for ( $i = 0; $i < count( $fruits ); $i++ ) { if ( 'orange' == $fruits[$i] ) return true; } return false; } Best Case Scenario: Loop executes once, orange is found, and it returns. Worst Case Scenario: Loop executes n times (where n is the number of elements in $fruits) Performance: contains_orange() is in O(n)
  • 11. Remember! - With Big-Oh we are only concerned with what happens in the worst case. Sometimes knowing what happens in the best case is useful, but we are mostly worried about the performance hit our code could take in the worst possible situation.
  • 12. // $fruits contains a non-empty array of strings. For educational // purposes, $fruits is guaranteed to have at least one duplicate. function contains_duplicate_fruit( $fruits = array() ) { for ( $i = 0; $i < count( $fruits ); $i++ ) { for ( $z = 0; $z < count( $fruits ); $z++ ) { if ( $i != $z && $fruits[$z] == $fruits[$i] ) return true; } } return false; } What does everyone think?
  • 13. Best Case Scenario: Outer loop executes once, inner loop executes twice, duplicate is found, function returns Worst Case Scenario: Outer loop executes n - 1 times (where n is the size of $fruits), inner loop executes n times for each outer loop execution... n * (n -1) = n2 - n Performance: contains_duplicate_fruit is in O(n2 - n) = O(n2)
  • 14. An important reminder - We dropped the (-n) from our final Big-Oh evaluation because, as n approaches infinity, n2 dominates and (-n) becomes insignificant.
  • 15. But seriously... How is this useful?
  • 16. Big-Oh Notation and Databases - Big-Oh notation is used a lot in conjunction with SQL operations. - We've all heard that indexing a column in MySQL makes search on that column faster. - But why? What does that actually mean?
  • 17. MySQL Indexes - An index is a data structure that speeds up search time for information. - Without an index, searching for a specific column value is O(n) because in the worst case scenario every single row in the table must be examined.
  • 18. MySQL Indexes - When a column is indexed, MySQL takes the data across all of the rows in that column and stores references to that data in a B-tree (this structure is used for the majority of index types). - A B-tree is just what it sounds like: A tree of data that speeds up search time. The worst case scenario for the amount of items to be processed in a B-tree is log n. A log is a mathematical function such that: n2 > n > log n http://en.wikipedia.org/wiki/B-tree
  • 19. Post Meta Queries - The full Big-Oh analysis of a post meta query is pretty complex because of the join operation and therefore is outside the scope of this talk. - For our purposes, searching for posts based on a meta key is O(n) where n is the number of posts that have that key. - Let's frame this in terms of featured posts. Featured posts refers to the situation where a website needs to mark select posts as featured and query for them.
  • 20. Featured Posts Solution #1 On post update: if ( isset( $_POST['meta_box_feature'] ) ) update_post_meta( $post_id, 'featured', 1 ); else update_post_meta( $post_id, 'featured', 0 ); Query: $args = array( 'meta_key' => 'featured', 'meta_value' => 1, ); $featured_posts = new WP_Query( $args );
  • 21. Solution #1 Analysis - Using this code, every time a post is saved, it will have post meta attached to it such that 'featured' = 1 or 0. This will create a ton of unnecessary post meta rows. - Remember searching for posts based on a meta key is O(n) where n is the number of posts that have that key. Therefore saving meta when a post is not featured is not only unnecessary but will really slow us down. This would result in O(m) performance where m is the number of posts!
  • 22. Featured Posts Solution #2 On post update: if ( isset( $_POST['meta_box_feature'] ) ) update_post_meta( $post_id, 'featured', 1 ); else delete_post_meta( $post_id, 'featured' ); Query: $args = array( 'meta_key' => 'featured', 'meta_value' => 1, ); $featured_posts = new WP_Query( $args );
  • 23. Solution #2 Analysis - This solution is a major improvement over our first one. This will result in O(n) search time where n is the number of featured posts. - However, we can still do better.
  • 24. Featured Posts Solution #3 Let's create a tag called 'featured' and attach it to all our featured posts: On init: $args = array( ... ); register_taxonomy( 'featured', 'post', $args ); Query: $args = array( 'post_tag' => 'featured' ); $featured_posts = new WP_Query( $args );
  • 25. Solution #3 Analysis - For our purposes, searching for posts based on a tag is O(log n) since there is an index on the tag id column. The full Big-Oh analysis of our tag solution is pretty complex due to SQL join operations and therefore is beyond the scope of this talk.
  • 26. Concurrency - In Computer Science concurrency is a property describing the event where multiple computations are executed simultaneously, sometimes interacting with each other.
  • 27. Concurrency - With concurrent programming we can, among other things, force each core in a computer to process a piece of a larger problem or handle separate tasks. This is extremely powerful. - When not properly account for, Concurrency can sometimes result in unexpected bugs that are difficult to reproduce.
  • 28. Concurrency in WordPress - Concurrency takes a slightly different form in WordPress. We don't solve problems by starting new threads/processes. However, since behind the scenes servers can run multiple processes at the same time and thus multiple users can execute the same code simultaneously, issues surrounding concurrency can arise.
  • 29. Tracking Postviews in WordPress - A common request in WordPress is to display the number of views for each post on the frontend. - There are many different ways to approach this problem; the most common is to increment an integer stored in post meta each time a post is viewed, then to display this number for each post. - This implementation can lead to data races.
  • 30. Here is the code that executes on each post request $views = get_post_meta( $id, 'views', true ); $views++; update_post_meta( $id, 'views', $views );
  • 31. Data Races - A data race is the situation where two or more threads access a shared memory location, at least one of those accesses is a write, and the order of the accesses is unknown (meaning there are no explicit locking mechanisms used). - Think of each page request as a thread on the server. If two users request a post at the same time, a data race for pageviews occurs since both accesses are writing to the postmeta table.
  • 32. A Possible Ordering of Events Code executed for User A is in red and User B in blue $views = get_post_meta( $id, 'views', true ); // $views = 0 $views++; // $views = 1 update_post_meta( $id, 'views', $views ); // _views = 1 $views = get_post_meta( $id, 'views', true ); // $views = 1 $views++; // $views = 2 update_post_meta( $id, 'views', $views ); // _views = 2 In this ordering of events, $views ends up with a value of 2 which is what we want. However, these events could occur in any order...
  • 33. Another Ordering of Events $views = get_post_meta( $id, 'views', true ); // $views = 0 $views = get_post_meta( $id, 'views', true ); // $views = 0 $views++; // $views = 1 $views++; // $views = 1 update_post_meta( $id, 'views', $views ); // _views = 1 update_post_meta( $id, 'views', $views ); // _views = 1 In this ordering of events, $views ends up with a value of 1 which is NOT what we want.
  • 35. Solution to Pageview Problem? Solution 1: Jetpack plugin. We can install Jetpack and leverage it's stats API to query information on specific posts. Solution 2: Google Analytics. Using a websites Google Analytics account, we can set custom variables on a post-to-post basis and query the API based on those variables.