Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero...

49
Programação Genérica Levando a Abstração ao Limite Fabio Galuppo, M.Sc. http://fabiogaluppo.com e http://simplycpp.com/ [email protected] @FabioGaluppo Microsoft MVP Visual Studio and Development Technologies https://mvp.microsoft.com/en-us/PublicProfile/9529 http://bit.ly/prog_gen_qconsp_2016

Transcript of Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero...

Page 1: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Programação Genérica Levando a Abstração ao Limite

Fabio Galuppo, M.Sc. http://fabiogaluppo.com e http://simplycpp.com/

[email protected]

@FabioGaluppo

Microsoft MVP Visual Studio and Development Technologies https://mvp.microsoft.com/en-us/PublicProfile/9529

http://bit.ly/prog_gen_qconsp_2016

Page 2: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fabio Razzo Galuppo, M.Sc. Novembro 1973

• Mestrado em Engenharia Elétrica (Universidade Presbiteriana Mackenzie)

– Ciência da Computação - Inteligência Artificial

• Por mais de 10 anos premiado com Microsoft MVP em Visual C++

• Engenheiro de Software (Programador)

• Matemática Aplicada

• Linguagens de programação prediletas: – C++

– F#

– Haskell

• Rock’n’Roll – E boa música em geral

• http://fabiogaluppo.com

• https://twitter.com/FabioGaluppo

• https://github.com/fabiogaluppo

• http://simplycpp.com

Page 3: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Templates e Generics Polimorfismo Paramétrico

https://docs.oracle.com/javase/tutorial/java/generics/why.html

Page 4: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Templates e Generics Polimorfismo Paramétrico e Restrições

https://msdn.microsoft.com/en-us/library/d5x73970.aspx

class Dictionary<TKey,TValue> : IDictionary<TKey,TValue> where TKey: IComparable<TKey> where TValue: IKeyProvider<TKey>, IPersistable, new() { public void Add(TKey key, TValue value) { ... } }

Page 5: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks. It supports the use

of light-weight abstraction for resource-constrained and often mission-critical infrastructure applications. The aim is to allow a programmer to work at the highest feasible level of abstraction by providing

• A simple and direct mapping to hardware

• Zero-overhead abstraction mechanisms

• In general, C++ implementations obey the zero-overhead principle: What you don’t use, you don’t pay for” – Bjarne Stroustrup (http://www.stroustrup.com/ETAPS-corrected-draft.pdf)

http://blog.rust-lang.org/2015/05/11/traits.html

Page 6: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

https://twitter.com/FabioGaluppo/status/705191871832784896

Quando o contexto é sobre desempenho

• Não use abstração (! ou ?)

Page 7: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

ZERO-COST ABSTRACTIONS Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Page 8: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64

b com valor 299

b com valor 199

otimizado

não otimizado

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Page 9: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64

otimizado

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Page 10: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

otimizado

before:25270 14008 47948 28218 52679 29321 20101 7968 31023 36364 62B6 36B8

after :14008 25270 28218 47948 29321 52679 7968 20101 36364 31023

my_swap

Page 11: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

otimizado

Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64

No contexto da chamada da função swap_from_array_no_raw_loops, o corpo da função utilizada encontra-se inline

Page 12: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

O que é Programação Genérica?

Datatype-Generic Programming (Jeremy Gibbons)

Generic Programming (David Musser, Alexander Stepanov)

Page 13: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Abstraindo do Concreto

Datatype-Generic Programming (Jeremy Gibbons)

Page 14: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Abstraindo do Concreto

Datatype-Generic Programming (Jeremy Gibbons)

Page 15: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

ABSTRACTING FROM CONCRETE Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Page 17: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Então, o que é mesmo Programação Genérica?

http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt1.pdf

Page 19: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Álgebra Abstrata e Estruturas Algébricas

http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt2.pdf

Page 20: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

ORDERING Strict Totally and Strict Weak

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Page 21: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

https://github.com/fabiogaluppo/samples/blob/master/events/qconsp2016/code/cpp/ordering_sample.hpp

Page 22: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Estrutura Algébrica: Grupo

• Grupo implica: • Operação Binária Associativa • Inversa e Cancelamento • Elemento de Identidade

http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt2.pdf

Page 23: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

GROUP https://en.wikipedia.org/wiki/Group_(mathematics)

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Page 24: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

BINARY ASSOCIATIVE OPERATION

GROUP AXIOMS: • CLOSURE • ASSOCIATIVITY • IDENTITY • INVERTIBILITY

Page 26: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

ASSOCIATIVITY Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Page 27: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

https://github.com/fabiogaluppo/samples/blob/master/events/qconsp2016/code/cpp/associativity_sample.hpp

Page 28: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

http://www.fssnip.net/7OM

Page 30: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Abstração, revisitada

http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt3.pdf

Page 32: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Objetos

http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt3.pdf

Page 33: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Concepts

http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt3.pdf

Page 35: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

O Conceito de Tipo Regular

http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt3.pdf

• Operations Requirements

• Semantic Requirements

• Space/Time Complexity Requirements • Each required operation must be no worse than linear in the area of the object

Page 36: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

REGULAR TYPE

Copy Constructor Copy Assignment Equality (and Inequality) Destruction Default Constructor

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Page 37: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Equality/Inequality

Ordering

https://github.com/fabiogaluppo/samples/blob/master/events/qconsp2016/code/cpp/regular_type_sample.hpp

Page 38: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

https://github.com/fabiogaluppo/samples/blob/master/events/qconsp2016/code/csharp/RegularTypeSampleProgram.cs

Page 39: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Standard Template Library (STL)

http://www.sgi.com/tech/stl/stl_introduction.html

http://www.cplusplus.com/reference/stl/

http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt2.pdf

Page 40: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Iterators

http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt3.pdf

Page 41: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Categoria de Iterators

http://www.cplusplus.com/reference/iterator/

array doubly‐linked list singly‐linked list

input stream (cin)

output stream (cout)

Page 43: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Policy-based Design

https://en.wikipedia.org/wiki/Policy-based_design

Page 44: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

POLICY-BASED DESIGN Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Page 45: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

https://github.com/fabiogaluppo/samples/blob/master/events/qconsp2016/code/cpp/policy_sample.hpp

Page 47: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

• “Programming is an iterative process: studying useful problems, finding efficient algorithms for them, distilling the concepts underlying the algorithms, and organizing the concepts and algorithms into a coherent mathematical theory. Each new discovery adds to the permanent body of knowledge, but each has its limitations.”

AlexFest: Paul McJones - The Concept of Concept

• “STL is intended to be an example. An example of how you

code, not the beginning, not an end, it's an example.” CppCon 2015: Sean Parent "Better Code: Data Structures"

• “Abstração é essencial. Você não precisa abrir mão dela para alto desempenho, basta fazê-la da forma correta, de preferência com a linguagem de programação certa para isso.” – Fabio Galuppo, QCon SP 2016

Final Thought

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Page 48: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Fab

io G

alu

pp

o -

htt

p:/

/mem

ber

.acm

.org

/~fa

bio

galu

pp

o -

fab

ioga

lup

po

@ac

m.o

rg

Se quiser saber mais sobre: C++

Programação Genérica Iterators

Policy-based Design Algoritmos

visite:

www.simplycpp.com

http://www.simplycpp.com

Page 49: Programação Genérica aplicada - levando a abstração ao limite · o-rg Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks.It supports

Programação Genérica Levando a Abstração ao Limite

Fabio Galuppo, M.Sc. http://fabiogaluppo.com e http://simplycpp.com/

[email protected]

@FabioGaluppo

Microsoft MVP Visual Studio and Development Technologies https://mvp.microsoft.com/en-us/PublicProfile/9529

http://bit.ly/prog_gen_qconsp_2016