Put attr and just data managment into Slot class

Added gen member to keep tracing working, but this is temporary.
diff --git a/src/Justifier.cpp b/src/Justifier.cpp
index 4ea1329..f5732d6 100644
--- a/src/Justifier.cpp
+++ b/src/Justifier.cpp
@@ -40,7 +40,7 @@
 typename std::result_of<Callable()>::type
 Segment::subsegment(SlotBuffer::const_iterator first, SlotBuffer::const_iterator last, Callable body)
 {
-    SlotBuffer sub_slots(m_srope.num_user_attrs());
+    SlotBuffer sub_slots/*(m_srope.num_user_attrs(), m_srope.num_just_levels())*/;
     sub_slots.splice(sub_slots.end(), m_srope, first, last);
     m_srope.swap(sub_slots);
 
@@ -244,7 +244,7 @@
 
 SlotBuffer::iterator Segment::addLineEnd(SlotBuffer::iterator pos)
 {
-    auto eSlot = slots().insert(pos, Slot());
+    auto eSlot = slots().insert(pos, Slot(numAttrs()));
     if (eSlot == slots().end()) return eSlot;
 
     const uint16 gid = silf()->endLineGlyphid();
diff --git a/src/Segment.cpp b/src/Segment.cpp
index 0461398..f06a778 100644
--- a/src/Segment.cpp
+++ b/src/Segment.cpp
@@ -43,9 +43,7 @@
 using namespace graphite2;
 
 Segment::Segment(size_t numchars, const Face* face, uint32 script, int textDir)
-: m_srope(face->chooseSilf(script)->numUser()),
-// TODO:rm m_freeSlots(NULL),
-  m_freeJustifies(NULL),
+: //m_srope(face->chooseSilf(script)->numUser(), face->chooseSilf(script)->numJustLevels()),
   m_charinfo(new CharInfo[numchars]),
   m_collisions(NULL),
   m_face(face),
@@ -63,8 +61,6 @@
 
 Segment::~Segment()
 {
-    for (JustifyRope::iterator i = m_justifies.begin(); i != m_justifies.end(); ++i)
-        free(*i);
     delete[] m_charinfo;
     free(m_collisions);
 }
@@ -78,14 +74,14 @@
     m_charinfo[id].base(coffset);
     m_charinfo[id].breakWeight(glyph ? glyph->attrs()[m_silf->aBreak()] : 0);
 
-    Slot aSlot;
-    aSlot.child(NULL);
+    slots().push_back(Slot(numAttrs()));
+    auto & aSlot = slots().back();
+    aSlot.child(nullptr);
     aSlot.setGlyph(*this, gid, glyph);
     aSlot.originate(id);
     aSlot.before(id);
     aSlot.after(id);
 
-    slots().push_back(aSlot);
     if (glyph && m_silf->aPassBits())
         m_passBits &= glyph->attrs()[m_silf->aPassBits()]
                     | (m_silf->numPasses() > 16 ? (glyph->attrs()[m_silf->aPassBits() + 1] << 16) : 0);
@@ -104,37 +100,6 @@
     m_srope.freeSlot(i);
 }
 
-SlotJustify *Segment::newJustify()
-{
-    if (!m_freeJustifies)
-    {
-        const size_t justSize = SlotJustify::size_of(m_silf->numJustLevels());
-        byte *justs = grzeroalloc<byte>(justSize * m_bufSize);
-        if (!justs) return NULL;
-        for (ptrdiff_t i = m_bufSize - 2; i >= 0; --i)
-        {
-            SlotJustify *p = reinterpret_cast<SlotJustify *>(justs + justSize * i);
-            SlotJustify *next = reinterpret_cast<SlotJustify *>(justs + justSize * (i + 1));
-            p->next = next;
-        }
-        m_freeJustifies = (SlotJustify *)justs;
-        m_justifies.push_back(m_freeJustifies);
-    }
-    SlotJustify *res = m_freeJustifies;
-    m_freeJustifies = m_freeJustifies->next;
-    res->next = NULL;
-    return res;
-}
-
-void Segment::freeJustify(SlotJustify *aJustify)
-{
-    int numJust = m_silf->numJustLevels();
-    if (m_silf->numJustLevels() <= 0) numJust = 1;
-    aJustify->next = m_freeJustifies;
-    memset(aJustify->values, 0, numJust*SlotJustify::NUMJUSTPARAMS*sizeof(int16));
-    m_freeJustifies = aJustify;
-}
-
 // reverse the slots but keep diacritics in their same position after their bases
 void Segment::reverseSlots()
 {
diff --git a/src/ShapingContext.cpp b/src/ShapingContext.cpp
index 48135a4..6cda27c 100644
--- a/src/ShapingContext.cpp
+++ b/src/ShapingContext.cpp
@@ -33,7 +33,7 @@
 ShapingContext::ShapingContext(Segment & seg, uint8 direction, size_t maxSize)
 : segment(seg),
   dbgout(seg.getFace() ? seg.getFace()->logger() : nullptr),
-  in(seg.slots().num_user_attrs()),
+  //in(seg.slots().num_user_attrs(), seg.slots().num_just_levels()),
   dir(direction),
   _max_size(int(maxSize)),
   _precontext(0),
diff --git a/src/Slot.cpp b/src/Slot.cpp
index c80f4af..25b0976 100644
--- a/src/Slot.cpp
+++ b/src/Slot.cpp
@@ -36,47 +36,182 @@
 
 using namespace graphite2;
 
-Slot::Slot(int16 *user_attrs) :
-    m_glyphid(0), m_realglyphid(0), m_original(0), m_before(0), m_after(0),
-    m_index(0), m_parent(NULL), m_child(NULL), m_sibling(NULL),
-    m_position(0, 0), m_shift(0, 0), m_advance(0, 0),
-    m_attach(0, 0), m_with(0, 0), m_just(0.),
-    m_flags(0), m_attLevel(0), m_bidiCls(-1), m_bidiLevel(0),
-    m_userAttr(user_attrs), m_justs(NULL)
-{
+auto Slot::attributes::operator = (attributes const & rhs) -> attributes & {
+    if (this != &rhs) {
+        reserve(rhs.num_attrs(), rhs.num_justs());
+        if (!rhs.is_inline() && external) {
+            auto const sz = external->n_attrs + external->n_justs*NUMJUSTPARAMS + 1;
+            memcpy(external, rhs.external, sz);
+        } else local = rhs.local;
+    }
+    return *this;
 }
 
-// take care, this does not copy any of the GrSlot pointer fields
-void Slot::set(const Slot & orig, int charOffset, size_t sizeAttr, size_t justLevels, size_t numChars)
+auto Slot::attributes::operator = (attributes && rhs) noexcept -> attributes & {
+    if (!is_inline()) free(external);
+    external = rhs.external;
+    rhs.external = nullptr;
+    return *this;
+}
+
+void Slot::attributes::reserve(size_t target_num_attrs, size_t target_num_justs) {
+    assert(target_num_attrs <  256);
+    assert(target_num_justs <  256);
+
+    if (num_attrs() >= target_num_attrs 
+        && num_justs() >= target_num_justs)
+        return;
+
+    if (target_num_justs > 0 
+        || target_num_attrs > sizeof local.data/sizeof *local.data)
+    {
+        auto const sz = target_num_attrs + target_num_justs*NUMJUSTPARAMS + 1;
+
+        if (is_inline()) {  // Convert to non-inline form.
+            auto box = reinterpret_cast<decltype(external)>(grzeroalloc<int16_t>(sz));
+            if (box) {
+                if (local.size) memcpy(box->data, local.data, local.size*sizeof *local.data);
+                external = box;
+                external->n_attrs = target_num_attrs-1;
+                external->n_justs = target_num_justs;
+            }
+        } else { // Grow the existing buffer.
+            external = static_cast<decltype(external)>(realloc(external, sz*sizeof *local.data));
+            external->n_attrs = target_num_attrs-1;
+            external->n_justs = target_num_justs;
+        }
+    }
+    else local.size = target_num_attrs;
+}
+
+
+void Slot::init_just_infos(Segment const & seg)
 {
-    // leave m_next and m_prev unchanged
-    m_glyphid = orig.m_glyphid;
-    m_realglyphid = orig.m_realglyphid;
-    m_original = orig.m_original + charOffset;
-    if (charOffset + int(orig.m_before) < 0)
-        m_before = 0;
-    else
-        m_before = orig.m_before + charOffset;
-    if (charOffset <= 0 && orig.m_after + charOffset >= numChars)
-        m_after = int(numChars) - 1;
-    else
-        m_after = orig.m_after + charOffset;
-    m_parent = NULL;
-    m_child = NULL;
-    m_sibling = NULL;
-    m_position = orig.m_position;
-    m_shift = orig.m_shift;
-    m_advance = orig.m_advance;
-    m_attach = orig.m_attach;
-    m_with = orig.m_with;
-    m_flags = orig.m_flags;
-    m_attLevel = orig.m_attLevel;
-    m_bidiCls = orig.m_bidiCls;
-    m_bidiLevel = orig.m_bidiLevel;
-    if (m_userAttr && orig.m_userAttr)
-        memcpy(m_userAttr, orig.m_userAttr, sizeAttr * sizeof(*m_userAttr));
-    if (m_justs && orig.m_justs)
-        memcpy(m_justs, orig.m_justs, SlotJustify::size_of(justLevels));
+    auto const target_num_justs = seg.silf()->numJustLevels();
+
+    for (int i = target_num_justs - 1; i >= 0; --i)
+    {
+        Justinfo *justs = seg.silf()->justAttrs() + i;
+        int16 *v = m_attrs.just_info() + i * NUMJUSTPARAMS;
+        v[0] = seg.glyphAttr(gid(), justs->attrStretch());
+        v[1] = seg.glyphAttr(gid(), justs->attrShrink());
+        v[2] = seg.glyphAttr(gid(), justs->attrStep());
+        v[3] = seg.glyphAttr(gid(), justs->attrWeight());
+    }
+}
+
+Slot::Slot(size_t num_attrs) :
+    m_parent{nullptr}, 
+    m_child{nullptr}, 
+    m_sibling{nullptr},
+    m_position{0, 0}, 
+    m_shift{0, 0}, 
+    m_advance{0, 0},
+    m_attach{0, 0}, 
+    m_with{0, 0}, 
+    m_attrs{num_attrs},
+    m_just{0},
+    m_original{0},
+    m_before{0},
+    m_after{0},
+    m_index{0},
+    m_glyphid{0}, 
+    m_realglyphid{0}, 
+    m_attLevel{0}, 
+    m_bidiLevel{0},
+    m_bidiCls{-1},
+    m_flags{false, false, false, false, false, false}
+{
+    assert(sizeof m_flags == 1);
+}
+
+Slot::Slot(Slot && rhs) noexcept
+: m_parent{nullptr},
+  m_child{nullptr},
+  m_sibling{nullptr},
+  m_position{std::move(rhs.m_position)},
+  m_shift{std::move(rhs.m_shift)},
+  m_advance{std::move(rhs.m_advance)},
+  m_attach{std::move(rhs.m_attach)},
+  m_with{std::move(rhs.m_with)},
+  m_attrs{std::move(rhs.m_attrs)},
+  m_just{std::move(rhs.m_just)},
+  m_original{std::move(rhs.m_original)},
+  m_before{std::move(rhs.m_before)},
+  m_after{std::move(rhs.m_after)},
+  m_index{std::move(rhs.m_index)},
+  m_glyphid{std::move(rhs.m_glyphid)},
+  m_realglyphid{std::move(rhs.m_realglyphid)},
+  m_attLevel{std::move(rhs.m_attLevel)},
+  m_bidiLevel{std::move(rhs.m_bidiLevel)},
+  m_bidiCls{std::move(rhs.m_bidiCls)},
+  m_flags{std::move(rhs.m_flags)}
+#if !defined GRAPHITE2_NTRACING
+  , m_gen{std::move(rhs.m_gen)}
+#endif
+{}
+
+Slot & Slot::operator = (Slot && rhs) noexcept 
+{
+    if (this != &rhs)
+    {
+        m_parent = nullptr;
+        m_child = nullptr;
+        m_sibling = nullptr;
+        m_attrs = std::move(rhs.m_attrs);
+        m_glyphid = std::move(rhs.m_glyphid);
+        m_realglyphid = std::move(rhs.m_realglyphid);
+        m_original = std::move(rhs.m_original);
+        m_before = std::move(rhs.m_before);
+        m_after = std::move(rhs.m_after);
+        m_index = std::move(rhs.m_index);
+        m_position = std::move(rhs.m_position);
+        m_shift = std::move(rhs.m_shift);
+        m_advance = std::move(rhs.m_advance);
+        m_attach = std::move(rhs.m_attach);
+        m_with = std::move(rhs.m_with);
+        m_just = std::move(rhs.m_just);
+        m_flags = std::move(rhs.m_flags);
+        m_attLevel = std::move(rhs.m_attLevel);
+        m_bidiCls = std::move(rhs.m_bidiCls);
+        m_bidiLevel = std::move(rhs.m_bidiLevel);
+#if !defined GRAPHITE2_NTRACING
+        m_gen = std::move(rhs.m_gen);
+#endif
+    }
+    return *this;
+}
+
+Slot & Slot::operator = (Slot const & rhs) noexcept
+{
+    if (this != &rhs)
+    {
+        m_parent = nullptr;
+        m_child = nullptr;
+        m_sibling = nullptr;
+        m_attrs = rhs.m_attrs;
+        m_glyphid = rhs.m_glyphid;
+        m_realglyphid = rhs.m_realglyphid;
+        m_original = rhs.m_original;
+        m_before = rhs.m_before;
+        m_after = rhs.m_after;
+        m_index = rhs.m_index;
+        m_position = rhs.m_position;
+        m_shift = rhs.m_shift;
+        m_advance = rhs.m_advance;
+        m_attach = rhs.m_attach;
+        m_with = rhs.m_with;
+        m_just = rhs.m_just;
+        m_flags = rhs.m_flags;
+        m_attLevel = rhs.m_attLevel;
+        m_bidiCls = rhs.m_bidiCls;
+        m_bidiLevel = rhs.m_bidiLevel;
+#if !defined GRAPHITE2_NTRACING
+        m_gen = rhs.m_gen;
+#endif
+    }
+
+    return *this;
 }
 
 void Slot::update(int /*numGrSlots*/, int numCharInfo, Position &relpos)
@@ -227,7 +362,7 @@
     case gr_slatJWidth:     return int(m_just);
     case gr_slatUserDefnV1: subindex = 0; GR_FALLTHROUGH;
       // no break
-    case gr_slatUserDefn :  return subindex < seg.numAttrs() ?  m_userAttr[subindex] : 0;
+    case gr_slatUserDefn :  return subindex < m_attrs.num_attrs() ? m_attrs.user_attributes()[subindex] : 0;
     case gr_slatSegSplit :  return seg.charinfo(m_original)->flags() & 3;
     case gr_slatBidiLevel:  return m_bidiLevel;
     case gr_slatColFlags :		{ SlotCollision *c = seg.collisionInfo(*this); return c ? c->flags() : 0; }
@@ -344,7 +479,7 @@
     case gr_slatMeasureEol :    break;
     case gr_slatJWidth :    just(value); break;
     case gr_slatSegSplit :  seg.charinfo(m_original)->addflags(value & 3); break;
-    case gr_slatUserDefn :  m_userAttr[subindex] = value; break;
+    case gr_slatUserDefn :  assert(subindex < m_attrs.num_attrs()); m_attrs.user_attributes()[subindex] = value; break;
     case gr_slatColFlags :  {
         SlotCollision *c = seg.collisionInfo(*this);
         if (c)
@@ -377,8 +512,8 @@
 {
     if (level && level >= seg.silf()->numJustLevels()) return 0;
 
-    if (m_justs)
-        return m_justs->values[level * SlotJustify::NUMJUSTPARAMS + subindex];
+    if (has_justify())
+        return m_attrs.just_info()[level * Slot::NUMJUSTPARAMS + subindex];
 
     if (level >= seg.silf()->numJustLevels()) return 0;
     Justinfo *jAttrs = seg.silf()->justAttrs() + level;
@@ -396,14 +531,12 @@
 void Slot::setJustify(Segment & seg, uint8 level, uint8 subindex, int16 value)
 {
     if (level && level >= seg.silf()->numJustLevels()) return;
-    if (!m_justs)
-    {
-        SlotJustify *j = seg.newJustify();
-        if (!j) return;
-        j->LoadSlot(*this, seg);
-        m_justs = j;
+    if (!has_justify()) {
+        m_attrs.reserve(m_attrs.num_attrs(), std::max(1ul,seg.silf()->numJustLevels()));
+        init_just_infos(seg);
     }
-    m_justs->values[level * SlotJustify::NUMJUSTPARAMS + subindex] = value;
+    
+    m_attrs.just_info()[level * Slot::NUMJUSTPARAMS + subindex] = value;
 }
 
 bool Slot::child(Slot *ap)
@@ -491,19 +624,6 @@
     if (m_sibling) m_sibling->floodShift(adj, depth + 1);
 }
 
-void SlotJustify::LoadSlot(const Slot & s, const Segment & seg)
-{
-    for (int i = seg.silf()->numJustLevels() - 1; i >= 0; --i)
-    {
-        Justinfo *justs = seg.silf()->justAttrs() + i;
-        int16 *v = values + i * NUMJUSTPARAMS;
-        v[0] = seg.glyphAttr(s.gid(), justs->attrStretch());
-        v[1] = seg.glyphAttr(s.gid(), justs->attrShrink());
-        v[2] = seg.glyphAttr(s.gid(), justs->attrStep());
-        v[3] = seg.glyphAttr(s.gid(), justs->attrWeight());
-    }
-}
-
 Slot * Slot::nextInCluster(const Slot *s) const
 {
     Slot *base;
diff --git a/src/SlotBuffer.cpp b/src/SlotBuffer.cpp
index d48b71f..2cbb0c2 100644
--- a/src/SlotBuffer.cpp
+++ b/src/SlotBuffer.cpp
@@ -45,15 +45,16 @@
 }
 
 
-SlotBuffer::SlotBuffer(size_t num_user)
-: _size{0},
-  _attrs_size{num_user}
+SlotBuffer::SlotBuffer(/*size_t num_user, size_t num_justs*/)
+: _size{0}
+//   _attrs_size{num_user},
+//   _justs_size{num_justs}
 {
 }
 
 SlotBuffer::SlotBuffer(SlotBuffer && rhs)
-: _size{rhs._size},
-  _attrs_size{rhs._attrs_size}
+: _size{rhs._size}
+//   _attrs_size{rhs._attrs_size}
 {
     if (!rhs.empty())
         _head.splice(*rhs._head._next, *rhs._head._prev);
@@ -97,10 +98,10 @@
 SlotBuffer::iterator SlotBuffer::insert(const_iterator pos, value_type const & slot)
 {
     assert(pos._p);
-    auto node = _allocate_node();
+    auto node = _allocate_node(Slot());
     if (!node) return end();
 
-    node->_value.set(slot, 0, _attrs_size, 0, slot.after()+1);
+    node->_value = slot;
     node->link(*const_cast<_node_linkage *>(pos._p));
 
     ++_size;
@@ -110,10 +111,9 @@
 SlotBuffer::iterator SlotBuffer::insert(const_iterator pos, value_type && slot)
 {
     assert(pos._p);
-    auto node = _allocate_node();
+    auto node = _allocate_node(std::move(slot));
     if (!node) return end();
 
-    node->_value.set(slot, 0, _attrs_size, 0, slot.after()+1);
     node->link(*const_cast<_node_linkage *>(pos._p));
 
     ++_size;
@@ -175,16 +175,16 @@
     _garbage._next = _garbage._prev = &_garbage;
 }
 
-auto SlotBuffer::_allocate_node() -> SlotBuffer::_node<value_type> * 
+auto SlotBuffer::_allocate_node(value_type && v) -> SlotBuffer::_node<value_type> * 
 {
-    auto const real_attr_size = _attrs_size + DEBUG_ATTRS;
-    auto attrs = grzeroalloc<int16>(real_attr_size);
-    auto node = attrs ? new _node<value_type>() : nullptr;
+    // auto const real_attr_size = _attrs_size + DEBUG_ATTRS;
+    // auto attrs = grzeroalloc<int16>(real_attr_size);
+    auto node = new _node<value_type>{std::forward<value_type>(v)};
     if (!node) { 
-        free(attrs); 
+        // free(attrs); 
         return nullptr;
     }
-    node->_value.userAttrs(attrs);
+    // node->_value.userAttrs(attrs);
     return static_cast<_node<value_type> *>(node);
 }
 
diff --git a/src/inc/Segment.h b/src/inc/Segment.h
index b7bad22..7bfbf4e 100644
--- a/src/inc/Segment.h
+++ b/src/inc/Segment.h
@@ -46,9 +46,6 @@
 namespace graphite2 {
 
 typedef vector<Features>        FeatureList;
-//typedef vector<Slot *>          SlotRope;
-//typedef vector<int16 *>         AttributeRope;
-typedef vector<SlotJustify *>   JustifyRope;
 
 class Font;
 class Segment;
@@ -98,8 +95,6 @@
     void appendSlot(int i, int cid, int gid, int fid, size_t coffset);
     SlotBuffer::iterator newSlot();
     void freeSlot(SlotBuffer::iterator);
-    SlotJustify *newJustify();
-    void freeJustify(SlotJustify *aJustify);
 
     Position positionSlots(
         Font const * font, 
@@ -140,7 +135,6 @@
     void doMirror(uint16 aMirror);
     SlotBuffer::iterator addLineEnd(SlotBuffer::iterator nSlot);
     void delLineEnd(SlotBuffer::iterator s);
-    bool hasJustification() const { return m_justifies.size() != 0; }
     void reverseSlots();
 
     bool isWhitespace(const int cid) const;
@@ -161,10 +155,7 @@
 private:
     SlotBuffer      m_srope;
     Position        m_advance;          // whole segment advance
-    JustifyRope     m_justifies;        // Slot justification info buffers
     FeatureList     m_feats;            // feature settings referenced by charinfos in this segment
-    //TODO: rm Slot          * m_freeSlots;        // linked list of free slots
-    SlotJustify   * m_freeJustifies;    // Slot justification blocks free list
     CharInfo      * m_charinfo;         // character info, one per input character
     SlotCollision * m_collisions;
     const Face    * m_face;             // GrFace
diff --git a/src/inc/Silf.h b/src/inc/Silf.h
index 133d7ba..49093c9 100644
--- a/src/inc/Silf.h
+++ b/src/inc/Silf.h
@@ -95,7 +95,7 @@
     uint16 numClasses() const { return m_nClass; }
     byte  flags() const { return m_flags; }
     byte  dir() const { return m_dir; }
-    uint8 numJustLevels() const { return m_numJusts; }
+    size_t numJustLevels() const { return m_numJusts; }
     Justinfo *justAttrs() const { return m_justs; }
     uint16 endLineGlyphid() const { return m_gEndLine; }
     const gr_faceinfo *silfInfo() const { return &m_silfinfo; }
diff --git a/src/inc/Slot.h b/src/inc/Slot.h
index a6a7bc7..8ba5174 100644
--- a/src/inc/Slot.h
+++ b/src/inc/Slot.h
@@ -26,6 +26,7 @@
 */
 #pragma once
 
+#include <cstring>
 #include "graphite2/Segment.h"
 
 #include "inc/Main.h"
@@ -41,39 +42,52 @@
 class ShapingContext;
 class Font;
 
-struct SlotJustify
-{
-    static const int NUMJUSTPARAMS = 5;
-
-    SlotJustify(const SlotJustify &);
-    SlotJustify & operator = (const SlotJustify &);
-
-public:
-    static size_t size_of(size_t levels) { return sizeof(SlotJustify) + ((levels > 1 ? levels : 1)*NUMJUSTPARAMS - 1)*sizeof(int16); }
-
-    void LoadSlot(const Slot &s, const Segment & seg);
-
-    SlotJustify *next;
-    int16 values[1];
-};
-
 class Slot
 {
-    enum Flag
-    {
-        DELETED     = 1,
-        INSERTED    = 2,
-        COPIED      = 4,
-        POSITIONED  = 8,
-        ATTACHED    = 16,
-        END_OF_LINE = 32
+    static constexpr int NUMJUSTPARAMS = 5;
+
+    union attributes {
+    private:
+        struct {
+            uint8_t size;
+            int16_t data[sizeof(uintptr_t)/sizeof(int16_t)-1];
+        }   local;
+        struct { 
+            uint8_t n_attrs, n_justs; 
+            int16_t data[1]; 
+        } * external;
+
+        bool is_inline() const { return !external || uintptr_t(external) & 0x3;}
+
+    public:
+        attributes(size_t n_attrs, size_t n_justs = 0): external(nullptr) { reserve(n_attrs, n_justs); }
+        attributes(attributes const & rhs): external{rhs.external} { operator = (rhs); }
+        attributes(attributes && rhs) noexcept : external{rhs.external} { rhs.external = nullptr; }
+        ~attributes() noexcept { if (!is_inline()) free(external); }
+
+        void reserve(size_t target_num_attrs, size_t target_num_justs);
+        attributes & operator = (attributes const & rhs);
+        attributes & operator = (attributes && rhs) noexcept;
+
+        size_t num_attrs() const { return is_inline() ? local.size : external->n_attrs + 1; }
+        size_t num_justs() const { return is_inline() ? 0 : external->n_justs; }
+
+        int16_t       * user_attributes() { return is_inline() ? local.data : external->data; }
+        int16_t const * user_attributes() const { return is_inline() ? local.data : external->data; }
+        int16_t       * just_info() { return is_inline() ? nullptr : external->data + external->n_attrs; }
+        int16_t const * just_info() const { return is_inline() ? nullptr : external->data + external->n_attrs; }
     };
 
+    bool has_justify() const { return m_attrs.num_justs() != 0; };
+    void init_just_infos(Segment const & seg);
+
 public:
     struct iterator;
 
-    Slot(Slot const &) = default;
-    Slot & operator=(Slot const & rhs) noexcept;
+    Slot(Slot const &) = delete;
+    Slot(Slot &&);
+    Slot & operator=(Slot const &) noexcept;
+    Slot & operator=(Slot &&) noexcept;
 
     unsigned short gid() const { return m_glyphid; }
     Position origin() const { return m_position; }
@@ -85,8 +99,7 @@
     uint32 index() const { return m_index; }
     void index(uint32 val) { m_index = val; }
 
-    Slot(int16 *m_userAttr = NULL);
-    void set(const Slot & slot, int charOffset, size_t numUserAttr, size_t justLevels, size_t numChars);
+    Slot(size_t num_attrs = 0);
     uint16 glyph() const { return m_realglyphid ? m_realglyphid : m_glyphid; }
     void setGlyph(Segment &seg, uint16 glyphid, const GlyphFace * theGlyph = NULL);
     void setRealGid(uint16 realGid) { m_realglyphid = realGid; }
@@ -99,28 +112,27 @@
     bool isBase() const { return (!m_parent); }
     void update(int numSlots, int numCharInfo, Position &relpos);
     Position finalise(const Segment & seg, const Font* font, Position & base, Rect & bbox, uint8 attrLevel, float & clusterMin, bool rtl, bool isFinal, int depth = 0);
-    bool isDeleted() const { return m_flags & DELETED; }
-    void markDeleted(bool state) { if (state) m_flags |= DELETED; else m_flags &= ~DELETED; }
-    bool isCopied() const { return m_flags & COPIED; }
-    void markCopied(bool state) { if (state) m_flags |= COPIED; else m_flags &= ~COPIED; }
-    bool isPositioned() const { return m_flags; }
-    void markPositioned(bool state) { if (state) m_flags |= POSITIONED; else m_flags &= ~POSITIONED; }
-    bool isEndOfLine() const { return m_flags & END_OF_LINE; }
-    void markEndOfLine(bool state) { if (state) m_flags |= END_OF_LINE; else m_flags &= ~END_OF_LINE; }
-    bool isInsertBefore() const { return !(m_flags & INSERTED); }
+    bool isDeleted() const { return m_flags.deleted; }
+    void markDeleted(bool state) { m_flags.deleted = state; }
+    bool isCopied() const { return m_flags.copied; }
+    void markCopied(bool state) { m_flags.copied = state; }
+    bool isPositioned() const { return m_flags.positioned; }
+    void markPositioned(bool state) { m_flags.positioned = state; }
+    bool isEndOfLine() const { return m_flags.eol; }
+    void markEndOfLine(bool state) { m_flags.eol = state; }
+    bool isInsertBefore() const { return !m_flags.inserted; }
+    void markInsertBefore(bool state) { m_flags.inserted = !state; }
     uint8 getBidiLevel() const { return m_bidiLevel; }
     void setBidiLevel(uint8 level) { m_bidiLevel = level; }
     int8 getBidiClass(const Segment &seg);
     int8 getBidiClass() const { return m_bidiCls; }
     void setBidiClass(int8 cls) { m_bidiCls = cls; }
-    int16 *userAttrs() const { return m_userAttr; }
-    void userAttrs(int16 *p) { m_userAttr = p; }
-    void markInsertBefore(bool state) { if (!state) m_flags |= INSERTED; else m_flags &= ~INSERTED; }
+    int16 const *userAttrs() const { return m_attrs.user_attributes(); }
+    // void userAttrs(int16 *p) { m_userAttr = p; }
     void setAttr(Segment & seg, attrCode ind, uint8 subindex, int16 val, const ShapingContext & map);
     int getAttr(const Segment &seg, attrCode ind, uint8 subindex) const;
     int getJustify(const Segment &seg, uint8 level, uint8 subindex) const;
     void setJustify(Segment &seg, uint8 level, uint8 subindex, int16 value);
-    bool isLocalJustify() const { return m_justs != NULL; };
     void attachTo(Slot *ap) { m_parent = ap; }
     Slot *attachedTo() const { return m_parent; }
     Position attachOffset() const { return m_attach - m_with; }
@@ -139,31 +151,43 @@
     Slot *nextInCluster(const Slot *s) const;
     bool isChildOf(const Slot *base) const;
 
+#if !defined GRAPHITE2_NTRACING
+    ~Slot() noexcept { ++m_gen; }
+    uintptr_t generation() const { return m_gen; }
+#endif
     CLASS_NEW_DELETE
 
 private:
-    unsigned short m_glyphid;        // glyph id
-    uint16 m_realglyphid;
-    uint32 m_original;      // charinfo that originated this slot (e.g. for feature values)
-    uint32 m_before;        // charinfo index of before association
-    uint32 m_after;         // charinfo index of after association
-    uint32 m_index;         // slot index given to this slot during finalising
-    Slot *m_parent;         // index to parent we are attached to
-    Slot *m_child;          // index to first child slot that attaches to us
-    Slot *m_sibling;        // index to next child that attaches to our parent
-    Position m_position;    // absolute position of glyph
-    Position m_shift;       // .shift slot attribute
-    Position m_advance;     // .advance slot attribute
-    Position m_attach;      // attachment point on us
-    Position m_with;        // attachment point position on parent
-    float    m_just;        // Justification inserted space
-    uint8    m_flags;       // holds bit flags
-    byte     m_attLevel;    // attachment level
-    int8     m_bidiCls;     // bidirectional class
-    byte     m_bidiLevel;   // bidirectional level
-    int16   *m_userAttr;    // pointer to user attributes
-    SlotJustify *m_justs;   // pointer to justification parameters
-
+    Slot      * m_parent;   // index to parent we are attached to
+    Slot      * m_child;    // index to first child slot that attaches to us
+    Slot      * m_sibling;  // index to next child that attaches to our parent
+    Position    m_position; // absolute position of glyph
+    Position    m_shift;    // .shift slot attribute
+    Position    m_advance;  // .advance slot attribute
+    Position    m_attach;   // attachment point on us
+    Position    m_with;     // attachment point position on parent
+    attributes  m_attrs;
+    float       m_just;     // Justification inserted space
+    uint32      m_original; // charinfo that originated this slot (e.g. for feature values)
+    uint32      m_before;   // charinfo index of before association
+    uint32      m_after;    // charinfo index of after association
+    uint32      m_index;    // slot index given to this slot during finalising
+    uint16      m_glyphid;  // glyph id
+    uint16      m_realglyphid;
+    byte        m_attLevel;    // attachment level
+    byte        m_bidiLevel;   // bidirectional level
+    int8        m_bidiCls;     // bidirectional class
+    struct {
+        bool    deleted: 1,
+                inserted: 1,
+                copied: 1,
+                positioned: 1,
+                attached: 1,
+                eol:1;
+    }        m_flags;       // holds bit flags
+#if !defined GRAPHITE2_NTRACING
+    uint8_t   m_gen;
+#endif
     friend class Segment;
 };
 
diff --git a/src/inc/SlotBuffer.h b/src/inc/SlotBuffer.h
index 286c2b3..7fd2537 100644
--- a/src/inc/SlotBuffer.h
+++ b/src/inc/SlotBuffer.h
@@ -64,6 +64,11 @@
         T _value;
 
         CLASS_NEW_DELETE;
+        
+        _node(T && v) : _value(std::forward<T>(v)) {}
+
+        template<typename... Args> 
+        _node(Args &&... args) { new (&_value) T(std::forward<Args>(args)...); }
     };
 
     struct _head_node : public _node_linkage {
@@ -91,11 +96,12 @@
     _head_node          _head;
     _node_linkage       _garbage;   // TODO: Move into slot map or merge slotmap into here.
     size_type           _size;
-    size_type           _attrs_size;
+    // uint8_t             _attrs_size,
+    //                     _justs_size;
 
     template<typename T> class _iterator;
 
-    _node<Slot> *   _allocate_node();
+    _node<Slot> *   _allocate_node(value_type &&);
     void            _free_node(_node<Slot> * const);
     void            _reverse_range(_node_linkage * const start, _node_linkage * const end);
 
@@ -115,10 +121,11 @@
     iterator newSlot();
     void     freeSlot(iterator i);
 
-    SlotBuffer(size_t num_user);
+    SlotBuffer(/*size_t num_user, size_t num_justs*/);
     ~SlotBuffer()   { clear(); }
 
-    size_type       num_user_attrs() const noexcept { return _attrs_size; }
+    // size_type       num_user_attrs() const noexcept { return _attrs_size; }
+    // size_type       num_just_levels() const noexcept { return _justs_size; }
 
     iterator        begin() noexcept;
     const_iterator  begin() const noexcept;
@@ -145,9 +152,14 @@
 
     iterator insert(const_iterator pos, value_type const &);
     iterator insert(const_iterator pos, value_type &&);
+    template <typename... Args>
+    iterator emplace(const_iterator pos, Args &&... args);
 
-    void push_back(value_type const &);
-    void push_back(value_type &&);
+    void push_back(value_type const &v);
+    void push_back(value_type &&v);
+
+    template <typename... Args>
+    void emplace_back(Args &&... args) { emplace(end(), std::forward(args)...); }
 
     void pop_back();
 
@@ -246,6 +258,17 @@
     operator _iterator<T const>() const noexcept { return _iterator<T const>(_p); }
 };
 
+template <typename... Args>
+inline
+auto SlotBuffer::emplace(const_iterator pos, Args &&... args) -> iterator {
+    assert(pos._p);
+    auto node = new _node<value_type>(std::forward<Args>(args)...);
+    if (!node) return end();
+    node->link(*const_cast<_node_linkage *>(pos._p));
+    ++_size;
+    return iterator(node);
+}
+
 inline
 void SlotBuffer::swap(SlotBuffer & rhs) {
     SlotBuffer tmp{std::move(rhs)};
@@ -255,7 +278,7 @@
 
 inline 
 auto SlotBuffer::newSlot() -> iterator { 
-    auto r = _allocate_node(); 
+    auto r = _allocate_node(Slot()); 
     if (!r) return end();
     r->_next = r->_prev = nullptr; 
     return iterator(r); 
diff --git a/src/inc/debug.h b/src/inc/debug.h
index 2eb93b7..d38e020 100644
--- a/src/inc/debug.h
+++ b/src/inc/debug.h
@@ -60,7 +60,7 @@
 objectid::objectid(SlotBuffer::const_iterator const s) noexcept
 {
     void const * o = s.handle();
-    set_name(o, o ? size_t(s->userAttrs()) : 0);
+    set_name(o, o ? s->generation() : 0);
 }
 
 
diff --git a/src/inc/opcodes.h b/src/inc/opcodes.h
index abd935d..38d2164 100644
--- a/src/inc/opcodes.h
+++ b/src/inc/opcodes.h
@@ -250,13 +250,8 @@
         auto ref = slotat(slot_ref);
         if (ref != reg.seg.slots().end() && ref != reg.os)
         {
-            int16 *tempUserAttrs = reg.is->userAttrs();
             if (reg.os->attachedTo() || reg.os->firstChild()) DIE
-            memcpy(tempUserAttrs, ref->userAttrs(), reg.seg.numAttrs() * sizeof(uint16));
-            memcpy(&*reg.is, &*ref, sizeof(Slot));
-            reg.is->firstChild(NULL);
-            reg.is->nextSibling(NULL);
-            reg.is->userAttrs(tempUserAttrs);
+            *reg.os = *ref;
             if (reg.os->attachedTo())
                 reg.os->attachedTo()->child(&*reg.os);
         }
@@ -632,10 +627,7 @@
     auto slot = reg.seg.newSlot();
     if (slot == reg.seg.slots().end() || reg.os == reg.seg.slots().end()) DIE;
     // copy slot reg.os into new slot
-    int16 *tempUserAttrs = slot->userAttrs();
-    memcpy(&*slot, &*reg.is, sizeof(Slot));
-    memcpy(tempUserAttrs, reg.is->userAttrs(), reg.seg.numAttrs() * sizeof(uint16));
-    slot->userAttrs(tempUserAttrs);
+    *slot = *reg.os;
     slot->markCopied(true);
     // TODO: remove this once we're using gr::list methods. This is the
     // hack that, that enables the hack, that enables debug output.
diff --git a/tests/vm/basic_test.cpp b/tests/vm/basic_test.cpp
index b22aaf9..b42682c 100644
--- a/tests/vm/basic_test.cpp
+++ b/tests/vm/basic_test.cpp
@@ -105,14 +105,15 @@
 
     // run the program
     auto dummy_segment = grzeroalloc<Segment>(1);
-    SlotBuffer sb(32);
+    SlotBuffer sb;
     uint32 ret = 0;
     ShapingContext ctxt(*dummy_segment, 0, 0);
     Machine m(ctxt);
     ctxt.pushSlot(sb.newSlot());
     slotref * map = ctxt.map.begin();
+    auto slot_out = *map;
     for(size_t n = repeats; n; --n) {
-        ret = prog.run(m, map);
+        ret = prog.run(m, map, slot_out);
         switch (m.status()) {
             case Machine::stack_underflow:
             case Machine::stack_overflow: